-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2589.py
More file actions
50 lines (38 loc) · 1.12 KB
/
2589.py
File metadata and controls
50 lines (38 loc) · 1.12 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
from collections import deque
dy = [-1,0,1,0]
dx = [0,1,0,-1]
n, m = map(int, input().split())
arr = [['0'] * m for _ in range(n)]
visited = [[-1] * m for _ in range(n)]
land_data = []
for i in range(n) :
data = input()
for j in range(m) :
arr[i][j] = data[j]
if arr[i][j] == 'L' :
land_data.append((i, j)) #땅 위치 저장
q = deque()
ret = 0
def bfs(y,x) :
q.append((y,x))
visited[y][x] = 0
bfs_ret = 0
while q :
qy,qx = q.popleft()
for i in range(4) :
ny = dy[i] + qy
nx = dx[i] + qx
if ny < 0 or ny >= n or nx < 0 or nx >= m or visited[ny][nx] >= 0 :
continue
if arr[ny][nx] == 'W' : #물이면
continue
if arr[ny][nx] == 'L' :
q.append((ny,nx))
visited[ny][nx] = visited[qy][qx] + 1 #이동시 업데이트
bfs_ret = max(bfs_ret, visited[ny][nx])
return bfs_ret
for y,x in land_data :
visited = [[-1] * m for _ in range(n)]
#bfs 다른 L과의 최대 거리 저장
ret = max(ret,bfs(y,x))
print(ret)