๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/boj

BOJ 16173 - ์ ํ”„์™• ์ฉฐ๋ฆฌ(small)

by HandHand 2021. 3. 18.

๋ฌธ์ œ

๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 16173๋ฒˆ

ํ’€์ด ๊ณผ์ •

์‹œ์ž‘ ์ง€์ ์—์„œ ๋ชฉํ‘œ ์ง€์ ๊นŒ์ง€์˜ ๋„๋‹ฌ ๊ฐ€๋Šฅ์„ฑ ์œ ๋ฌด๋ฅผ ํŒ๋‹จํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

BFS ๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ ์นธ์˜ ์ด๋™๋ ฅ์— ๋”ฐ๋ผ ํƒ์ƒ‰์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ


import sys
from collections import deque

N = int(input())
board = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]

dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]


def bfs(sx, sy):
    q = deque()
    visit = [[0] * N for _ in range(N)]

    q.append([sx, sy])

    while q:
        x, y = q.popleft()
        if board[x][y] == -1:
            return True

        offset = board[x][y]
        for dir in range(4):
            nx, ny = x + dx[dir] * offset, y + dy[dir] * offset
            if 0 <= nx < N and 0 <= ny < N and not visit[nx][ny]:
                visit[nx][ny] = 1
                q.append([nx, ny])

    return False


def solution():
    is_reachable = bfs(0, 0)
    return 'HaruHaru' if is_reachable else 'Hing'


print(solution())
๋ฐ˜์‘ํ˜•

๐Ÿ’ฌ ๋Œ“๊ธ€