728x90
๋ฌธ์
ํ์ด
1. 0์ ๋ฌถ์์ ๊ทธ๋ฃน์ผ๋ก ํ์ํด์ค๋ค.
2. ๊ฐ ๊ทธ๋ฃน๋ณ๋ก 0์ ๊ฐ์๊ฐ ๋ช ๊ฐ์ธ์ง dict๋ฅผ ํตํด ์ ์ฅํ๋ค.
3. ๋ฒฝ(1)์์ ์ํ์ข์ฐ๋ก ์ธ์ ํ ๊ทธ๋ฃน์ ๊ตฌํ ๋ค, ๊ทธ ๊ทธ๋ฃน์ ํด๋นํ๋ 0์ ๊ฐ์๋ฅผ ๋ํด์ค๋ค.
๊ทธ๋ฃน์ด ์ค๋ณต๋์ง ์๊ฒ set()์ ์ฌ์ฉํ๋ค.
์ฝ๋
from collections import deque
input = __import__('sys').stdin.readline
def bfs(start):
q = deque()
q.append(start)
cnt = 1
while q:
i, j = q.popleft()
zeros[i][j] = group
for idx in range(4):
ni, nj = i + dy[idx], j + dx[idx]
if ni < 0 or ni >= n or nj < 0 or nj >= m or visited[ni][nj] or graph[ni][nj] == 1:
continue
visited[ni][nj] = True
q.append((ni, nj))
cnt += 1
return cnt
n, m = map(int, input().split())
graph = [list(map(int, input().strip())) for _ in range(n)]
visited = [[False] * m for _ in range(n)]
zeros = [[0] * m for _ in range(n)]
group = 1
info = {}
info[0] = 0
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
for i in range(n):
for j in range(m):
if graph[i][j] == 0 and not visited[i][j]:
visited[i][j] = True
w = bfs((i, j))
info[group] = w
group += 1
for i in range(n):
for j in range(m):
addList = set()
if graph[i][j]:
for idx in range(4):
ni, nj = i + dy[idx], j + dx[idx]
if ni < 0 or ni >= n or nj < 0 or nj >= m:
continue
addList.add(zeros[ni][nj])
for add in addList:
graph[i][j] += info[add]
graph[i][j] %= 10
for g in graph:
print("".join(map(str, g)))
๋ง๋ฌด๋ฆฌ
๋๋ฌด ์ด๋ ค์์ ๋ฒฝ ๋๊ผ๋ค...
ํผ์์๋ ๋์ ํ ๋ชป ํ์์ ๋ฌธ์ ใ
728x90