알고리즘
[SWEA/JAVA] 2001 파리퇴치
writing turtle
2024. 11. 11. 19:26
출처 : https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PzOCKAigDFAUq

시간이 매우 넉넉해서 완전탐색으로 모든 경우를 다 검색했다. 부분합으로도 구할 수 있다는데 몰라.. 그냥 마음가는대로 풀래... 4중 for문의 압박이 있긴하지만.. 직관적이니까...
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testCase = sc.nextInt();
for(int tc = 0; tc < testCase; tc++){
int n = sc.nextInt(); // map의 크기 NxN
int m = sc.nextInt(); // 파리채의 크기 MxM
int[][] map = new int[n][n];
int max = 0;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
map[i][j] = sc.nextInt();
}
}
for(int i=0; i<=n-m; i++){
for(int j=0; j<=n-m; j++){
int temp = 0;
for(int l=0; l<m; l++){
for(int k=0; k<m; k++){
temp += map[i+l][j+k];
}
}
max = temp > max ? temp : max;
}
}
System.out.println("#" + (tc+1) + " " + max);
}
}
}