|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.Arrays; |
| 6 | + |
| 7 | +public class Main { |
| 8 | + private static BufferedReader br; |
| 9 | + private static int N; |
| 10 | + private static int[][] Map; |
| 11 | + public static void main(String[] args) throws IOException { |
| 12 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 13 | + N = Integer.parseInt(br.readLine()); |
| 14 | + Map = new int[N+1][N+1]; |
| 15 | + for(int r = 1; r < N+1; r++){ |
| 16 | + String[] temp = br.readLine().split(" "); |
| 17 | + for(int c = 1; c<N+1; c++){ |
| 18 | + Map[r][c] = Integer.parseInt(temp[c-1]); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + int answer = Integer.MIN_VALUE; |
| 23 | + //누적합 구하기 |
| 24 | + var preMap = new int[N+1][N+1]; |
| 25 | + for(int r = 1; r < N+1; r++){ |
| 26 | + for(int c = 1; c < N+1; c++){ |
| 27 | + preMap[r][c] = Map[r][c] + preMap[r-1][c] + preMap[r][c-1] - preMap[r-1][c-1]; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + // for(int[] line: preMap){ |
| 32 | + // System.out.println(Arrays.toString(line)); |
| 33 | + // } |
| 34 | + |
| 35 | + |
| 36 | + for(int r = 1; r<N+1; r++){ |
| 37 | + for(int c = 1; c<N+1; c++){ |
| 38 | + for(int n = 0; n<N+1; n++){ |
| 39 | + int[] rightDown = new int[2]; |
| 40 | + rightDown[0] = r+n; |
| 41 | + rightDown[1] = c+n; |
| 42 | + if(rightDown[0] >= N+1 || rightDown[1] >= N+1){ |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + int partitialSum; |
| 47 | + if(n == 0){ |
| 48 | + partitialSum = Map[r][c]; |
| 49 | + }else{ |
| 50 | + partitialSum= |
| 51 | + preMap[rightDown[0]][rightDown[1]] |
| 52 | + - preMap[rightDown[0] - (n+1)][rightDown[1]] |
| 53 | + - preMap[rightDown[0]][rightDown[1] - (n+1)] |
| 54 | + + preMap[r-1][c-1]; |
| 55 | + } |
| 56 | + answer = Math.max(answer, partitialSum); |
| 57 | + //System.out.println(String.format("(r, c)=(%d, %d) (rd0, rd1)=(%d, %d), partitialSum: %d", r, c, rightDown[0], rightDown[1], partitialSum)); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + System.out.println(answer); |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
0 commit comments