SWEA2001 파리 퇴치

단순한 2차원 배열 구현 문제다.

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

class Solution {
	static BufferedReader br;
	static BufferedWriter bw;
	static StringTokenizer st;
	static int[][] map;

	static int swing(int M, int i, int j) { //
		int count = 0;
		for (int k = i; k < i + M; k++) {
			for (int l = j; l < j + M; l++) {
				count += map[k][l];
			}
		}
		return count;
	}

	public static void main(String args[]) throws Exception {
		br = new BufferedReader(new InputStreamReader(System.in));
		bw = new BufferedWriter(new OutputStreamWriter(System.out));
		int T = Integer.parseInt(br.readLine());
		for (int tc = 1; tc <= T; tc++) {
			st = new StringTokenizer(br.readLine(), " ");
			int N = Integer.parseInt(st.nextToken());
			int M = Integer.parseInt(st.nextToken());
			int answer = 0;
			map = new int[N][N];
			for (int i = 0; i < N; i++) {
				st = new StringTokenizer(br.readLine(), " ");
				for (int j = 0; j < N; j++) {
					map[i][j] = Integer.parseInt(st.nextToken());
				}
			}

			for (int i = 0; i < N - M + 1; i++) {
				for (int j = 0; j < N - M + 1; j++) {
					int tmp = swing(M, i, j);
					if (tmp > answer) {
						answer = tmp;
					}
				}
			}

			bw.write(String.format("#%d ", tc));
			bw.write(String.format("%d\n", answer));

		}
		bw.flush();
		bw.close();
	}
}

좋은 웹페이지 즐겨찾기