https://www.acmicpc.net/problem/14940
각 좌표마다 거리를 측정해서 숫자를 지정해 주는 것은 어떻게 하는지 몰라서 고민하다가 전에 풀었던 https://alpacalabs.tistory.com/18 에 토마토 문제랑 거의 흡사하게 풀면 되겠다고 생각하고 풀었다.
import sys
imput = sys.stdin.readline
row_length , col_length = map(int,input().split())
visited = [[False for _ in range(col_length)] for _ in range(row_length)]
Map = list()
resultMap = [[0 for _ in range(col_length)]for _ in range(row_length) ]
stack1 = list()
stack2 = list()
for i in range(row_length):
Map.append(list(map(int,input().split())))
for row in range(row_length):
for col in range(col_length):
if Map[row][col] == 2:
start = (row,col)
visited[start[0]][start[1]] = True
length = 0
resultMap[start[0]][start[1]] = 0
stack1.append(start)
while stack1:
stack2 = stack1[:]
stack1.clear()
length += 1
while stack2:
row , col = stack2.pop()
if row+1 < row_length and Map[row+1][col] == 1 and visited[row+1][col] == False:
stack1.append((row+1,col))
resultMap[row+1][col] = length
visited[row+1][col] = True
if col+1 < col_length and Map[row][col+1] == 1 and visited[row][col+1] == False:
stack1.append((row,col+1))
resultMap[row][col+1] = length
visited[row][col+1] = True
if row-1 >= 0 and Map[row-1][col] == 1 and visited[row-1][col] == False:
stack1.append((row-1,col))
resultMap[row-1][col] = length
visited[row-1][col] = True
if col-1 >= 0 and Map[row][col-1] == 1 and visited[row][col-1] == False:
stack1.append((row,col-1))
resultMap[row][col-1] = length
visited[row][col-1] = True
result = True
for i in range(row_length):
for j in range(col_length):
if resultMap[i][j] == 0 and Map[i][j] == 1 and not visited[i][j]:
resultMap[i][j] = -1
if result:
for r in range(len(resultMap)):
print(' '.join(map(str,resultMap[r])))
----------입력값--------------
15 15
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 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 0 0 0 0 0
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
-------------- 출력 값------------------------
14 13 12 11 10 9 8 7 8 9 10 11 12 13 14
13 12 11 10 9 8 7 6 7 8 9 10 11 12 13
12 11 10 9 8 7 6 5 6 7 8 9 10 11 12
11 10 9 8 7 6 5 4 5 6 7 8 9 10 11
10 9 8 7 6 5 4 3 4 5 6 7 8 9 10
9 8 7 6 5 4 3 2 3 4 5 6 7 8 9
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
7 6 5 4 3 2 1 0 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 4 5 6 7 8 9 10
11 10 9 8 7 6 5 4 5 6 0 0 0 0 0
12 11 10 9 8 7 6 5 6 7 0 -1 -1 -1 -1
13 12 11 10 9 8 7 6 7 8 0 -1 0 0 0
14 13 12 11 10 9 8 7 8 9 0 -1 -1 -1 -1
'코딩테스트' 카테고리의 다른 글
[python] 백준 17219 : 비밀번호 찾기 (실버 4) (0) | 2024.07.17 |
---|---|
[python] 백준 18870 번 : 좌표 압축 (실버 2) (0) | 2024.07.16 |
[python] 백준 11724 : 연결 요소의 개수 (실버 2) (0) | 2024.07.15 |
[python] 백준 11659 : 구간 합 구하기 4 (실버 3) (0) | 2024.07.13 |
[python] 백준 7576 번 : 토마토 (골드 5) (0) | 2024.07.13 |