Skip to content

Commit e110fdd

Browse files
committed
백준 14940번 쉬운 최단거리
문제 링크: https://www.acmicpc.net/problem/14940
1 parent e73901a commit e110fdd

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import sys
2+
from typing import List
3+
4+
5+
def solve():
6+
row, col = map(int, sys.stdin.readline().strip().split(' '))
7+
board = [[0 for _ in range(col)] for _ in range(row)]
8+
9+
row_start = 0
10+
col_start = 0
11+
12+
for row_idx in range(row):
13+
line = list(map(int, sys.stdin.readline().strip().split(' ')))
14+
15+
for col_idx in range(col):
16+
board[row_idx][col_idx] = line[col_idx]
17+
18+
if 2 == line[col_idx]:
19+
row_start = row_idx
20+
col_start = col_idx
21+
22+
board2 = bfs(board, row_start, col_start, row, col)
23+
24+
for row_idx in range(row):
25+
for col_idx in range(col):
26+
if sys.maxsize == board2[row_idx][col_idx] and board[row_idx][col_idx] == 0:
27+
board2[row_idx][col_idx] = 0
28+
if sys.maxsize == board2[row_idx][col_idx] and board[row_idx][col_idx] == 1:
29+
board2[row_idx][col_idx] = -1
30+
31+
for line in board2:
32+
print(' '.join(map(str, line)).strip())
33+
34+
35+
def bfs(board: List[List[int]], row_start: int, col_start: int, row_max: int, col_max: int):
36+
board2 = [[sys.maxsize for _ in range(col_max)] for _ in range(row_max)]
37+
board2[row_start][col_start] = 0
38+
39+
queue = [[row_start, col_start, 0]]
40+
drc = [[1, 0], [0, 1], [-1, 0], [0, -1]]
41+
42+
while queue:
43+
rs, rc, v = queue.pop(0)
44+
45+
for dr, dc in drc:
46+
nr, nc = rs + dr, rc + dc
47+
48+
if 0 <= nr < row_max and 0 <= nc < col_max:
49+
if board[nr][nc] == 1:
50+
if board2[nr][nc] > v + 1:
51+
queue.append([nr, nc, v + 1])
52+
board2[nr][nc] = v + 1
53+
54+
return board2
55+
56+
57+
if __name__ == '__main__':
58+
solve()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
15 15
2+
2 1 1 1 1 1 1 1 1 1 1 1 1 1 1
3+
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
4+
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
5+
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
6+
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
7+
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
8+
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
9+
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
10+
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
11+
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
12+
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
13+
1 1 1 1 1 1 1 1 1 1 0 0 0 0 1
14+
1 1 1 1 1 1 1 1 1 1 0 1 1 1 1
15+
1 1 1 1 1 1 1 1 1 1 0 1 0 0 0
16+
1 1 1 1 1 1 1 1 1 1 0 1 1 1 1
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
2+
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
3+
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
4+
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
5+
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
6+
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
7+
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
8+
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
9+
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
10+
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
11+
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
12+
11 12 13 14 15 16 17 18 19 20 0 0 0 0 25
13+
12 13 14 15 16 17 18 19 20 21 0 29 28 27 26
14+
13 14 15 16 17 18 19 20 21 22 0 30 0 0 0
15+
14 15 16 17 18 19 20 21 22 23 0 31 32 33 34
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import sys
2+
from pathlib import Path
3+
from unittest import TestCase
4+
from main import solve
5+
6+
7+
class Test(TestCase):
8+
def my_solve(self, testcase_input):
9+
sys.stdin = open(testcase_input, 'r')
10+
stdout = sys.stdout
11+
sys.stdout = open('stdout.txt', 'w')
12+
solve()
13+
sys.stdout.close()
14+
sys.stdout = stdout
15+
16+
def test_solve(self, testcase_number: str):
17+
self.my_solve('test' + testcase_number + '.txt')
18+
self.assertEqual(
19+
Path('test' + testcase_number + '_answer.txt').read_text().strip(),
20+
Path('stdout.txt').read_text().strip())
21+
22+
def test1_solve(self):
23+
self.test_solve('1')

0 commit comments

Comments
 (0)