|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | +
|
| 5 | +public class Main { |
| 6 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + private static final int[] dx = {1, 1, 0, 0, -1, -1}; |
| 9 | + private static final int[] dy = {0, -1, 1, -1, 0, 1}; |
| 10 | + private static char[][] board; |
| 11 | + private static int[][] arr; |
| 12 | + private static int N, color; |
| 13 | +
|
| 14 | + public static void main(String[] args) throws IOException { |
| 15 | + init(); |
| 16 | + for (int i = 0; i < N; i++) { |
| 17 | + for (int j = 0; j < N; j++) { |
| 18 | + if (board[i][j] == 'X' && arr[i][j] == 0) { |
| 19 | + color = Math.max(color, 1); |
| 20 | + BFS(i, j); |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | +
|
| 25 | + bw.write(color + "\n"); |
| 26 | + bw.flush(); |
| 27 | + bw.close(); |
| 28 | + br.close(); |
| 29 | + } |
| 30 | +
|
| 31 | + private static void init() throws IOException { |
| 32 | + N = Integer.parseInt(br.readLine()); |
| 33 | + color = 0; |
| 34 | + board = new char[N][N]; |
| 35 | + arr = new int[N][N]; |
| 36 | +
|
| 37 | + for (int i = 0; i < N; i++) { |
| 38 | + board[i] = br.readLine().toCharArray(); |
| 39 | + } |
| 40 | + } |
| 41 | +
|
| 42 | + private static void BFS(int x, int y) { |
| 43 | + Queue<int[]> q = new ArrayDeque<>(); |
| 44 | + arr[x][y] = 1; |
| 45 | + q.add(new int[]{x, y}); |
| 46 | +
|
| 47 | + while (!q.isEmpty()) { |
| 48 | + int[] current = q.poll(); |
| 49 | +
|
| 50 | + for (int i = 0; i < 6; i++) { |
| 51 | + int nx = current[0] + dx[i]; |
| 52 | + int ny = current[1] + dy[i]; |
| 53 | +
|
| 54 | + if (OOB(nx, ny) || board[nx][ny] != 'X') continue; |
| 55 | + if (arr[nx][ny] == 0) { |
| 56 | + color = Math.max(color, 2); |
| 57 | + arr[nx][ny] = 3 - arr[current[0]][current[1]]; |
| 58 | + q.add(new int[]{nx, ny}); |
| 59 | + } else if (arr[nx][ny] == arr[current[0]][current[1]]) { |
| 60 | + color = Math.max(color, 3); |
| 61 | + return; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +
|
| 67 | + private static boolean OOB(int nx, int ny) { |
| 68 | + return nx < 0 || nx > N-1 || ny < 0 || ny > N-1; |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
0 commit comments