Skip to content

Commit 96d1a98

Browse files
authored
Add files via upload
1 parent 40041d4 commit 96d1a98

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from collections import deque
2+
3+
n, m, t = map(int, input().split())
4+
graph=[] #0 빔, 1 벽, 2 그람
5+
for _ in range(n):
6+
graph.append(list(map(int, input().split())))
7+
8+
#최단 거리 vs 그람까지 최단 거리 + 그람에서 도착지까지 최단거리
9+
def bfs():
10+
tmp=float('inf')
11+
q=deque()
12+
q.append([0, 0])
13+
visited=[[0]*m for _ in range(n)]
14+
15+
while q:
16+
r, c=q.popleft()
17+
if graph[r][c]==2:
18+
tmp=visited[r][c]+abs(n-1-r)+abs(m-1-c)
19+
if r==n-1 and c==m-1:
20+
return min(visited[r][c], tmp)
21+
for dr, dc in [0, -1], [0, 1], [1, 0], [-1, 0]:
22+
nr, nc = r+dr, c+dc
23+
if 0<=nr<n and 0<=nc<m and graph[nr][nc]!=1 and not visited[nr][nc]:
24+
q.append([nr, nc])
25+
visited[nr][nc]=visited[r][c]+1
26+
return tmp
27+
28+
result=bfs()
29+
30+
if result<=t:
31+
print(result)
32+
else:
33+
print("Fail")

0 commit comments

Comments
 (0)