-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2589(BFS).java
More file actions
94 lines (79 loc) · 2.41 KB
/
2589(BFS).java
File metadata and controls
94 lines (79 loc) · 2.41 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
86
87
88
89
90
91
92
93
94
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static char map[][];
static int R, C;
static int temp[][];
static int dx[] = {0, 0, 1, -1};
static int dy[] = {1, -1, 0, 0};
public static void bfs(int x, int y) {
boolean visited[][] = new boolean[R][C];
Queue<Tuple> queue = new LinkedList<>();
visited[x][y] = true;
queue.add(new Tuple(x, y, 0));
while (!queue.isEmpty()) {
Tuple current = queue.remove();
int currentX = current.x;
int currentY = current.y;
int count = current.count;
if(temp[currentX][currentY] < count){
temp[currentX][currentY] = count;
}
for(int i = 0; i < 4; i++) {
int nextX = currentX + dx[i];
int nextY = currentY + dy[i];
if(nextX < 0 || nextY < 0 || nextX >= R || nextY >= C){
continue;
}
if(map[nextX][nextY] == 'W') {
continue;
}
if(!visited[nextX][nextY]){
visited[nextX][nextY] = true;
queue.add(new Tuple(nextX, nextY, count + 1));
}
}
}
}
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
map = new char[R][C];
temp = new int[R][C];
for(int i = 0; i < R; i++) {
String s = br.readLine();
for(int j = 0; j < C; j++) {
map[i][j] = s.charAt(j);
}
}
int max = 0;
for(int i = 0; i < R; i++) {
for(int j = 0; j < C; j++){
if(map[i][j] == 'L'){
bfs(i, j);
}
}
}
for(int i = 0; i < R; i++) {
for(int j = 0; j < C; j++){
max = Math.max(max, temp[i][j]);
}
}
System.out.println(max);
}
}
class Tuple{
int x;
int y;
int count;
Tuple(int x, int y, int c){
this.x = x;
this.y = y;
count = c;
}
}