Computer Science/Algorithm
[Baekjoon] DFS/BFS - 16173 점프왕 쩰리 (Small)
ddu0422
2023. 9. 4. 13:46
문제
16173번: 점프왕 쩰리 (Small)
쩰리는 맨 왼쪽 위의 칸에서 출발해 (행, 열)로 나타낸 좌표계로, (1, 1) -> (2, 1) -> (3, 1) -> (3, 3)으로 이동해 게임에서 승리할 수 있다.
www.acmicpc.net
풀이
import sys
n = int(sys.stdin.readline().rstrip())
maps = []
for _ in range(n):
lines = list(map(int ,sys.stdin.readline().rstrip().split()))
maps.append(lines)
dx = [1, 0]
dy = [0, 1]
visited = [[False for _ in range(n)] for _ in range(n)]
def solution(maps, visited):
queue = [(0, 0, maps[0][0])]
visited[0][0] = True
while queue:
x, y, distance = queue.pop()
for i in range(len(dx)):
nx = x + dx[i] * distance
ny = y + dy[i] * distance
if not (0 <= nx < n and 0 <= ny < n):
continue
if visited[nx][ny]:
continue
queue.append((nx, ny, maps[nx][ny]))
visited[nx][ny] = True
if maps[nx][ny] == -1:
return "Haru" * 2
return "Hing"
print(solution(maps, visited))