|
| 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 | +
|
| 9 | + private static final int[] dx_even = {-1, -1, 0, 0, 1, 1}; |
| 10 | + private static final int[] dy_even = {-1, 0, -1, 1, -1, 0}; |
| 11 | +
|
| 12 | + private static final int[] dx_odd = {-1, -1, 0, 0, 1, 1}; |
| 13 | + private static final int[] dy_odd = {0, 1, -1, 1, 0, 1}; |
| 14 | +
|
| 15 | + private static int[][] board; |
| 16 | + private static int W, H, answer; |
| 17 | +
|
| 18 | + public static void main(String[] args) throws IOException { |
| 19 | + init(); |
| 20 | +
|
| 21 | + markOutside(); |
| 22 | +
|
| 23 | + for (int i = 1; i <= H; i++) { |
| 24 | + for (int j = 1; j <= W; j++) { |
| 25 | + if (board[i][j] == 0) { |
| 26 | + board[i][j] = 1; |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | +
|
| 31 | + for (int i = 1; i <= H; i++) { |
| 32 | + for (int j = 1; j <= W; j++) { |
| 33 | + if (board[i][j] == 1) { |
| 34 | + answer += countWalls(i, j); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | +
|
| 39 | + bw.write(answer + "\n"); |
| 40 | + bw.flush(); |
| 41 | + bw.close(); |
| 42 | + br.close(); |
| 43 | + } |
| 44 | +
|
| 45 | + private static void init() throws IOException { |
| 46 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 47 | + W = Integer.parseInt(st.nextToken()); |
| 48 | + H = Integer.parseInt(st.nextToken()); |
| 49 | +
|
| 50 | + board = new int[H+2][W+2]; |
| 51 | +
|
| 52 | + for (int i = 1; i <= H; i++) { |
| 53 | + st = new StringTokenizer(br.readLine()); |
| 54 | + for (int j = 1; j <= W; j++) { |
| 55 | + board[i][j] = Integer.parseInt(st.nextToken()); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | +
|
| 60 | + private static void markOutside() { |
| 61 | + Queue<int[]> q = new ArrayDeque<>(); |
| 62 | + board[0][0] = -1; |
| 63 | + q.add(new int[]{0, 0}); |
| 64 | +
|
| 65 | + while (!q.isEmpty()) { |
| 66 | + int[] current = q.poll(); |
| 67 | + int x = current[0]; |
| 68 | + int y = current[1]; |
| 69 | +
|
| 70 | + int[] dx = (x % 2 == 0) ? dx_even : dx_odd; |
| 71 | + int[] dy = (x % 2 == 0) ? dy_even : dy_odd; |
| 72 | +
|
| 73 | + for (int i = 0; i < 6; i++) { |
| 74 | + int nx = x + dx[i]; |
| 75 | + int ny = y + dy[i]; |
| 76 | +
|
| 77 | + if (nx < 0 || nx >= H+2 || ny < 0 || ny >= W+2) continue; |
| 78 | + if (board[nx][ny] != 0) continue; |
| 79 | +
|
| 80 | + board[nx][ny] = -1; |
| 81 | + q.add(new int[]{nx, ny}); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +
|
| 86 | + private static int countWalls(int x, int y) { |
| 87 | + int walls = 0; |
| 88 | +
|
| 89 | + int[] dx = (x % 2 == 0) ? dx_even : dx_odd; |
| 90 | + int[] dy = (x % 2 == 0) ? dy_even : dy_odd; |
| 91 | +
|
| 92 | + for (int i = 0; i < 6; i++) { |
| 93 | + int nx = x + dx[i]; |
| 94 | + int ny = y + dy[i]; |
| 95 | +
|
| 96 | + if (nx < 0 || nx >= H+2 || ny < 0 || ny >= W+2 || board[nx][ny] == -1) { |
| 97 | + walls++; |
| 98 | + } |
| 99 | + } |
| 100 | +
|
| 101 | + return walls; |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
0 commit comments