끄적끄적
SWE 파리 퇴치 본문
package day0803; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Q2001 { static int M, N; static int Max; static int[][] arr; public static void main(String[] args) throws IOException { // TODO Auto-generated method stub BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int test = Integer.parseInt(bf.readLine()); for (int t = 1; t <= test; t++) { String s = bf.readLine(); StringTokenizer st = new StringTokenizer(s); N = Integer.parseInt(st.nextToken()); M = Integer.parseInt(st.nextToken()); arr = new int[N + 1][N + 1]; for (int i = 0; i < N; i++) { s = bf.readLine(); st = new StringTokenizer(s); for (int j = 0; j < N; j++) { arr[i][j] = Integer.parseInt(st.nextToken()); } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (isIn(i + M - 1, j + M - 1)) { findMax(i, j); } } } System.out.println("#" + t + " " + Max); Max = 0; } } static void findMax(int y, int x) { int sum = 0; for (int i = y; i < y + M; i++) { for (int j = x; j < x + M; j++) { sum += arr[i][j]; } } if (sum > Max) Max = sum; } static boolean isIn(int y, int x) { if (y > N || x > N || y < 0 || x < 0) return false; return true; } }
package day0803;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Q2001 {
static int M, N;
static int Max;
static int[][] arr;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int test = Integer.parseInt(bf.readLine());
for (int t = 1; t <= test; t++) {
String s = bf.readLine();
StringTokenizer st = new StringTokenizer(s);
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
arr = new int[N + 1][N + 1];
for (int i = 0; i < N; i++) {
s = bf.readLine();
st = new StringTokenizer(s);
for (int j = 0; j < N; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (isIn(i + M - 1, j + M - 1)) {
findMax(i, j);
}
}
}
System.out.println("#" + t + " " + Max);
Max = 0;
}
}
static void findMax(int y, int x) {
int sum = 0;
for (int i = y; i < y + M; i++) {
for (int j = x; j < x + M; j++) {
sum += arr[i][j];
}
}
if (sum > Max)
Max = sum;
}
static boolean isIn(int y, int x) {
if (y > N || x > N || y < 0 || x < 0)
return false;
return true;
}
}
'알고리즘' 카테고리의 다른 글
SWE 상호의 배트필드 (0) | 2021.08.06 |
---|---|
SWE 농작물 수확하기 (0) | 2021.08.06 |
프로그래머스 - 타겟 넘버(c++) (0) | 2021.04.21 |
백준 1916 최소 비용 구하기 (0) | 2021.04.16 |
백준 1700 멀티탭 스케줄링 (0) | 2021.04.06 |