SW Expert Academy
SW ํ๋ก๊ทธ๋๋ฐ ์ญ๋ ๊ฐํ์ ๋์์ด ๋๋ ๋ค์ํ ํ์ต ์ปจํ ์ธ ๋ฅผ ํ์ธํ์ธ์!
swexpertacademy.com
๋ฌธ์ ์ค๋ช
NxN ๋ฐฐ์ด ์์ ํด๋น ์์ญ์ ์กด์ฌํ๋ ํ๋ฆฌ์ ๊ฐ์๊ฐ ์กด์ฌํจ.
์ด๋, MxM ํฌ๊ธฐ์ ํ๋ฆฌ์ฑ๋ฅผ ํ ๋ฒ ๋ด๋ฆฌ์ณ ์ต๋ํ ํ๋ฆฌ๋ฅผ ์ฃฝ์ด๊ณ ์ ํ ๋ ์ฃฝ์ ํ๋ฆฌ์ ๊ฐ์ ๊ตฌํ๊ธฐ
์ ์ฝ ์ฌํญ
- N์ 5 ์ด์ 15 ์ดํ
- M์ 2 ์ด์ N ์ดํ
- ๊ฐ ์์ญ์ ํ๋ฆฌ ๊ฐ์๋ 30 ์ดํ
์ ์ถ๋ ฅ ์์
| N | M | arr | result |
| 5 | 2 | [[1, 3, 3, 6, 7], [8, 13, 9, 12, 8], [4, 16, 11, 12, 6], [2, 4, 1, 23, 2], [9, 13, 4, 7, 3]] |
49 |


ํ์ด ๋ฐฉ๋ฒ
2์ฐจ์ ๋ฐฐ์ด ๋์ ํฉ ์ด์ฉ
1. 2์ฐจ์ ๋ฐฐ์ด ๋์ ํฉ ๊ตฌํ๊ธฐ
| 0 | 1 | 2 | 3 | 4 | |
| 0 | 1 | 4 | 7 | 13 | 20 |
| 1 | 9 | 25 | 37 | 55 | 70 |
| 2 | 13 | 45 | 68 | 98 | 119 |
| 3 | 15 | 51 | 75 | 28 | 151 |
| 4 | 24 | 73 | 101 | 161 | 187 |
- 0ํ → DP[0][c] = DP[0][c-1]+arr[0][c]
- 0์ด → DP[r][0] = DP[r-1][0]+arr[r][0]
- ๋๋จธ์ง → DP[r][c] = DP[r-1][c] + DP[r][c-1] - DP[r-1][c-1] + arr[r][c]
2. MxM ํฌ๊ธฐ์ ๋ถ๋ถํฉ ๊ตฌํ๊ธฐ
| 0 | 1 | 2 | 3 | 4 | |
| 0 | 1 | 4 | 7 | 13 | 20 |
| 1 | 9 | 25 | 28 | 30 | 33 |
| 2 | 13 | 41 | 49 | 44 | 38 |
| 3 | 15 | .. | .. | .. | .. |
| 4 | 24 | .. | .. | .. | .. |
- r >= M-1 && c >= M-1 ์ธ ๊ฒฝ์ฐ๋ง ๋ถ๋ถํฉ ๊ตฌํ๊ธฐ
- ์ด๊ธฐ max ๊ฐ = DP[M-1][M-1] = 25
- r-M < 0์ธ ๊ฒฝ์ฐ → DP[r][c] - DP[r][c-M]
- c-M < 0์ธ ๊ฒฝ์ฐ → DP[r][c] - DP[r-M][c]
- ๋๋จธ์ง → DP[r][c] - DP[r-M][c] - DP[r][c-M] + DP[r-M][c-M]
์ฝ๋
import java.util.*;
import java.io.*;
import java.lang.*;
class Solution
{
public static void main(String args[]) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine()); // ํ
์คํธ ์ผ์ด์ค ๊ฐ์
StringBuilder sb = new StringBuilder();
for(int test_case = 1; test_case <= T; test_case++)
{
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken()); // NxN ๋ฐฐ์ด ํฌ๊ธฐ
int M = Integer.parseInt(st.nextToken()); // ํ๋ฆฌ์ฑ ํฌ๊ธฐ
int[][] arr = new int[N][N];
// ๋ฐฐ์ด ์ฑ์ฐ๊ธฐ
for(int r=0; r<N; r++){
st = new StringTokenizer(br.readLine());
int c = 0;
while(st.hasMoreTokens()){
arr[r][c++] = Integer.parseInt(st.nextToken());
}
}
int[][] dp = new int[N][N]; // ๋์ ํฉ
dp[0][0] = arr[0][0]; // 0ํ 0์ด ๊ฐ ์ค์
// 0ํ ๋์ ํฉ
for(int c=1; c<N; c++) dp[0][c] = dp[0][c-1] + arr[0][c];
// 0์ด ๋์ ํฉ
for(int r=1; r<N; r++) dp[r][0] = dp[r-1][0] + arr[r][0];
// ๋๋จธ์ง ๋์ ํฉ
for(int r=1; r<N; r++){
for(int c=1; c<N; c++) {
dp[r][c] = dp[r-1][c] + dp[r][c-1] - dp[r-1][c-1] + arr[r][c];
}
}
// MxM ํฌ๊ธฐ๋งํผ ๋ถ๋ถํฉ ๊ตฌํ๊ธฐ
int max = dp[M-1][M-1]; // ์ด๊ธฐ ์ต๋๊ฐ ์ค์
for(int r=M-1; r<N; r++) {
for(int c=M-1; c<N; c++) {
int part_sum = 0; // ๋ถ๋ถํฉ
if(r==M-1 && c==M-1) continue;
else if(r-M < 0) {
part_sum = dp[r][c] - dp[r][c-M];
}else if(c-M < 0) {
part_sum = dp[r][c] - dp[r-M][c];
}else {
part_sum = dp[r][c] - dp[r-M][c] - dp[r][c-M] + dp[r-M][c-M];
}
max = Math.max(max, part_sum); // ์ต๋๊ฐ ๊ฐฑ์
}
}
sb.append(String.format("#%d %d\n", test_case, max));
}
System.out.println(sb);
}
}
'์๊ณ ๋ฆฌ์ฆ > ์ฝ๋ฉํ ์คํธ ๋ฌธ์ ํ์ด' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [ํ๋ก๊ทธ๋๋จธ์ค-60059]์๋ฌผ์ ์ ์ด์ (0) | 2026.06.29 |
|---|---|
| [ํ๋ก๊ทธ๋๋จธ์ค-12907] ๊ฑฐ์ค๋ฆ๋ (0) | 2026.06.17 |
| [ํ๋ก๊ทธ๋๋จธ์ค-258711] ๋๋๊ณผ ๋ง๋ ๊ทธ๋ํ (0) | 2026.05.27 |
| [ํ๋ก๊ทธ๋๋จธ์ค-49191] ์์ (0) | 2026.05.17 |
| [ํ๋ก๊ทธ๋๋จธ์ค-1843] ์ฌ์น์ฐ์ฐ (0) | 2026.05.02 |