-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13460 copy.py
More file actions
61 lines (54 loc) · 1.59 KB
/
13460 copy.py
File metadata and controls
61 lines (54 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from sys import stdin
from collections import deque
input = stdin.readline
n, m = map(int,input().split())
board = [list(input().strip()) for _ in range(n)]
q = deque()
visited_r = [[0]*m for _ in range(n)]
visited_b = [[0]*m for _ in range(n)]
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
def init():
rx,ry,bx,by = [0]*4
for i in range(n):
for j in range(m):
if board[i][j] == 'R':
rx, ry = i,j
elif board[i][j] == 'B':
bx, by = i,j
q.append((rx,ry,bx,by,1))
visited_r[rx][ry] = 1
visited_b[bx][by] = 1
def move(x,y,dx,dy):
count = 0
while board[x+dx][y+dy] != '#' and board[x][y] != 'O':
x += dx
y += dy
count += 1
return x,y,count
def bfs():
init()
while q:
rx,ry,bx,by,depth = q.popleft()
if depth > 10:
break
for i in range(4): #4방향 돌리기
nrx, nry, count_r = move(rx, ry, dx[i], dy[i])
nbx, nby, count_b = move(bx, by, dx[i], dy[i])
if board[nbx][nby] == 'O':
continue
if board[nrx][nry] == 'O':
return print(depth)
if nrx == nbx and nry == nby:
if count_r>count_b:
nrx -= dx[i]
nry -= dy[i]
else:
nbx -= dx[i]
nby -= dy[i]
if not visited_r[nrx][nry] or not visited_b[nbx][nby]:
q.append((nrx,nry,nbx,nby,depth+1))
visited_r[nrx][nry] = 1
visited_b[nbx][nby] = 1
return print(-1)
bfs()