[백준] 13460 :구슬탈출2

JIN·2022년 1월 10일
0

구슬탈출2

구슬탈출
이 문제와 동일한데 구슬탈출 문제는 마지막에 1을 리턴했다면 이 문제는 갯수를 리턴해주기만 하면 되고 , 0 대신 -1 으로 바꾸면 됩니다.

import sys
from collections import deque
input = sys.stdin.readline
n, m = map(int, input().split())
graph = []
for i in range(n):
	graph.append(list(sys.stdin.readline().rstrip()))
dx =[-1, 1, 0, 0]
dy = [0, 0, -1, 1]
def bfs(rx, ry, bx, by, cnt):
	cnt = 0
	visited[rx][ry][bx][by] = True
	while q:
		for _ in range(len(q)):
			rx, ry, bx, by, cnt = q.popleft()
			if cnt > 10:
				return -1
			if graph[rx][ry] =='O':
				return cnt

			for i in range(4):
				rnx = rx
				rny = ry
				bnx = bx
				bny = by
				while True:
					rnx += dx[i]
					rny += dy[i]
					if graph[rnx][rny]=='#':
						rnx -= dx[i]
						rny -= dy[i]
						break
					if graph[rnx][rny] == 'O':
						break
				while True:
					bnx += dx[i]
					bny += dy[i]
					if graph[bnx][bny]=='#':
						bnx -= dx[i]
						bny -= dy[i]
						break
					if graph[bnx][bny] == 'O':
						break
				if graph[bnx][bny] == 'O':
					continue
				if rnx == bnx and rny == bny:
					if abs(rnx - rx) + abs(rny - ry) > abs(bnx - bx) + abs(bny - by):
						rnx -= dx[i]
						rny -= dy[i]
					else:
						bnx -= dx[i]
						bny -= dy[i]
				if not visited[rnx][rny][bnx][bny]:
					visited[rnx][rny][bnx][bny] = True
					q.append((rnx, rny, bnx, bny, cnt+1))

	return -1
red = []
blue = []
for i in range(n):
	for j in range(m):
		if graph[i][j] == 'R':
			red.append((i, j))
		if graph[i][j] == 'B':
			blue.append((i, j))
q = deque()
q.append((red[0][0], red[0][1], blue[0][0], blue[0][1], 0))
visited = [[[[False for _ in range(m)]for _ in range(n)]for _ in range(m)]for _ in range(n)]
print(bfs(red[0][0], red[0][1], blue[0][0], blue[0][1], 0))
profile
배우고 적용하고 개선하기

0개의 댓글