from collections import deque
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]
m, n = map(int, input().split())
graph = []
queue = deque([])
for i in range(n):
graph.append(list(map(int, input().split())))
for j in range(m):
if graph[i][j] == 1:
queue.append([i,j])
def bfs():
while queue:
a, b = queue.popleft()
for i in range(4):
x = a + dx[i]
y = b + dy[i]
if 0 <= a <= n and 0 <= b <= m and graph[x][y] == 0:
queue.append([x, y])
graph[x][y] += 1
for i in range(n):
for j in range(m):
if graph[i][j] == 1:
check(i, j)
////////////////////////////////
from collections import deque
mx = [+2, +1, -1, -2, -2, -1, +1, +2] # 반시계방향
my = [+1, +2, +2, +1, -1, -2, -2, -1]
# for i in range(n):
# for j in range(n):
# print(graph[i][j], end=' ')
# print()
def bfs(a, b, x, y):
queue = deque()
queue.append((a, b))
while queue: # queue가 끝날때까지 무한반복
a, b = queue.popleft() # a, b 현재위치 제거
if a==x and b==y: # 종료지점 도착시 종료
print(graph[a][b])
return True
for i in range(8): # 나이트 8개 방향
nx = a + mx[i] # 1~8 번 x방면
ny = b + my[i] # 1~8 번 y방면
if nx<0 or ny<0 or nx>=n or nx>=n:
continue
if graph[nx][ny]==0:
graph[nx][ny]=graph[a][b]+1
queue.append((nx, ny)) #
return False
n= int(input())
graph = []
for i in range(n):
graph.append([0] * n)
for i in range(n):
for j in range(n):
print(graph[i][j], end=' ')
print()
for i in range(n):
# queue = deque()
# graph = [[0]*n for _ in range(n)]
a, b = map(int, input().split()) # 시작위치 설정
x, y = map(int, input().split()) # 종료위치 설정
# graph[a][b] = 1
# queue.append((a, b)) # queue에 a, b 위치 추가
graph[a][b] = 1
bfs(a, b, x, y)
for i in range(n):
for j in range(n):
print(graph[i][j], end=' ')
print()
top of page
실제 작동 상태를 확인하려면 라이브 사이트로 이동하세요.
220203
220203
댓글 0개
좋아요
댓글(0)
bottom of page