-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ2589_보물섬.cpp
More file actions
85 lines (76 loc) · 1.83 KB
/
BOJ2589_보물섬.cpp
File metadata and controls
85 lines (76 loc) · 1.83 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// main.cpp
// BOJ2589_보물섬
//
// Created by 신지식 on 14/11/2018.
// Copyright © 2018 Shin Ji Sik. All rights reserved.
//
#include <iostream>
#include <string>
#include <queue>
using namespace std;
int w, h, ans = 0;
char map[51][51];
bool check[51][51];
int dist[51][51];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
void bfs(int a, int b){
queue<pair<int, int>> q;
q.push({a, b});
dist[a][b] = 0;
while(!q.empty()){
int x = q.front().first;
int y = q.front().second;
q.pop();
for(int i = 0; i < 4; i++){
int nx = dx[i] + x;
int ny = dy[i] + y;
if(nx < 0 || ny < 0 || nx >= h || ny >= w) continue;
if(check[nx][ny] == false && map[nx][ny] != 'W'){
check[nx][ny] = true;
if(dist[nx][ny] < dist[x][y] + 1)
dist[nx][ny] = dist[x][y] + 1;
q.push({nx, ny});
}
}
}
int sum = 0;
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
if(dist[i][j] > sum)
sum = dist[i][j];
}
}
if(sum > ans)
ans = sum;
}
void init(){
for(int i = 0; i <= 50; i++){
for(int j = 0; j <= 50; j++){
check[i][j] = false;
dist[i][j] = 0;
}
}
}
int main(int argc, const char * argv[]) {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> h >> w;
string s;
for(int i = 0; i < h; i++){
cin >> s;
for(int j = 0; j < w; j++){
map[i][j] = s[j];
}
}
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
if(map[i][j] == 'L'){
bfs(i, j);
init();
}
}
}
cout << ans << "\n";
}