알고리즘/완전탐색(BFS,DFS)
BOJ -2589 ) 보물섬
개발자가될수있을까?
2020. 1. 17. 15:02
입력받은 배열의 모든 값을 확인해주면서 W가 아닌 부분에서의
갈수있는 모든 경로를 BFS를 통하여 구해준다.
가장 큰 길이를 가진 두점이 정답이 된다.
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
|
#include <iostream>
#include <queue>
using namespace std;
char map[51][51];
bool visit[51][51];
int a, b;
void input() {
cin >> a >> b;
for (int y = 0; y < a; y++) {
cin >> map[y];
}
}
int dx[] = { 0,0,+1,-1 };
int dy[] = { +1,-1,0,0 };
typedef struct {
int x, y, time;
}coordi;
int ans = -1;
int main(void) {
cin.tie(0); ios_base::sync_with_stdio(NULL);
input();
for (int y = 0; y < a; y++) {
for (int x = 0; x < b; x++) {
int res = 0;
if (map[y][x] == 'W') continue;
else if(map[y][x]== 'L'){
queue < coordi > Q;
coordi start = { x ,y,0 };
Q.push(start);
while (!Q.empty()) {
int x = Q.front().x;
int y = Q.front().y;
int time = Q.front().time;
Q.pop();
res > time ? res = res : res = time;
for (int index = 0; index < 4; index++) {
int nx = x + dx[index];
int ny = y + dy[index];
if (nx >= 0 && ny >= 0 && nx < b && ny < a) {
if (!visit[ny][nx] && map[ny][nx] =='L') {
visit[ny][nx] = true;
Q.push({ nx,ny,time + 1 });
}
}
}
}
}
ans < res ? ans = res : ans = ans;
for (int y = 0; y < a; y++) {
for (int x = 0; x < b; x++) {
visit[y][x] = false;
}
}
}
}
cout << ans;
return 0;
}
|