|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.util.StringTokenizer; |
| 5 | + |
| 6 | +public class Main { |
| 7 | + |
| 8 | + static int n, answer; |
| 9 | + static int[][] map; |
| 10 | + |
| 11 | + public static void main(String[] args) throws Exception { |
| 12 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 13 | + n = Integer.parseInt(br.readLine()); |
| 14 | + answer = 0; |
| 15 | + map = new int[n][n]; |
| 16 | + |
| 17 | + for (int i = 0; i < n; i++) { |
| 18 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 19 | + for (int j = 0; j < n; j++) { |
| 20 | + map[i][j] = Integer.parseInt(st.nextToken()); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + game(0); |
| 25 | + System.out.println(answer); |
| 26 | + } |
| 27 | + |
| 28 | + public static void game(int count) { |
| 29 | + if (count == 10) { |
| 30 | + findMax(); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + int currentMax = getCurrentMax(); |
| 35 | + if (currentMax * (1 << (10 - count)) <= answer) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + int[][] backup = new int[n][n]; |
| 40 | + for (int i = 0; i < n; i++) { |
| 41 | + backup[i] = map[i].clone(); |
| 42 | + } |
| 43 | + |
| 44 | + for (int dir = 0; dir < 4; dir++) { |
| 45 | + move(dir); |
| 46 | + game(count + 1); |
| 47 | + |
| 48 | + for (int i = 0; i < n; i++) { |
| 49 | + map[i] = backup[i].clone(); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public static void move(int dir) { |
| 55 | + if (dir == 0) { |
| 56 | + for (int j = 0; j < n; j++) { |
| 57 | + int index = 0; |
| 58 | + int prev = 0; |
| 59 | + for (int i = 0; i < n; i++) { |
| 60 | + if (map[i][j] != 0) { |
| 61 | + if (prev == map[i][j]) { |
| 62 | + map[index - 1][j] = prev * 2; |
| 63 | + prev = 0; |
| 64 | + } else { |
| 65 | + prev = map[i][j]; |
| 66 | + map[index][j] = prev; |
| 67 | + index++; |
| 68 | + } |
| 69 | + if (i != index - 1) map[i][j] = 0; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } else if (dir == 1) { |
| 74 | + for (int j = 0; j < n; j++) { |
| 75 | + int index = n - 1; |
| 76 | + int prev = 0; |
| 77 | + for (int i = n - 1; i >= 0; i--) { |
| 78 | + if (map[i][j] != 0) { |
| 79 | + if (prev == map[i][j]) { |
| 80 | + map[index + 1][j] = prev * 2; |
| 81 | + prev = 0; |
| 82 | + } else { |
| 83 | + prev = map[i][j]; |
| 84 | + map[index][j] = prev; |
| 85 | + index--; |
| 86 | + } |
| 87 | + if (i != index + 1) map[i][j] = 0; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } else if (dir == 2) { |
| 92 | + for (int i = 0; i < n; i++) { |
| 93 | + int index = 0; |
| 94 | + int prev = 0; |
| 95 | + for (int j = 0; j < n; j++) { |
| 96 | + if (map[i][j] != 0) { |
| 97 | + if (prev == map[i][j]) { |
| 98 | + map[i][index - 1] = prev * 2; |
| 99 | + prev = 0; |
| 100 | + } else { |
| 101 | + prev = map[i][j]; |
| 102 | + map[i][index] = prev; |
| 103 | + index++; |
| 104 | + } |
| 105 | + if (j != index - 1) map[i][j] = 0; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } else { |
| 110 | + for (int i = 0; i < n; i++) { |
| 111 | + int index = n - 1; |
| 112 | + int prev = 0; |
| 113 | + for (int j = n - 1; j >= 0; j--) { |
| 114 | + if (map[i][j] != 0) { |
| 115 | + if (prev == map[i][j]) { |
| 116 | + map[i][index + 1] = prev * 2; |
| 117 | + prev = 0; |
| 118 | + } else { |
| 119 | + prev = map[i][j]; |
| 120 | + map[i][index] = prev; |
| 121 | + index--; |
| 122 | + } |
| 123 | + if (j != index + 1) map[i][j] = 0; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public static void findMax() { |
| 131 | + for (int i = 0; i < n; i++) { |
| 132 | + for (int j = 0; j < n; j++) { |
| 133 | + answer = Math.max(answer, map[i][j]); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + public static int getCurrentMax() { |
| 139 | + int max = 0; |
| 140 | + for (int i = 0; i < n; i++) { |
| 141 | + for (int j = 0; j < n; j++) { |
| 142 | + max = Math.max(max, map[i][j]); |
| 143 | + } |
| 144 | + } |
| 145 | + return max; |
| 146 | + } |
| 147 | +} |
| 148 | +``` |
0 commit comments