N x M, 한 칸에 상어 최대 1마리
이동 기준은 대각선 포함쓰... 가운데 제외 총 8개
구해야 하는 것 : 안전 거리의 최댓값
브루트폴스로 시작해서 그 loop안에 bfs함수 돌리는데
''' 1회차 '''
from collections import deque
def bfs(init_y,init_x,graph,visit,n, m):
que = deque()
que.append((init_y,init_x))
visit[init_y][init_x] = True
# 좌측 3개, 중앙 2개, 우측 3개
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
#dx = (-1, -1, -1, 0, 1, 1, 1, 0)
#dy = (-1, 0, 1, 1, 1, 0, -1, -1)
while que:
y, x = que.popleft()
for nextp in range(8):
ny = y + dy[nextp]
nx = x + dx[nextp]
#print(ny, nx)
#print(visit[ny][nx])
# 방문하지 않고, 범위 안에 있는 위치만
if 0<= ny < n and 0 <= nx < m and visit[ny][nx] == False:
#print(y, x, "-> go [", ny, nx, "]", que)
if graph[ny][nx] == 1:
#print("y,x=", y,x, "+",dy[nextp], dx[nextp], "ny,nx=",ny,nx)
return graph[y][x]
visit[ny][nx] = True
graph[ny][nx] = graph[y][x] + 1
que.append((ny,nx))
return 0
n, m = map(int,input().split())
#graph = [list(map(int, input().split())) for _ in range(n)]
graph = [[0, 0, 1, 0], [0, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]]
#graph = [[0, 0, 0, 1], [0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
#visit = [[False]*m]*n5
visit = [[-1] * m for _ in range(n)]
answer = []
for y in range(n):
for x in range(m):
if graph[y][x] == 1:
answer.append(bfs(y,x,graph,visit,n, m))
visit = [[False]*m]*n
print(max(answer))
print(answer)
print()
for i in graph:
print(i)
''' 답지 코드 참고 '''
from collections import deque
n, m = map(int,input().split())
graph = [list(map(int, input().split())) for _ in range(n)]
#graph = [[0, 0, 1, 0], [0, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]]
#graph = [[0, 0, 0, 1], [0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1]]
#visit = [[-1]*m]*n #<---------------이러면 답이 틀림
visit = [[-1] * m for _ in range(n)] #<----이래야 맞음 Why????
answer = 0
que = deque()
# (매우중요) 토마토와 똑같이!!! 시작점 다 넣어줌
for y in range(n):
for x in range(m):
if graph[y][x] == 1:
visit[y][x] = 0
que.append((y,x)) # <-- 토마토처럼 해줘서 max(ans, visit)이 가능했던 것!
# 좌측 3개, 중앙 2개, 우측 3개
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
# (매우중요) graph를 안쓰고 별도의 visit 횟수 count
while que:
y, x = que.popleft()
for nextp in range(8):
ny = y + dy[nextp]
nx = x + dx[nextp]
# 한번도 방문하지 않고, 범위 안에 있는 위치만
if 0<= ny < n and 0 <= nx < m and visit[ny][nx] == -1:
visit[ny][nx] = visit[y][x] + 1
answer = max(answer, visit[ny][nx]) # (매우중요) 바로바로 비교
que.append((ny,nx))
print(answer)