개발자 톡
#4, #12 오답
- 등록일
- 2024-06-20 15:27:17
- 조회수
- 251
- 작성자
- kmlim0893
import sys
N,M = map(int,input().split())
arr = [list(map(str,input().split())) for _ in range(N)]
# 0 빈칸, 1 벽, 2 출구, 3 남우, 4 유령
new_arr = [[0] * M for _ in range(N)]
ghost = []
for i in range(N):
for j in range(M):
if arr[i][0][j] == 'D':
ei,ej = i,j
new_arr[i][j] = 2
elif arr[i][0][j] == 'N':
si,sj = i,j
new_arr[i][j] = 3
elif arr[i][0][j] == 'G':
ghost.append((i,j))
new_arr[i][j] = 4
elif arr[i][0][j] == '#':
new_arr[i][j] = 1
else:
new_arr[i][j] = 0
arr = new_arr
from collections import deque
def bfs(si,sj,ghost):
q = deque()
q.append((si,sj))
v = [[False] * M for _ in range(N)]
v[si][sj] = True
gv = [[False] * M for _ in range(N)]
nq = deque()
for gi,gj in ghost:
gv[gi][gj] = True
nq.append((gi,gj))
while q and nq:
ci,cj = q.popleft()
gi,gj = nq.popleft()
if (ci,cj) == (ei,ej):
return True
for ni,nj in ((ci-1,cj),(ci,cj+1),(ci+1,cj),(ci,cj-1)):
if 0<=ni<N and 0<=nj<M and not v[ni][nj] and not arr[ni][nj] == 1 and not arr[ni][nj] == 4:
q.append((ni,nj))
v[ni][nj] = True
arr[ci][cj] = 0
arr[ni][nj] = 3
si,sj = ni,nj
dist = (gi-si)**2 + (gj-sj)**2
for ni,nj in ((gi-1,gj),(gi,gj+1),(gi+1,gj),(gi,gj-1)):
if 0<=ni<N and 0<=nj<M and not gv[ni][nj] and dist > (ni-si)**2+(nj-sj)**2:
gv[ni][nj] = True
arr[gi][gj] = 0
arr[ni][nj] = 4
nq.append((ni,nj))
return False
if bfs(si,sj,ghost): print('Yes')
else: print('No')
어느부분에서 부족한걸까요 ..?