|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + |
| 7 | + static int N, M; |
| 8 | + static int[][] map; |
| 9 | + static int res = 0; |
| 10 | + static int[] dy = {-1, 1, 0, 0}; |
| 11 | + static int[] dx = {0, 0, -1, 1}; |
| 12 | + |
| 13 | + public static void main(String[] args) throws IOException { |
| 14 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 15 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 16 | + |
| 17 | + N = Integer.parseInt(st.nextToken()); |
| 18 | + M = Integer.parseInt(st.nextToken()); |
| 19 | + |
| 20 | + map = new int[N][M]; |
| 21 | + |
| 22 | + for (int i = 0; i < N; i++) { |
| 23 | + String s = br.readLine(); |
| 24 | + for (int j = 0; j < M; j++) { |
| 25 | + char c = s.charAt(j); |
| 26 | + switch (c) { |
| 27 | + case 'W': { |
| 28 | + map[i][j] = 0; |
| 29 | + break; |
| 30 | + } |
| 31 | + case 'L': { |
| 32 | + map[i][j] = 1; |
| 33 | + break; |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + for (int i = 0; i < N; i++) { |
| 40 | + for (int j = 0; j < M; j++) { |
| 41 | + if (map[i][j] == 1) { |
| 42 | + bfs(i, j); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + System.out.println(res); |
| 48 | + } |
| 49 | + |
| 50 | + static void bfs(int y, int x) { |
| 51 | + boolean[][] visited = new boolean[N][M]; |
| 52 | + Queue<int[]> q = new ArrayDeque<>(); |
| 53 | + visited[y][x] = true; |
| 54 | + q.add(new int[]{y, x}); |
| 55 | + int temp = 0; |
| 56 | + |
| 57 | + while (!q.isEmpty()) { |
| 58 | + temp++; |
| 59 | + int s = q.size(); |
| 60 | + for (int i = 0; i < s; i++) { |
| 61 | + int[] cur = q.poll(); |
| 62 | + for (int d = 0; d < 4; d++) { |
| 63 | + int ny = cur[0] + dy[d]; |
| 64 | + int nx = cur[1] + dx[d]; |
| 65 | + if (ny < 0 || nx < 0 || ny >= N || nx >= M || visited[ny][nx] || map[ny][nx] == 0) |
| 66 | + continue; |
| 67 | + visited[ny][nx] = true; |
| 68 | + q.add(new int[]{ny, nx}); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + res = Math.max(res, temp - 1); |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
0 commit comments