|
| 1 | +package org.example._25week; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.StringTokenizer; |
| 7 | + |
| 8 | +public class Game2048 { |
| 9 | + |
| 10 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 11 | + private static int mapSize; |
| 12 | + private static final int EMPTY = 0; |
| 13 | + private static int maxValue = 0; |
| 14 | + private static int[] maxPath = new int[5]; |
| 15 | + private static int UP = 0; |
| 16 | + private static int DOWN = 1; |
| 17 | + private static int RIGHT = 2; |
| 18 | + private static int LEFT = 3; |
| 19 | + |
| 20 | + public static void main(String[] args) throws IOException { |
| 21 | + mapSize = Integer.parseInt(br.readLine()); |
| 22 | + final int[][] map = new int[mapSize][mapSize]; |
| 23 | + |
| 24 | + for (int i = 0; i < mapSize; i++) { |
| 25 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 26 | + for (int j = 0; j < mapSize; j++) { |
| 27 | + map[i][j] = Integer.parseInt(st.nextToken()); |
| 28 | + maxValue = Math.max(maxValue, map[i][j]); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + if (mapSize == 1) { |
| 33 | + System.out.println(maxValue); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + int[] path = new int[5]; |
| 38 | + dfs(0, map, path); |
| 39 | + System.out.println(maxValue); |
| 40 | + for (int dir : maxPath) { |
| 41 | + System.out.print(dir+" "); |
| 42 | + } |
| 43 | + System.out.println(); |
| 44 | + } |
| 45 | + |
| 46 | + private static void dfs(int depth, int[][] map, int[] path) { |
| 47 | + if (depth == 5) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | +// if (depth == 0) { |
| 52 | +// int[][] upSwipeCloneMap = upSwipe(map); |
| 53 | +// dfs(depth + 1, upSwipeCloneMap); |
| 54 | +// } |
| 55 | +// |
| 56 | +// if (depth == 1) { |
| 57 | +// int[][] rightSwipeCloneMap = rightSwipe(map); |
| 58 | +// dfs(depth + 1, rightSwipeCloneMap); |
| 59 | +// } |
| 60 | + |
| 61 | + // left swipe |
| 62 | + path[depth] = LEFT; |
| 63 | + int[][] leftSwipeCloneMap = leftSwipe(map, path); |
| 64 | + dfs(depth + 1, leftSwipeCloneMap, path); |
| 65 | + |
| 66 | + // right swipe |
| 67 | + path[depth] = RIGHT; |
| 68 | + int[][] rightSwipeCloneMap = rightSwipe(map, path); |
| 69 | + dfs(depth + 1, rightSwipeCloneMap, path); |
| 70 | + |
| 71 | + // up swipe |
| 72 | + path[depth] = UP; |
| 73 | + int[][] upSwipeCloneMap = upSwipe(map, path); |
| 74 | + dfs(depth + 1, upSwipeCloneMap, path); |
| 75 | + |
| 76 | +// down swipe |
| 77 | + path[depth] = DOWN; |
| 78 | + int[][] downSwipeCloneMap = downSwipe(map, path); |
| 79 | + dfs(depth + 1, downSwipeCloneMap, path); |
| 80 | + } |
| 81 | + |
| 82 | + private static int[][] leftSwipe(final int[][] map, int[] path) { |
| 83 | + int[][] cloneMap = clone(map); |
| 84 | + |
| 85 | + for (int row = 0; row < mapSize; row++) { |
| 86 | + int pivot = 0; |
| 87 | + int startPoint = 0; |
| 88 | + for (int col = pivot; col < mapSize; col++) { |
| 89 | + if (cloneMap[row][col] != EMPTY) { |
| 90 | + startPoint = col + 1; |
| 91 | + cloneMap[row][pivot] = cloneMap[row][col]; |
| 92 | + if (col != pivot) { |
| 93 | + cloneMap[row][col] = 0; |
| 94 | + } |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + for (int col = startPoint; col < mapSize; col++) { |
| 100 | + if (cloneMap[row][col] == EMPTY) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + |
| 104 | + if (pivot == col) { |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + if (cloneMap[row][pivot] == cloneMap[row][col]) { |
| 109 | + cloneMap[row][pivot] *= 2; |
| 110 | + maxValue = Math.max(maxValue, cloneMap[row][pivot]); |
| 111 | + maxPath = path.clone(); |
| 112 | + cloneMap[row][col] = 0; |
| 113 | + pivot++; |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + if (cloneMap[row][pivot] == 0) { |
| 118 | + cloneMap[row][pivot] = cloneMap[row][col]; |
| 119 | + cloneMap[row][col] = 0; |
| 120 | + } else { |
| 121 | + col--; |
| 122 | + pivot++; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return cloneMap; |
| 128 | + } |
| 129 | + |
| 130 | + private static int[][] upSwipe(final int[][] map, int[] path) { |
| 131 | + int[][] cloneMap = clone(map); |
| 132 | + for (int col = 0; col < mapSize; col++) { |
| 133 | + int pivot = 0; |
| 134 | + int startPoint = 0; |
| 135 | + for (int row = pivot; row < mapSize; row++) { |
| 136 | + if (cloneMap[row][col] != EMPTY) { |
| 137 | + startPoint = row + 1; |
| 138 | + cloneMap[pivot][col] = cloneMap[row][col]; |
| 139 | + if (row != pivot) { |
| 140 | + cloneMap[row][col] = 0; |
| 141 | + } |
| 142 | + break; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + for (int row = startPoint; row < mapSize; row++) { |
| 147 | + if (cloneMap[row][col] == EMPTY) { |
| 148 | + continue; |
| 149 | + } |
| 150 | + |
| 151 | + if (pivot == row) { |
| 152 | + continue; |
| 153 | + } |
| 154 | + |
| 155 | + if (cloneMap[pivot][col] == cloneMap[row][col]) { |
| 156 | + cloneMap[pivot][col] *= 2; |
| 157 | + maxValue = Math.max(maxValue, cloneMap[pivot][col]); |
| 158 | + maxPath = path.clone(); |
| 159 | + |
| 160 | + cloneMap[row][col] = 0; |
| 161 | + pivot++; |
| 162 | + continue; |
| 163 | + } |
| 164 | + |
| 165 | + if (cloneMap[pivot][col] == 0) { |
| 166 | + cloneMap[pivot][col] = cloneMap[row][col]; |
| 167 | + cloneMap[row][col] = 0; |
| 168 | + } else { |
| 169 | + row--; |
| 170 | + pivot++; |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + return cloneMap; |
| 176 | + } |
| 177 | + |
| 178 | + private static int[][] rightSwipe(final int[][] map, int[] path) { |
| 179 | + int[][] cloneMap = clone(map); |
| 180 | + for (int row = 0; row < mapSize; row++) { |
| 181 | + int pivot = mapSize - 1; |
| 182 | + int startPoint = mapSize - 1; |
| 183 | + for (int col = pivot; col >= 0; col--) { |
| 184 | + if (cloneMap[row][col] != EMPTY) { |
| 185 | + startPoint = col - 1; |
| 186 | + cloneMap[row][pivot] = cloneMap[row][col]; |
| 187 | + if (col != pivot) { |
| 188 | + cloneMap[row][col] = 0; |
| 189 | + } |
| 190 | + break; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + for (int col = startPoint; col >= 0; col--) { |
| 195 | + if (cloneMap[row][col] == EMPTY) { |
| 196 | + continue; |
| 197 | + } |
| 198 | + |
| 199 | + if (pivot == col) { |
| 200 | + continue; |
| 201 | + } |
| 202 | + |
| 203 | + if (cloneMap[row][pivot] == cloneMap[row][col]) { |
| 204 | + cloneMap[row][pivot] *= 2; |
| 205 | + maxValue = Math.max(maxValue, cloneMap[row][pivot]); |
| 206 | + maxPath = path.clone(); |
| 207 | + |
| 208 | + cloneMap[row][col] = 0; |
| 209 | + pivot--; |
| 210 | + continue; |
| 211 | + } |
| 212 | + |
| 213 | + if (cloneMap[row][pivot] == 0) { |
| 214 | + cloneMap[row][pivot] = cloneMap[row][col]; |
| 215 | + cloneMap[row][col] = 0; |
| 216 | + } else { |
| 217 | + col++; |
| 218 | + pivot--; |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + return cloneMap; |
| 224 | + } |
| 225 | + |
| 226 | + private static int[][] downSwipe(final int[][] map, int[] path) { |
| 227 | + int[][] cloneMap = clone(map); |
| 228 | + for (int col = 0; col < mapSize; col++) { |
| 229 | + int pivot = mapSize - 1; |
| 230 | + int startPoint = mapSize - 1; |
| 231 | + for (int row = pivot; row >= 0; row--) { |
| 232 | + if (cloneMap[row][col] != EMPTY) { |
| 233 | + startPoint = row - 1; |
| 234 | + cloneMap[pivot][col] = cloneMap[row][col]; |
| 235 | + if (row != pivot) { |
| 236 | + cloneMap[row][col] = 0; |
| 237 | + } |
| 238 | + break; |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + for (int row = startPoint; row >= 0; row--) { |
| 243 | + if (cloneMap[row][col] == EMPTY) { |
| 244 | + continue; |
| 245 | + } |
| 246 | + |
| 247 | + if (pivot == row) { |
| 248 | + continue; |
| 249 | + } |
| 250 | + |
| 251 | + if (cloneMap[pivot][col] == cloneMap[row][col]) { |
| 252 | + cloneMap[pivot][col] *= 2; |
| 253 | + maxValue = Math.max(maxValue, cloneMap[pivot][col]); |
| 254 | + maxPath = path.clone(); |
| 255 | + |
| 256 | + cloneMap[row][col] = 0; |
| 257 | + pivot--; |
| 258 | + continue; |
| 259 | + } |
| 260 | + |
| 261 | + if (cloneMap[pivot][col] == 0) { |
| 262 | + cloneMap[pivot][col] = cloneMap[row][col]; |
| 263 | + cloneMap[row][col] = 0; |
| 264 | + } else { |
| 265 | + row++; |
| 266 | + pivot--; |
| 267 | + } |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + return cloneMap; |
| 272 | + } |
| 273 | + |
| 274 | + private static int[][] clone(int[][] map) { |
| 275 | + int[][] cloneMap = new int[mapSize][mapSize]; |
| 276 | + |
| 277 | + for (int i = 0; i < mapSize; i++) { |
| 278 | + for (int j = 0; j < mapSize; j++) { |
| 279 | + cloneMap[i][j] = map[i][j]; |
| 280 | + } |
| 281 | + } |
| 282 | + |
| 283 | + return cloneMap; |
| 284 | + } |
| 285 | +} |
0 commit comments