알고리즘(종합)/Lv.2

2001 - 파리퇴치

ch.0 2022. 11. 14. 17:14


package test;

import java.io.IOException;
import java.util.Scanner;

public class Solution {

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

		Scanner sc = new Scanner(System.in);

		int test = sc.nextInt();

		for (int k = 0; k < test; k++) {

			int s = sc.nextInt();
			int p = sc.nextInt();

			int count=0;
			int max=0;

			int[][] array = new int[s][s];

			for (int i = 0; i < s; i++) {
				for (int j = 0; j < s; j++) {
					array[i][j] = sc.nextInt();

				}

			}
			
			for (int i = 0; i < s-p+1; i++) {
				for (int j = 0; j < s-p+1; j++) {
					int sum=0;
					for(int x=0;x<p;x++ ) {
						for(int z=0;z<p;z++ ) {
							sum+=array[i+x][j+z];
						}
					}
					
					if(max<sum) {
						max=sum;
					}
					
				}

			}
			System.out.println("#"+(k+1)+" "+max);
		}
	}

}

1. 배열크기와 배열의 값을 입력받는다.

 

2. 5의 배열에 2의 파리채면 가로/세로4번씩 반복된다. 그래서 s-p+1로 했다.

 

3. 파리채의 수의 합을 더해준다.

 

4. 합을 비교해준다.