|
| 1 | +```java |
| 2 | +import java.awt.Point; |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | +public class Main { |
| 9 | + |
| 10 | + static long ans; |
| 11 | + static int size; |
| 12 | + static long[][] arr,sumArr; |
| 13 | + static int[] dy = {0,1}; |
| 14 | + static int[] dx = {1,0}; |
| 15 | + |
| 16 | + public static void main(String[] args) throws IOException { |
| 17 | + init(); |
| 18 | + process(); |
| 19 | + print(); |
| 20 | + } |
| 21 | + |
| 22 | + private static void init() throws IOException { |
| 23 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 24 | + size = Integer.parseInt(br.readLine()); |
| 25 | + ans = Integer.MIN_VALUE; |
| 26 | + |
| 27 | + arr = new long[size][size]; |
| 28 | + sumArr = new long[size][size]; // i,j -> 0,0부터 i.j까지 직사각형 하 |
| 29 | + for (int i = 0; i < size; i++) { |
| 30 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 31 | + for (int j = 0; j < size; j++) { |
| 32 | + arr[i][j] = Long.parseLong(st.nextToken()); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private static void process() throws IOException { |
| 38 | + makeSumArr(); |
| 39 | + |
| 40 | + for (int i = 0; i < size; i++) { |
| 41 | + for (int j = 0; j < size; j++) { |
| 42 | + calculate(i,j); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + private static void calculate(int y, int x) { |
| 49 | + for (int i = 0; i < size; i++) { |
| 50 | + if ( y - i < 0 || x - i < 0) continue; |
| 51 | + long cur = sumArr[y][x]; |
| 52 | + if ( y- i > 0) cur -= sumArr[y-i-1][x]; |
| 53 | + if ( x - i > 0 ) cur -= sumArr[y][x-i-1]; |
| 54 | + if ( x-i > 0 && y-i > 0) cur += sumArr[y-i-1][x-i-1]; |
| 55 | + |
| 56 | + ans = Math.max(ans, cur); |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + private static void makeSumArr() { |
| 62 | + |
| 63 | + for (int i = 0; i < size; i++) { |
| 64 | + for (int j = 0; j < size; j++) { |
| 65 | + if (i == 0 && j == 0) sumArr[i][j] = arr[i][j]; |
| 66 | + else if (i == 0) sumArr[i][j]= sumArr[i][j-1] + arr[i][j]; |
| 67 | + else if (j == 0) sumArr[i][j] = sumArr[i-1][j] + arr[i][j]; |
| 68 | + else sumArr[i][j] = sumArr[i-1][j] + sumArr[i][j-1] + arr[i][j] - sumArr[i-1][j-1]; |
| 69 | + |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + private static void print() { |
| 78 | + System.out.println(ans); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | +``` |
0 commit comments