Post

🐣 보물섬

1. 문제 링크

2589번: 보물섬


2. 코드

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
from collections import deque

n,m=map(int, input().split())
maps=[]
for i in range(n):
    maps.append(list(input()))

dx=[1,-1,0,0]
dy=[0,0,1,-1]

def bfs(i,j):
    queue=deque()
    queue.append((i,j))
    visited=[[0]*m for _ in range(n)]
    visited[i][j]=1
    cnt=0
    while queue:
        x,y=queue.popleft()
        for i in range(4):
            nx=x+dx[i]
            ny=y+dy[i]
            if nx<0 or nx>=n or ny<0 or ny>=m:
                continue
            elif maps[nx][ny]=='L' and visited[nx][ny]==0:
                visited[nx][ny]=visited[x][y]+1
                cnt=max(cnt,visited[nx][ny])
                queue.append((nx,ny))
    return cnt-1

result=0
for i in range(n):
    for j in range(m):
        if maps[i][j]=='L':
            result=max(result,bfs(i,j))

print(result)


3. 해설

This post is licensed under CC BY 4.0 by the author.