Post

🐣 게임 맵 최단거리

1. 문제 링크

게임 맵 최단거리


2. 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from collections import deque

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

def bfs(maps, x, y):
    queue = deque()
    queue.append((x, y))

    while queue:
        x, y = queue.popleft()

        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]

            if nx < 0 or nx >= len(maps) or ny < 0 or ny >= len(maps[0]): continue

            if maps[nx][ny] == 0:  continue

            if maps[nx][ny] == 1:
                maps[nx][ny] = maps[x][y] + 1
                queue.append((nx, ny))
    return maps[len(maps)-1][len(maps[0])-1]


def solution(maps):
    answer = bfs(maps, 0, 0)
    print(maps)
    return -1 if answer == 1 else answer    # 상대 팀 진영에 도착할 수 없을 때 -1
This post is licensed under CC BY 4.0 by the author.