|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.StringTokenizer; |
| 5 | + |
| 6 | +public class BJ_12100_2048_Easy { |
| 7 | + |
| 8 | + private static final int UP = 0; |
| 9 | + private static final int DOWN = 1; |
| 10 | + private static final int LEFT = 2; |
| 11 | + private static final int RIGHT = 3; |
| 12 | + |
| 13 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 14 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 15 | + private static StringTokenizer st; |
| 16 | + |
| 17 | + private static int N; |
| 18 | + private static int ans; |
| 19 | + private static int[] order; |
| 20 | + private static int[][] map; |
| 21 | + private static int[][] copiedMap; |
| 22 | + private static boolean[][] visited; |
| 23 | + |
| 24 | + public static void main(String[] args) throws IOException { |
| 25 | + init(); |
| 26 | + sol(); |
| 27 | + } |
| 28 | + |
| 29 | + private static void init() throws IOException { |
| 30 | + N = Integer.parseInt(br.readLine()); |
| 31 | + ans = 0; |
| 32 | + |
| 33 | + order = new int[5]; |
| 34 | + |
| 35 | + map = new int[N][N]; |
| 36 | + copiedMap = new int[N][N]; |
| 37 | + visited = new boolean[N][N]; |
| 38 | + for (int i = 0; i < N; i++) { |
| 39 | + st = new StringTokenizer(br.readLine()); |
| 40 | + for (int j = 0; j < N; j++) { |
| 41 | + map[i][j] = Integer.parseInt(st.nextToken()); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private static void sol() throws IOException { |
| 47 | + dfs(0); |
| 48 | + |
| 49 | + bw.write(ans + ""); |
| 50 | + bw.flush(); |
| 51 | + bw.close(); |
| 52 | + br.close(); |
| 53 | + } |
| 54 | + |
| 55 | + private static void dfs(int depth) { |
| 56 | + if (depth == 5) { |
| 57 | + copyMap(); |
| 58 | + playGame(); |
| 59 | + updateMaxValue(); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + for (int i = 0; i < 4; i++) { |
| 64 | + order[depth] = i; |
| 65 | + dfs(depth + 1); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private static void copyMap() { |
| 70 | + for (int i = 0; i < N; i++) { |
| 71 | + System.arraycopy(map[i], 0, copiedMap[i], 0, N); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private static void playGame() { |
| 76 | + for (int d : order) { |
| 77 | + clearVisited(); |
| 78 | + if (d == UP) { |
| 79 | + up(); |
| 80 | + } else if (d == DOWN) { |
| 81 | + down(); |
| 82 | + } else if (d == LEFT) { |
| 83 | + left(); |
| 84 | + } else if (d == RIGHT) { |
| 85 | + right(); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private static void up() { |
| 91 | + for (int j = 0; j < N; j++) { |
| 92 | + for (int i = 1; i < N; i++) { |
| 93 | + if (copiedMap[i][j] == 0) continue; |
| 94 | + |
| 95 | + int value = copiedMap[i][j]; |
| 96 | + copiedMap[i][j] = 0; |
| 97 | + |
| 98 | + int k = i - 1; |
| 99 | + // 0이 아닌 블록이나 경계를 찾기 |
| 100 | + while (k >= 0 && copiedMap[k][j] == 0) { |
| 101 | + k--; |
| 102 | + } |
| 103 | + |
| 104 | + if (k >= 0 && copiedMap[k][j] == value && !visited[k][j]) { |
| 105 | + // 합치기 가능 |
| 106 | + copiedMap[k][j] = value * 2; |
| 107 | + visited[k][j] = true; |
| 108 | + } else { |
| 109 | + // 합치기 불가능 - 빈 공간에 배치 |
| 110 | + copiedMap[k + 1][j] = value; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private static void down() { |
| 117 | + for (int j = 0; j < N; j++) { |
| 118 | + for (int i = N - 2; i >= 0; i--) { |
| 119 | + if (copiedMap[i][j] == 0) continue; |
| 120 | + |
| 121 | + int value = copiedMap[i][j]; |
| 122 | + copiedMap[i][j] = 0; |
| 123 | + |
| 124 | + int k = i + 1; |
| 125 | + while (k < N && copiedMap[k][j] == 0) { |
| 126 | + k++; |
| 127 | + } |
| 128 | + |
| 129 | + if (k < N && copiedMap[k][j] == value && !visited[k][j]) { |
| 130 | + copiedMap[k][j] = value * 2; |
| 131 | + visited[k][j] = true; |
| 132 | + } else { |
| 133 | + copiedMap[k - 1][j] = value; |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private static void left() { |
| 140 | + for (int i = 0; i < N; i++) { |
| 141 | + for (int j = 1; j < N; j++) { |
| 142 | + if (copiedMap[i][j] == 0) continue; |
| 143 | + |
| 144 | + int value = copiedMap[i][j]; |
| 145 | + copiedMap[i][j] = 0; |
| 146 | + |
| 147 | + int k = j - 1; |
| 148 | + while (k >= 0 && copiedMap[i][k] == 0) { |
| 149 | + k--; |
| 150 | + } |
| 151 | + |
| 152 | + if (k >= 0 && copiedMap[i][k] == value && !visited[i][k]) { |
| 153 | + copiedMap[i][k] = value * 2; |
| 154 | + visited[i][k] = true; |
| 155 | + } else { |
| 156 | + copiedMap[i][k + 1] = value; |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private static void right() { |
| 163 | + for (int i = 0; i < N; i++) { |
| 164 | + for (int j = N - 2; j >= 0; j--) { |
| 165 | + if (copiedMap[i][j] == 0) continue; |
| 166 | + |
| 167 | + int value = copiedMap[i][j]; |
| 168 | + copiedMap[i][j] = 0; |
| 169 | + |
| 170 | + int k = j + 1; |
| 171 | + while (k < N && copiedMap[i][k] == 0) { |
| 172 | + k++; |
| 173 | + } |
| 174 | + |
| 175 | + if (k < N && copiedMap[i][k] == value && !visited[i][k]) { |
| 176 | + copiedMap[i][k] = value * 2; |
| 177 | + visited[i][k] = true; |
| 178 | + } else { |
| 179 | + copiedMap[i][k - 1] = value; |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + private static void updateMaxValue() { |
| 186 | + for (int i = 0; i < N; i++) { |
| 187 | + for (int j = 0; j < N; j++) { |
| 188 | + ans = Math.max(ans, copiedMap[i][j]); |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + private static void clearVisited() { |
| 194 | + for (int i = 0; i < N; i++) { |
| 195 | + Arrays.fill(visited[i], false); |
| 196 | + } |
| 197 | + } |
| 198 | +} |
| 199 | +``` |
0 commit comments