-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain_2589_보물섬.java
68 lines (63 loc) · 1.55 KB
/
Main_2589_보물섬.java
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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static char[][] map;
static int ans,n,m;
static int[] dx = { 0, 0, -1, 1 };
static int[] dy = { 1, -1, 0, 0 };
static class Node {
int x;
int y;
int dist;
Node(int x, int y, int dist) {
this.x = x;
this.y = y;
this.dist = dist;
}
}
static void bfs(boolean[][] check, int x, int y) {
Queue<Node> q = new LinkedList<Node>();
check[x][y] = true;
q.add(new Node(x,y,0));
while(!q.isEmpty()) {
Node now = q.poll();
ans = Math.max(ans, now.dist);
for (int i = 0; i < 4; i++) {
int nx = now.x + dx[i];
int ny = now.y + dy[i];
if(nx < 0 || nx >= n || ny < 0 || ny >= m )
continue;
if(map[nx][ny] == 'W' || check[nx][ny])
continue;
check[nx][ny] = true;
q.add(new Node(nx, ny, now.dist+1));
}
}
}
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
map = new char[n][m];
for (int i = 0; i < n; i++) {
map[i] = br.readLine().toCharArray();
}
boolean[][] check;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (map[i][j] == 'L') {
check = new boolean[n][m];
bfs(check, i, j);
}
}
}
System.out.println(ans);
} catch (Exception e) {
}
}
}