본문으로 바로가기

https://swexpertacademy.com/main/solvingProblem/solvingProblem.do

 

SW Expert Academy

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

swexpertacademy.com

 


 

import java.util.Scanner;

public class Solution {

	private static int n;
	private static int m;
	private static int[][] fly;
	private static int[][] result;
	
    
	public static int flyNum (int i, int j) {
    
		int sum = 0;
		
		for (int x = i; x < m + i; x++) {
			for (int y = j; y < m + j; y++)
				sum += fly[x][y];
		}
		
		return sum;
	}


	public static void main (String[] args) {
    
		Scanner scanner = new Scanner(System.in);
		
		int T = scanner.nextInt();
		for (int t = 1; t <= T; t++) {
			n = scanner.nextInt(); // 파리가 있는 영역
			m = scanner.nextInt(); // 파리채 크기
			
			fly = new int[n][n];
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					fly[i][j] = scanner.nextInt();
				}
			}
			
			result = new int[n-m+1][n-m+1];
			int max = 0;
			for (int i = 0; i < n-m+1; i++) {
				for (int j = 0; j < n-m+1; j++) {
					result[i][j] = flyNum(i, j);
					if (result[i][j] > max)
						max = result[i][j];
				}
			}
			
			System.out.println("#" + t + " " + max);
		}
		
		scanner.close();
	}
}