실패 코드
import sys
from collections import deque
N, M = map(int, sys.stdin.readline().split())
maps = []
R = []
B = []
for i in range(N):
map_add = list(map(str, sys.stdin.readline().strip()))
maps.append(map_add)
if 'R' in map_add or 'B' in map_add:
for j, word in enumerate(map_add):
if word == 'R':
R = [i, j]
if word == 'B':
B = [i, j]
# 구슬의 이동
def moves(x, y, dx, dy, gole_check):
nx, ny = x, y
i = 1
while True:
cnx, cny = x + (i * dx), y + (i * dy)
# 구슬이 맵에서 벗어 나는지와 벽또는 앞의 구슬에 막혔는지 확인
if cnx < 0 or cnx >= N or cny < 0 or cny >= M:
break
elif maps[cnx][cny] in ['#', 'R', 'B']:
break
else:
nx, ny = cnx, cny
# 구슬이 출구를 통과 했는지 확인
if maps[nx][ny] == 'O':
if maps[x][y] == 'R':
gole_check[0] = True
else:
gole_check[1] = True
i += 1
return nx, ny, gole_check
def bfs(R, B):
# 구슬이 출구에 도달 했는 확인 하는 함수
is_gols = [False, False]
dq = deque([0, R, B])
# 구슬의 초기 위치 기록
x1, y1 = R
x2, y2 = B
dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1]
while dq:
count = dq.popleft()
# 기울기의 획수가 10이상면 실패로 종료
if count > 10:
is_gols = [False, False]
return
# 구슬이 이동하기에 맵에 그려진 구슬을 일단 자리 비움
maps[x1][y1] = '.'
maps[x2][y2] = '.'
rx, ry = dq.popleft()
bx, by = dq.popleft()
# 구슬이 받아 변수 값 부터 이동 하기에 초기 위기 셋팅
maps[rx][ry] = 'R'
maps[bx][by] = 'B'
# if count == 6:
# print(count)
# print('\n'.join(map(str, maps)))
for i in range(4):
# 구슬이 출구로 나가지 않은 상태로 가정하기 구슬의 상태 초기화
is_gols = [False, False]
# 어떤 구슬부터 움직일지 확인
if (i == 0 and rx < bx) or (i == 1 and rx > bx) or (i == 2 and ry < by) or (i == 3 and ry > by):
nrx, nry, is_gols = moves(rx, ry, dx[i], dy[i], is_gols)
# 구슬이 동일 한 위치 이면 일단 먼저 해당 구슬 위치 이동
if ry == by or rx == bx:
maps[rx][ry], maps[nrx][nry] = maps[nrx][nry], maps[rx][ry]
nbx, nby, is_gols = moves(bx, by, dx[i], dy[i], is_gols)
# 이동 해두었던 위치 초기화
if ry == by or rx == bx:
maps[rx][ry], maps[nrx][nry] = maps[nrx][nry], maps[rx][ry]
if is_gols[1]: continue
else:
nbx, nby, is_gols = moves(bx, by, dx[i], dy[i], is_gols)
if is_gols[1]: continue
# 구슬이 동일 한 위치 이면 일단 먼저 해당 구슬 위치 이동
if ry == by or rx == bx:
maps[bx][by], maps[nbx][nby] = maps[nbx][nby], maps[bx][by]
nrx, nry, is_gols = moves(rx, ry, dx[i], dy[i], is_gols)
# 이동 해두었던 위치 초기화
if ry == by or rx == bx:
maps[bx][by], maps[nbx][nby] = maps[nbx][nby], maps[bx][by]
# 빨강 구슬만 출구를 이동했다면 함수 종료
if is_gols == [True, False]:
return is_gols
# 파랑 구슬이 출구를 밟았다면 이후 검색은 필요없기에 다음 작업 수행
if is_gols[1]:
continue
# 구슬이 이동이 되었다면 큐에 기록
if rx != nrx or ry != nry or bx != nbx or by != nby:
dq.append(count + 1)
dq.append([nrx, nry])
dq.append([nbx, nby])
# 기존의 위치를 초기화 한 뒤 최초의 상태로 구슬 위치 변경
maps[rx][ry] = '.'
maps[bx][by] = '.'
maps[x1][y1] = 'R'
maps[x2][y2] = 'B'
return is_gols
check = bfs(R, B)
if check == [True, False]:
sys.stdout.write(f'{1}')
else:
sys.stdout.write(f'{0}')