diff --git "a/\353\260\261\354\244\200 14940\353\262\210 \354\211\254\354\232\264 \354\265\234\353\213\250\352\261\260\353\246\254/main.py" "b/\353\260\261\354\244\200 14940\353\262\210 \354\211\254\354\232\264 \354\265\234\353\213\250\352\261\260\353\246\254/main.py" new file mode 100644 index 0000000..864ff23 --- /dev/null +++ "b/\353\260\261\354\244\200 14940\353\262\210 \354\211\254\354\232\264 \354\265\234\353\213\250\352\261\260\353\246\254/main.py" @@ -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() diff --git "a/\353\260\261\354\244\200 14940\353\262\210 \354\211\254\354\232\264 \354\265\234\353\213\250\352\261\260\353\246\254/test1.txt" "b/\353\260\261\354\244\200 14940\353\262\210 \354\211\254\354\232\264 \354\265\234\353\213\250\352\261\260\353\246\254/test1.txt" new file mode 100644 index 0000000..275fbfd --- /dev/null +++ "b/\353\260\261\354\244\200 14940\353\262\210 \354\211\254\354\232\264 \354\265\234\353\213\250\352\261\260\353\246\254/test1.txt" @@ -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 diff --git "a/\353\260\261\354\244\200 14940\353\262\210 \354\211\254\354\232\264 \354\265\234\353\213\250\352\261\260\353\246\254/test1_answer.txt" "b/\353\260\261\354\244\200 14940\353\262\210 \354\211\254\354\232\264 \354\265\234\353\213\250\352\261\260\353\246\254/test1_answer.txt" new file mode 100644 index 0000000..38515e0 --- /dev/null +++ "b/\353\260\261\354\244\200 14940\353\262\210 \354\211\254\354\232\264 \354\265\234\353\213\250\352\261\260\353\246\254/test1_answer.txt" @@ -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 diff --git "a/\353\260\261\354\244\200 14940\353\262\210 \354\211\254\354\232\264 \354\265\234\353\213\250\352\261\260\353\246\254/test_main.py" "b/\353\260\261\354\244\200 14940\353\262\210 \354\211\254\354\232\264 \354\265\234\353\213\250\352\261\260\353\246\254/test_main.py" new file mode 100644 index 0000000..a987c34 --- /dev/null +++ "b/\353\260\261\354\244\200 14940\353\262\210 \354\211\254\354\232\264 \354\265\234\353\213\250\352\261\260\353\246\254/test_main.py" @@ -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') \ No newline at end of file