PS/Java
[SWEA 2001] 파리 퇴치
siyamaki
2021. 3. 9. 17:41
N x N 배열에 M x M의 파리채를 내려 쳐서 얼마나 많은 파리가 죽는지 계산한다.
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
for(int tc = 1; tc <= T; tc++) {
int n = scanner.nextInt();
int m = scanner.nextInt();
int sum = 0;
int[][] arr = new int[n][n];
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
arr[i][j] = scanner.nextInt();
}
}
for(int i = 0; i <= n - m; i++) {
for(int j = 0; j <= n - m; j++) {
int tmp = 0;
for(int k = i; k < i + m; k++) {
for(int l = j; l < j + m; l++) {
tmp += arr[k][l];
}
}
if(tmp > sum) {
sum = tmp;
}
}
}
System.out.println("#"+ tc +" "+sum);
}
}
}
0,0부터 N-M 까지 파리채 크기만큼 돌면 된다.