Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

자르비 왕국

[SW Academy] 2001 파리퇴치 - JAVA 본문

문제풀이

[SW Academy] 2001 파리퇴치 - JAVA

자르비옹스 2022. 2. 5. 15:40

1. 문제 유형 : 부르트 포스

2. 시간 복잡도 : O((N-M)^2*M^2)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Solution {
	static int N;
	static int M;
	static int answer;
	static int[][] map;

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		int T = Integer.parseInt(in.readLine());
		for (int i = 1; i <= T; i++) {
			String s = in.readLine();
			N = Integer.parseInt(s.split(" ")[0]);
			M = Integer.parseInt(s.split(" ")[1]);
			answer = 0;
			map = new int[N][N];
			for (int j = 0; j < N; j++) {
				StringTokenizer st = new StringTokenizer(in.readLine());
				for (int j2 = 0; j2 < N; j2++) {
					map[j][j2] = Integer.parseInt(st.nextToken());
				}
			}

			for (int j = 0; j <= N - M; j++) {
				for (int j2 = 0; j2 <= N - M; j2++) {
					solution(j, j2);
				}
			}
			sb.append("#").append(i).append(" ").append(answer).append('\n');
		}
		System.out.println(sb.toString());
	}

	public static void solution(int row, int col) {
		int sum = 0;
		for (int i = 0; i < M; i++) {
			for (int j = 0; j < M; j++) {
				if (row + i >= N || col + j >= N) return;
				sum += map[row + i][col + j];
			}
		}

		answer = Math.max(answer, sum);
	}

}