Skip to content

Commit

Permalink
백준 14940번 쉬운 최단거리
Browse files Browse the repository at this point in the history
  • Loading branch information
skysign committed Aug 21, 2024
1 parent e73901a commit e110fdd
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
58 changes: 58 additions & 0 deletions 백준 14940번 쉬운 최단거리/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import sys
from typing import List


def solve():
row, col = map(int, sys.stdin.readline().strip().split(' '))
board = [[0 for _ in range(col)] for _ in range(row)]

row_start = 0
col_start = 0

for row_idx in range(row):
line = list(map(int, sys.stdin.readline().strip().split(' ')))

for col_idx in range(col):
board[row_idx][col_idx] = line[col_idx]

if 2 == line[col_idx]:
row_start = row_idx
col_start = col_idx

board2 = bfs(board, row_start, col_start, row, col)

for row_idx in range(row):
for col_idx in range(col):
if sys.maxsize == board2[row_idx][col_idx] and board[row_idx][col_idx] == 0:
board2[row_idx][col_idx] = 0
if sys.maxsize == board2[row_idx][col_idx] and board[row_idx][col_idx] == 1:
board2[row_idx][col_idx] = -1

for line in board2:
print(' '.join(map(str, line)).strip())


def bfs(board: List[List[int]], row_start: int, col_start: int, row_max: int, col_max: int):
board2 = [[sys.maxsize for _ in range(col_max)] for _ in range(row_max)]
board2[row_start][col_start] = 0

queue = [[row_start, col_start, 0]]
drc = [[1, 0], [0, 1], [-1, 0], [0, -1]]

while queue:
rs, rc, v = queue.pop(0)

for dr, dc in drc:
nr, nc = rs + dr, rc + dc

if 0 <= nr < row_max and 0 <= nc < col_max:
if board[nr][nc] == 1:
if board2[nr][nc] > v + 1:
queue.append([nr, nc, v + 1])
board2[nr][nc] = v + 1

return board2


if __name__ == '__main__':
solve()
16 changes: 16 additions & 0 deletions 백준 14940번 쉬운 최단거리/test1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
15 15
2 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1 0 1 0 0 0
1 1 1 1 1 1 1 1 1 1 0 1 1 1 1
15 changes: 15 additions & 0 deletions 백준 14940번 쉬운 최단거리/test1_answer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
11 12 13 14 15 16 17 18 19 20 0 0 0 0 25
12 13 14 15 16 17 18 19 20 21 0 29 28 27 26
13 14 15 16 17 18 19 20 21 22 0 30 0 0 0
14 15 16 17 18 19 20 21 22 23 0 31 32 33 34
23 changes: 23 additions & 0 deletions 백준 14940번 쉬운 최단거리/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import sys
from pathlib import Path
from unittest import TestCase
from main import solve


class Test(TestCase):
def my_solve(self, testcase_input):
sys.stdin = open(testcase_input, 'r')
stdout = sys.stdout
sys.stdout = open('stdout.txt', 'w')
solve()
sys.stdout.close()
sys.stdout = stdout

def test_solve(self, testcase_number: str):
self.my_solve('test' + testcase_number + '.txt')
self.assertEqual(
Path('test' + testcase_number + '_answer.txt').read_text().strip(),
Path('stdout.txt').read_text().strip())

def test1_solve(self):
self.test_solve('1')

0 comments on commit e110fdd

Please sign in to comment.