SWEA

swea 2001 [D2] 파리 퇴치 JAVA

녁이 2023. 11. 22. 17:05
728x90
반응형

풀이 :

4중 for문 -> map[i][j] ~ map[i+x][j+y]

 


Code

public class Solution2001 {
    public static void main(String[] args) throws IOException {

        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();

        for (int tc = 1; tc <= T; tc++) {
            int N = sc.nextInt();   // 5
            int M = sc.nextInt();   // 2
            int[][] map = new int[N][N];
            int maxSum = 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 sum = 0;
                    for (int x = 0; x < M; x++) {
                        for (int y = 0; y < M; y++) {
                            sum += map[i + x][j + y];
                        }
                    }
                    maxSum = Math.max(sum, maxSum);
                }
            }
            System.out.printf("#%d %d\n", tc, maxSum);
        }
        sc.close();
    }
}
728x90
반응형