[코딩 테스트 RUN] SWEA 수업 : 9490. 풍선팡

코테런/SWEA(알고리즘 수업)
2026.03.09

 

⚠️ 주의!
SWEA에서 낸 코딩 문제에 대한 해답이 들어있습니다.
열람 시 주의해주세요.

 

 

[SW Expert Academy] 9490. 풍선팡

 

SW Expert Academy

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

swexpertacademy.com

 

 

파리퇴치를 먼저 하지 않았다면 좋았을 텐데 싶은 문제.

파리퇴치에서 대각선만 고려하지 않은 방식이면 된다.

 

파리퇴치 풀이는 아래에서 확인.

https://bbbbabbbababababa.tistory.com/110

 

[코딩 테스트 RUN] SWEA 수업 : 12712. 파리퇴치3

⚠️ 주의!SWEA에서 낸 코딩 문제에 대한 해답이 들어있습니다.열람 시 주의해주세요. [SW Expert Academy] 12712. 파리퇴치3 SW Expert AcademySW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를

bbbbabbbababababa.tistory.com

 

 

 

정답

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

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());
        StringBuilder sb = new StringBuilder();

        for(int test_case = 1; test_case <= T; test_case++) {
            StringTokenizer nm = new StringTokenizer(br.readLine());
            int N = Integer.parseInt(nm.nextToken());
            int M = Integer.parseInt(nm.nextToken());

            int[][] arr = new int[N][M];

            for (int i = 0; i < N; i++) {
                StringTokenizer st = new StringTokenizer(br.readLine());
                for (int j = 0; j < M; j++) {
                    arr[i][j] = Integer.parseInt(st.nextToken());
                }
            }
            // 세팅 완료

            // 하, 우, 상, 좌
            // 파리퇴치를 먼저 해버려서 파리퇴치에서 조금만 수정해도 됨..
            int[] dx = { 0, 1, 0, -1 };
            int[] dy = { 1, 0, -1, 0 };
            int maxScore = 0;
            for(int x = 0 ; x < N; x++){
                for (int y = 0; y < M; y++) {
                    int crossScore = arr[x][y];
                    for (int i = 0; i < 4; i++) {
                        for (int j = 1; j <= arr[x][y]; j++) {
                            int nx = x + dx[i] * j;
                            int ny = y + dy[i] * j;
                            if(nx >= 0 && nx < N && ny >= 0 && ny < M){
                                crossScore += arr[nx][ny];
                            }
                        }
                    }

                    maxScore = Math.max(maxScore,crossScore);
                }
            }

            sb.append("#").append(test_case).append(" ").append(maxScore).append("\n");
        }

        System.out.print(sb);
    }
}