|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +public class Main{ |
| 6 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + static StringTokenizer st; |
| 9 | + static int N,M; |
| 10 | + static char[][] map; |
| 11 | + static List<int[]> lands; |
| 12 | + static int[] di = {0,0,-1,1}; |
| 13 | + static int[] dj = {-1,1,0,0}; |
| 14 | + static int ans = 0; |
| 15 | + public static void main(String[] args) throws Exception { |
| 16 | + st = new StringTokenizer(br.readLine()); |
| 17 | + N = Integer.parseInt(st.nextToken()); |
| 18 | + M = Integer.parseInt(st.nextToken()); |
| 19 | + map = new char[N][M]; |
| 20 | + lands = new LinkedList<>(); |
| 21 | + |
| 22 | + for (int i = 0; i < N; i++) { |
| 23 | + String line = br.readLine(); |
| 24 | + for (int j = 0; j < M; j++) { |
| 25 | + map[i][j] = line.charAt(j); |
| 26 | + if(map[i][j] == 'L'){ |
| 27 | + lands.add(new int[]{i,j}); |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + for (int[] cur : lands) { |
| 34 | + BFS(cur[0],cur[1]); |
| 35 | + } |
| 36 | + bw.write(ans+""); |
| 37 | + bw.close(); |
| 38 | + } |
| 39 | + static void BFS(int starti, int startj){ |
| 40 | + ArrayDeque<int[]> q = new ArrayDeque<>(); |
| 41 | + boolean[][] visited = new boolean[N][M]; |
| 42 | + q.add(new int[]{starti,startj,0}); |
| 43 | + visited[starti][startj] = true; |
| 44 | + |
| 45 | + while(!q.isEmpty()){ |
| 46 | + int[] cur = q.poll(); |
| 47 | + ans = Math.max(ans,cur[2]); |
| 48 | + |
| 49 | + for (int k = 0; k < 4; k++) { |
| 50 | + int ni = cur[0] + di[k]; |
| 51 | + int nj = cur[1] + dj[k]; |
| 52 | + |
| 53 | + if(ni<0 || ni>=N || nj<0 || nj>=M || visited[ni][nj]) continue; |
| 54 | + |
| 55 | + if(map[ni][nj] == 'L'){ |
| 56 | + q.add(new int[]{ni,nj,cur[2]+1}); |
| 57 | + visited[ni][nj] = true; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
0 commit comments