boj)2589 - 보물섬
2020. 11. 11. 12:18ㆍPS/boj
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
|
import java.io.*;
import java.util.*;
public class boj_2589 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static int row, col, nx, ny, ans;
static char[][] map;
static int[][] max;
static boolean[][] v;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};
public static void main(String[] args) throws IOException {
input();
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (map[i][j] == 'L') {
bfs(i, j);
}
}
}
System.out.println(ans);
}
static void input() throws IOException {
st = new StringTokenizer(br.readLine());
row = Integer.parseInt(st.nextToken());
col = Integer.parseInt(st.nextToken());
map = new char[row][col];
max = new int[row][col];
for (int i = 0; i < row; i++) {
String str = br.readLine();
for (int j = 0; j < col; j++) {
map[i][j] = str.charAt(j);
}
}
}
static void bfs(int x, int y) {
v = new boolean[row][col];
Queue<int[]> q = new LinkedList<>();
int distance = 0;
v[x][y] = true;
q.offer(new int[] {x, y, distance});
while (!q.isEmpty()) {
int[] now = q.poll();
for (int i = 0; i < 4; i++) {
nx = now[0] + dx[i];
ny = now[1] + dy[i];
distance = now[2];
if (nx < 0 || nx >= row || ny < 0 || ny >= col) continue;
if (map[nx][ny] == 'L' && !v[nx][ny]) {
v[nx][ny] = true;
q.offer(new int[] {nx, ny, distance + 1});
}
}
}
if (distance > ans) ans = distance;
}
}
|
cs |
- 전체 지도를 돌면서 L 마다 bfs 실행
- 결국 전체 지도로 봤을때 최대 거리가 답이니까 distance를 측정하고 최댓값을 출력
'PS > boj' 카테고리의 다른 글
boj)1074 - Z (0) | 2020.11.12 |
---|---|
boj)11729 - 하노이 탑 이동 순서 (0) | 2020.11.11 |
boj)2941 - 크로아티아 알파벳 (0) | 2020.11.05 |
boj)1316 - 그룹 단어 체커 (0) | 2020.11.05 |
boj)11719 - 그대로 출력하기 2 (0) | 2020.11.05 |