|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | + |
| 8 | +public class Main { |
| 9 | + |
| 10 | + static final int END = 5; |
| 11 | + static final int UP = 0; |
| 12 | + static final int RIGHT = 1; |
| 13 | + static final int DOWN = 2; |
| 14 | + static final int LEFT = 3; |
| 15 | + |
| 16 | + static int[][] arr; |
| 17 | + static int[] dy = {-1,0,1,0}; |
| 18 | + static int[] dx = {0,1,0,-1}; |
| 19 | + static int size,ans; |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + public static void main(String[] args) throws IOException { |
| 24 | + init(); |
| 25 | + process(); |
| 26 | + print(); |
| 27 | + } |
| 28 | + |
| 29 | + public static void init() throws IOException { |
| 30 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 31 | + size = Integer.parseInt(br.readLine()); |
| 32 | + arr = new int[size][size]; |
| 33 | + ans = 0; |
| 34 | + |
| 35 | + for (int i = 0; i < size; i++) { |
| 36 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 37 | + for (int j = 0; j < size; j++) { |
| 38 | + arr[i][j] = Integer.parseInt(st.nextToken()); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + } |
| 44 | + |
| 45 | + public static void process() { |
| 46 | + dfs(arr, 0); |
| 47 | + } |
| 48 | + |
| 49 | + public static void dfs(int[][] board, int cnt) { |
| 50 | + if (cnt == END) { |
| 51 | + for (int i = 0 ; i < size; i++) { |
| 52 | + for (int j = 0; j < size; j++) { |
| 53 | + System.out.print(board[i][j]+ " "); |
| 54 | + ans = Math.max(board[i][j],ans); |
| 55 | + } |
| 56 | + System.out.println(); |
| 57 | + } |
| 58 | + System.out.println(); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + //dir으로 이동하면 dir부터 큐에 넣은 후, 하나씩 꺼내면 됨. 이걸 유지 |
| 64 | + |
| 65 | + for (int dir = 0 ; dir < 4; dir++) { |
| 66 | + Queue<Integer>[] qs = new Queue[size]; |
| 67 | + int[][] temp = new int[size][size]; |
| 68 | + boolean keepTrying = false; |
| 69 | + |
| 70 | + |
| 71 | + for (int i = 0 ; i < size; i++) { |
| 72 | + qs[i] = new LinkedList<Integer>(); |
| 73 | + for (int j = 0; j < size; j++) { |
| 74 | + int num = 0; |
| 75 | + |
| 76 | + if (dir == UP) num = board[j][i]; // 위에서부터 아래 순으로 담김 |
| 77 | + else if (dir == DOWN) num = board[size-1-j][i]; // 아래에서부터 위로 |
| 78 | + else if (dir == LEFT) num = board[i][j]; |
| 79 | + else if (dir == RIGHT) num = board[i][size-1-j]; |
| 80 | + |
| 81 | + if (num != 0 ) qs[i].add(num); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + for (int i = 0 ; i < size; i++) { |
| 86 | + Queue q = qs[i]; |
| 87 | + int idx = 0; |
| 88 | + |
| 89 | + while (!q.isEmpty()) { |
| 90 | + int target = -1; // 지금 보고있는 칸. |
| 91 | + |
| 92 | + if (dir == UP) target = temp[idx][i]; // 위에서부터 아래 순으로 담김 |
| 93 | + else if (dir == DOWN) target = temp[size-1-idx][i]; // 아래에서부터 위로 |
| 94 | + else if (dir == LEFT) target = temp[i][idx]; |
| 95 | + else if (dir == RIGHT) target = temp[i][size-1-idx]; |
| 96 | + |
| 97 | + |
| 98 | + int num = (int) q.poll(); |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + if (target != 0 && target != num) idx++; |
| 103 | + |
| 104 | + if (target == num) keepTrying = true; |
| 105 | + |
| 106 | + if (dir == UP) temp[idx][i] += num; |
| 107 | + else if (dir == DOWN) temp[size-1-idx][i] += num; |
| 108 | + else if (dir == LEFT) temp[i][idx] += num; |
| 109 | + else if (dir == RIGHT) temp[i][size-1-idx] += num; |
| 110 | + |
| 111 | + if (target == num) idx++; // 합쳐진것. 이제 이 칸에 볼일없음 |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + dfs(temp,cnt+1); |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + public static void print() { |
| 130 | + System.out.println(ans); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +``` |
0 commit comments