여니의 취준 준비/코딩테스트 (Java)

[SWEA] 2001. 파리 퇴치 in python

여니's 2022. 7. 11. 22:48

 

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PzOCKAigDFAUq&categoryId=AV5PzOCKAigDFAUq&categoryType=CODE&problemTitle=2001&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com


 

 

 

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T;
        T = sc.nextInt();

        for (int test_case = 1; test_case <= T; test_case++) {
            int N = sc.nextInt();
            int M = sc.nextInt(); // 파리채 크기
            int answer = 0;
            int[][] array=new int[N][N];

            for(int i=0; i<N; i++){
                for(int j=0; j<N; j++){
                    array[i][j]=sc.nextInt();
                }
            }
            for(int i=0; i<N-M+1; i++){
                for(int j=0; j<N-M+1; j++){
                    int temp=0;
                    for(int k=0; k<M; k++){
                        for(int p=0; p<M; p++){
                            temp+=array[i+k][j+p];
                        }
                    }
                    answer=Math.max(answer,temp);
                }
            }
            System.out.printf("#%d %d\n",test_case,answer);
        }
    }
}