-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |