|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + static int N; |
| 7 | + static int[][] arr; |
| 8 | + public static void main(String[] args) throws IOException { |
| 9 | + input(); |
| 10 | + int ans = solve(); |
| 11 | + System.out.println(ans); |
| 12 | + } |
| 13 | + |
| 14 | + static int solve() { |
| 15 | + int minDiff = Integer.MAX_VALUE; |
| 16 | + // 기준점을 중심으로 경계선을 나눈다. |
| 17 | + for (int x = 1; x < N+1; x++) { |
| 18 | + for (int y = 1; y < N+1; y++) { |
| 19 | + for (int d1 = 1; x + d1 <= N && 1 <= y - d1 ; d1++) { |
| 20 | + for (int d2 = 1; |
| 21 | + y + d2 <= N && x + d1 + d2 <= N && y - d1 + d2 <= N && y + d2 - d1 <= N; |
| 22 | + d2++) { |
| 23 | + int tmp = makeBorder(x, y, d1, d2); |
| 24 | + if (tmp < minDiff) { minDiff = tmp; } |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + return minDiff; |
| 30 | + } |
| 31 | + |
| 32 | + static int makeBorder(int x, int y, int d1, int d2) { |
| 33 | + int[][] area = new int[N+1][N+1]; |
| 34 | + for (int i = 1; i < N+1; i++) { |
| 35 | + for (int j = 1; j < N+1; j++) { |
| 36 | + area[i][j] = 5; // 5번 선거구로 채워둠 |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + // 경계선은 0으로 채운다. |
| 41 | + for (int i = 0; i <= d1; i++) { |
| 42 | + area[x+i][y-i] = 0; |
| 43 | + } |
| 44 | + for (int i = 0; i <= d2; i++) { |
| 45 | + area[x+i][y+i] = 0; |
| 46 | + } |
| 47 | + for (int i = 0; i <= d2; i++) { |
| 48 | + area[x+d1+i][y-d1+i] = 0; |
| 49 | + } |
| 50 | + for (int i = 0; i <= d1; i++) { |
| 51 | + area[x+d2+i][y+d2-i] = 0; |
| 52 | + } |
| 53 | + |
| 54 | + // 1번 구역 표시 |
| 55 | + for (int r = 1; r < x + d1; r++) { |
| 56 | + for (int c = 1; c <= y; c++) { |
| 57 | + if (area[r][c] == 0) { break; } |
| 58 | + area[r][c] = 1; |
| 59 | + } |
| 60 | + } |
| 61 | + // 2번 구역 표시 |
| 62 | + for (int r = 1; r <= x + d2; r++) { |
| 63 | + for (int c = N; c >= y+1; c--) { |
| 64 | + if (area[r][c] == 0) { break; } |
| 65 | + area[r][c] = 2; |
| 66 | + } |
| 67 | + } |
| 68 | + // 3번 구역 표시 |
| 69 | + for (int r = x + d1; r <= N; r++) { |
| 70 | + for (int c = 1; c < y - d1 + d2; c++) { |
| 71 | + if (area[r][c] == 0) { break; } |
| 72 | + area[r][c] = 3; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + //4번 구역 표시 |
| 77 | + for (int r = x + d2 + 1; r <= N; r++) { |
| 78 | + for (int c = N; c >= y - d1 + d2; c--) { |
| 79 | + if (area[r][c] == 0) { break; } |
| 80 | + area[r][c] =4; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + int[] sums = new int[6]; |
| 85 | + for (int r = 1; r < N+1; r++) { |
| 86 | + for (int c = 1; c < N+1; c++) { |
| 87 | + if (area[r][c] == 0) { |
| 88 | + sums[5] += arr[r][c]; |
| 89 | + } else { |
| 90 | + sums[area[r][c]] += arr[r][c]; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + int maxSum = Integer.MIN_VALUE; |
| 96 | + int minSum = Integer.MAX_VALUE; |
| 97 | + for (int i = 1; i <= 5; i++) { |
| 98 | + if (maxSum < sums[i]) { maxSum = sums[i]; } |
| 99 | + if (sums[i] < minSum) { minSum = sums[i]; } |
| 100 | + } |
| 101 | + return maxSum - minSum; |
| 102 | + } |
| 103 | + |
| 104 | + static void input() throws IOException { |
| 105 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 106 | + N = Integer.parseInt(br.readLine()); |
| 107 | + arr = new int[N+1][N+1]; |
| 108 | + for (int i = 1; i < N+1; i++) { |
| 109 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 110 | + for (int j = 1; j < N+1; j++) { |
| 111 | + arr[i][j] = Integer.parseInt(st.nextToken()); |
| 112 | + } |
| 113 | + } |
| 114 | + br.close(); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +``` |
0 commit comments