1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | import java.io.*; import java.util.*; public class Main { static int[] orders = new int[5]; static int[][] map; static int N, answer; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; st = new StringTokenizer(br.readLine()); N = Integer.parseInt(st.nextToken()); map = new int[N][N]; for (int i = 0; i < N; i++) { st = new StringTokenizer(br.readLine()); for (int j = 0; j < N; j++) { map[i][j] = Integer.parseInt(st.nextToken()); } } answer = Integer.MIN_VALUE; cases(0); System.out.println(answer); } private static void cases(int idx) { if (idx == 5) { int[][] temp = new int[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { temp[i][j] = map[i][j]; } } int lastIdx = 0; int prev = 0; for (int order : orders) { switch (order) { case 0: // 0 위쪽 당기기 for (int col = 0; col < N; col++) { lastIdx = 0; prev = 0; for (int row = 0; row < N; row++) { if (temp[row][col] == 0) continue; if (temp[row][col] == prev) { temp[lastIdx - 1][col] = temp[row][col] * 2; temp[row][col] = 0; prev = 0; } else { prev = temp[row][col]; temp[row][col] = 0; temp[lastIdx++][col] = prev; } } } break; case 1: // 1 아래쪽 당기기 for (int col = 0; col < N; col++) { lastIdx = N - 1; prev = 0; for (int row = N - 1; row >= 0; row--) { if (temp[row][col] == 0) continue; if (temp[row][col] == prev) { temp[lastIdx + 1][col] = temp[row][col] * 2; temp[row][col] = 0; prev = 0; } else { prev = temp[row][col]; temp[row][col] = 0; temp[lastIdx--][col] = prev; } } } break; case 2: // 2 왼쪽 당기기 for (int row = 0; row < N; row++) { lastIdx = 0; prev = 0; for (int col = 0; col < N; col++) { if (temp[row][col] == 0) continue; if (temp[row][col] == prev) { temp[row][lastIdx - 1] = temp[row][col] * 2; temp[row][col] = 0; prev = 0; } else { prev = temp[row][col]; temp[row][col] = 0; temp[row][lastIdx++] = prev; } } } break; case 3: // 3 오른쪽 당기기 for (int row = 0; row < N; row++) { lastIdx = N - 1; prev = 0; for (int col = N - 1; col >= 0; col--) { if (temp[row][col] == 0) continue; if (temp[row][col] == prev) { temp[row][lastIdx + 1] = temp[row][col] * 2; temp[row][col] = 0; prev = 0; } else { prev = temp[row][col]; temp[row][col] = 0; temp[row][lastIdx--] = prev; } } } break; } } for (int row = 0; row < N; row++) { for (int col = 0; col < N; col++) { int cur = temp[row][col]; answer = Math.max(cur, answer); } } return; } for (int i = 0; i < 5; i++) { orders[idx] = i; cases(idx + 1); } } } | cs |
반응형
'알고리즘 문제 풀이 > BOJ' 카테고리의 다른 글
[backjoon] 20057 마법사 상어와 토네이도 java (0) | 2022.04.29 |
---|---|
[backjoon]17822 원판 돌리기 java (0) | 2022.04.25 |
[backjoon]17144 미세먼지 안녕! java (0) | 2022.04.13 |
[backjoon]14500 테트로미노 java (0) | 2022.04.11 |
[beakjoon]7682 틱택토 java (0) | 2022.04.06 |