Notice
Recent Posts
Recent Comments
Link
관리 메뉴

one by one ◼◻◼◻

백준 2178 , 미로탐색 본문

코딩문제

백준 2178 , 미로탐색

JihyunLee 2022. 6. 16. 15:14

 

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
import sys
from collections import deque
dx = [-1,1,0,0]
dy = [0,0,-1,1]
x, y = (map(int, sys.stdin.readline().split()))
data = [[0* y for _ in range(x)]
visit = [[0* y for _ in range(x)]
 
for i in range(x):
    line = list(sys.stdin.readline().split()[0])
    for j, temp in enumerate(line):
        data[i][j] = int(temp)
 
togo = deque()
togo.append((0,0))
visit[0][0= 1
while togo:
    x_, y_ = togo.popleft()
    if x_ == x-1 and y_ == y-1:
        print(visit[x_][y_])
    else:
        for i in range(4):
            x_move, y_move = x_ + dx[i], y_ + dy[i]
            if x_move >=0 and x_move< x and y_move>=0 and y_move<y:
                if data[x_move][y_move]==1 and visit[x_move][y_move] ==0:
                    visit[x_move][y_move] = visit[x_][y_] +1
                    togo.append((x_move, y_move))
 
            
 
cs
Comments