Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f8f2072

Browse files
[Gold V] Title: 보물섬, Time: 4260 ms, Memory: 34984 KB -BaekjoonHub
1 parent 16129bc commit f8f2072

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# [Gold V] 보물섬 - 2589
2+
3+
[문제 링크](https://www.acmicpc.net/problem/2589)
4+
5+
### 성능 요약
6+
7+
메모리: 34984 KB, 시간: 4260 ms
8+
9+
### 분류
10+
11+
너비 우선 탐색, 브루트포스 알고리즘, 그래프 이론, 그래프 탐색
12+
13+
### 제출 일자
14+
15+
2025년 4월 11일 22:29:58
16+
17+
### 문제 설명
18+
19+
<p>보물섬 지도를 발견한 후크 선장은 보물을 찾아나섰다. 보물섬 지도는 아래 그림과 같이 직사각형 모양이며 여러 칸으로 나뉘어져 있다. 각 칸은 육지(L)나 바다(W)로 표시되어 있다. 이 지도에서 이동은 상하좌우로 이웃한 육지로만 가능하며, 한 칸 이동하는데 한 시간이 걸린다. 보물은 서로 간에 최단 거리로 이동하는데 있어 가장 긴 시간이 걸리는 육지 두 곳에 나뉘어 묻혀있다. 육지를 나타내는 두 곳 사이를 최단 거리로 이동하려면 같은 곳을 두 번 이상 지나가거나, 멀리 돌아가서는 안 된다.</p>
20+
21+
<p style="text-align: center;"><img alt="" src="https://www.acmicpc.net/upload/images/c1bYIsKpI6m317EAx.jpg" style="width: 238px; height: 144px; "></p>
22+
23+
<p>예를 들어 위와 같이 지도가 주어졌다면 보물은 아래 표시된 두 곳에 묻혀 있게 되고, 이 둘 사이의 최단 거리로 이동하는 시간은 8시간이 된다.</p>
24+
25+
<p style="text-align: center;"><img alt="" src="https://www.acmicpc.net/upload/images/XqDkWCRUWbzZ.jpg" style="width: 238px; height: 144px; "></p>
26+
27+
<p>보물 지도가 주어질 때, 보물이 묻혀 있는 두 곳 간의 최단 거리로 이동하는 시간을 구하는 프로그램을 작성하시오.</p>
28+
29+
### 입력
30+
31+
<p>첫째 줄에는 보물 지도의 세로의 크기와 가로의 크기가 빈칸을 사이에 두고 주어진다. 이어 L과 W로 표시된 보물 지도가 아래의 예와 같이 주어지며, 각 문자 사이에는 빈 칸이 없다. 보물 지도의 가로, 세로의 크기는 각각 50이하이다.</p>
32+
33+
### 출력
34+
35+
<p>첫째 줄에 보물이 묻혀 있는 두 곳 사이를 최단 거리로 이동하는 시간을 출력한다.</p>
36+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# 2589 보물섬
2+
from collections import deque
3+
4+
H, W = map(int, input().split())
5+
M = [list(map(str, input())) for _ in range(H)]
6+
7+
dx = [1, 0, -1, 0]
8+
dy = [0, 1, 0, -1]
9+
10+
def bfs(start_x, start_y):
11+
length = 0
12+
13+
visited = [[False]*W for _ in range(H)]
14+
q = deque([(start_x, start_y, 0)])
15+
visited[start_x][start_y] = True
16+
17+
while q:
18+
x, y, l = q.popleft()
19+
length = max(length, l)
20+
21+
for i in range(4):
22+
mvx, mvy = dx[i], dy[i]
23+
nx, ny = x + mvx, y + mvy
24+
if(nx >= 0 and ny >= 0 and nx < H and ny < W):
25+
if(not visited[nx][ny] and M[nx][ny] == 'L'):
26+
q.append([nx, ny, l+1])
27+
visited[nx][ny] = True
28+
return length
29+
30+
answer = 0
31+
for i in range(H):
32+
for j in range(W):
33+
if(M[i][j] == 'L'):
34+
answer = max(bfs(i, j), answer)
35+
36+
print(answer)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /