Skip to content

Commit 76f7857

Browse files
committed
[D4] Title: [S/W 문제해결 응용] 4일차 - 보급로, Time: 151 ms, Memory: 64,000 KB -BaekjoonHub
1 parent ca720d4 commit 76f7857

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# [D4] [S/W 문제해결 응용] 4일차 - 보급로 - 1249
2+
3+
[문제 링크](https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV15QRX6APsCFAYD)
4+
5+
### 성능 요약
6+
7+
메모리: 64,000 KB, 시간: 151 ms, 코드길이: 835 Bytes
8+
9+
### 제출 일자
10+
11+
2025-05-24 12:38
12+
13+
14+
15+
> 출처: SW Expert Academy, https://swexpertacademy.com/main/code/problem/problemList.do
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from collections import deque
2+
3+
def bfs(graph):
4+
N = len(graph)
5+
visited = [[float('inf')] * N for _ in range(N)]
6+
visited[0][0] = 0
7+
q = deque([(0, 0)])
8+
9+
dx = [1, -1, 0, 0]
10+
dy = [0, 0, 1, -1]
11+
12+
while q:
13+
x, y = q.popleft()
14+
15+
if x == N-1 and y == N-1:
16+
continue
17+
18+
for i in range(4):
19+
nx = x + dx[i]
20+
ny = y + dy[i]
21+
22+
if 0 <= nx < N and 0 <= ny < N:
23+
new_time = visited[x][y] + graph[nx][ny]
24+
if new_time < visited[nx][ny]:
25+
visited[nx][ny] = new_time
26+
q.append((nx, ny))
27+
28+
return visited[N-1][N-1]
29+
30+
T = int(input())
31+
32+
for tc in range(1, T+1):
33+
N = int(input())
34+
graph = [list(map(int, input())) for _ in range(N)]
35+
36+
print(f"#{tc} {bfs(graph)}")

0 commit comments

Comments
 (0)