hye-log

[SWEA]2001번 파리 퇴치(JAVA) 본문

CodingTest/SWEA

[SWEA]2001번 파리 퇴치(JAVA)

iihye_ 2023. 8. 6. 02:16

0. 문제 링크

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PzOCKAigDFAUq 

 

SW Expert Academy

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

swexpertacademy.com

 

1. 문제 설명

1) N*N 배열에서 M*M 크기의 파리채로 죽인 파리의 개수 구하기

 

2. 입출력

// input
10
5 2
1 3 3 6 7
8 13 9 12 8
4 16 11 12 6
2 4 1 23 2
9 13 4 7 3
6 3
29 21 26 9 5 8
21 19 8 0 21 19
9 24 2 11 4 24
19 29 1 0 21 19
10 29 6 18 4 3
29 11 15 3 3 29
...

// output
#1 49
#2 159
...

 

3. 코드

import java.util.Scanner;

public class swea2001 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt(); // 테스트 케이스의 개수
		for(int t = 1; t <= T; t++) {
			int N = sc.nextInt(); // 배열의 크기
			int M = sc.nextInt(); // 파리채의 크기
			int[][] map = new int[N][N]; // 배열
			for(int n = 0; n < N; n++) {
				for(int m = 0; m < N; m++) {
					map[n][m] = sc.nextInt();
				}
			}
			int ans = 0;
			
			for(int i = 0; i <= N-M; i++) {
				for(int j = 0; j <= N-M; j++) {
					int temp = 0; // 부분합
					for(int r = i; r < i+M; r++) { 
						for(int c = j; c < j+M; c++) {
							temp += map[r][c];
						}
					}
					ans = Math.max(temp, ans); // 최대값 찾기
				}
			}
		
			System.out.printf("#%d %d\n", t, ans);
		}
	}

}

 

실행 결과

 

4. 회고

1) 이중 for 문을 이용하여 배열의 합 구함

 

5. Github

https://github.com/iihye/Algorithm/blob/main/SWEA/swea2001.java

728x90
Comments