|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +public class Main { |
| 5 | + |
| 6 | + public static int N; |
| 7 | + public static int[][] map; |
| 8 | + public static int[][] visited; |
| 9 | + |
| 10 | + public static void main(String[] args) throws IOException { |
| 11 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + N = Integer.parseInt(br.readLine()); |
| 13 | + map = new int[N+1][N+1]; |
| 14 | + int max = Integer.MIN_VALUE; |
| 15 | + for(int i=0; i<N; i++) { |
| 16 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 17 | + for(int j=0; j<N; j++) { |
| 18 | + map[i][j] = Integer.parseInt(st.nextToken()); |
| 19 | + max = Math.max(max, map[i][j]); |
| 20 | + } |
| 21 | + } |
| 22 | + int answer = 1; |
| 23 | + for(int k=1; k<=max; k++) { |
| 24 | + visited = new int[N+1][N+1]; |
| 25 | + int count = 0; |
| 26 | + for(int i=0; i<N; i++) { |
| 27 | + for(int j=0; j<N; j++) { |
| 28 | + if(visited[i][j] == 0 && map[i][j] > k) { |
| 29 | + count++; |
| 30 | + bfs(i, j, k, count); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + answer = Math.max(answer, count); |
| 35 | + } |
| 36 | + System.out.println(answer); |
| 37 | + } |
| 38 | + |
| 39 | + public static void bfs(int h, int w, int depth, int count) { |
| 40 | + int[] dh = {-1, 0, 1, 0}; |
| 41 | + int[] dw = {0, 1, 0, -1}; |
| 42 | + Queue<int[]> q = new LinkedList<>(); |
| 43 | + q.offer(new int[]{h, w}); |
| 44 | + visited[h][w] = 1; |
| 45 | + while(!q.isEmpty()) { |
| 46 | + h = q.peek()[0]; |
| 47 | + w = q.peek()[1]; |
| 48 | + q.poll(); |
| 49 | + |
| 50 | + for(int i=0; i<4; i++) { |
| 51 | + int nextH = h + dh[i]; |
| 52 | + int nextW = w + dw[i]; |
| 53 | + if(nextH >= 0 && nextH < N && nextW >= 0 && nextW < N) { |
| 54 | + if(visited[nextH][nextW] == 0 && map[nextH][nextW] > depth) { |
| 55 | + visited[nextH][nextW] = count; |
| 56 | + q.offer(new int[]{nextH, nextW}); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments