-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2589.java
70 lines (53 loc) · 1.82 KB
/
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
69
70
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static class Point {
int x,y;
Point(int x, int y) {this.x=x; this.y=y;}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int h = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());
int[][] map = new int[h][w];
ArrayList<Point> lands = new ArrayList<>();
for(int i=0; i<h; i++) {
String str = br.readLine();
for(int j=0; j<w; j++) {
if(str.charAt(j) == 'L') {
map[i][j] = 1;
lands.add(new Point(i, j));
}
}
}
for(Point p : lands) {
bfs(map, p);
}
System.out.println(answer);
}
static int answer = Integer.MIN_VALUE;
static int[] dx = {1, 0, -1, 0};
static int[] dy = {0, 1, 0, -1};
static void bfs(int[][] map, Point start) {
int[][] cost = new int[map.length][map[0].length];
Queue<Point> q = new LinkedList<>();
q.add(start);
cost[start.x][start.y] = 1;
while(!q.isEmpty()) {
Point p = q.remove();
answer = Integer.max(answer, cost[p.x][p.y] - 1);
for(int i=0; i<4; i++) {
int x = p.x + dx[i];
int y = p.y + dy[i];
if(x<0 || x>=map.length || y<0 || y>=map[0].length)
continue;
if(cost[x][y] == 0 && map[x][y] == 1) {
cost[x][y] = cost[p.x][p.y] + 1;
q.add(new Point(x, y));
}
}
}
}
}