본문 바로가기
SSAFY 6기/📌swexpert

swexpert : 2001. 파리퇴치

by IMSfromSeoul 2021. 8. 4.

🏆 문제정보

난이도 : D2 ( 하)

문제 유형 : 2차원 배열 시물레이션

📌 문제

N x N 배열 안에서 존재하는 M x M 영역안에 있는 최대 값을 구하라.

📌 생각

i ~ j loop 를 돌면서 i 방향으로 + m ; j 방향으로 + m 해서 확인한다.

oooooo

oooooo

oooooo

oooooo

 

이런식으로 사각형이 있다고 치면

 

ㅁㅁooooo

ㅁㅁooooo

oooooooo

oooooooo

 

네모 -> 확인하는 전체탐색 

쭉 확인하는 것

🔥 실수

int di = 0;
int dj;
// N +1 까지임
while(di+M<N+1){
    dj=0;
    while(dj+M<N+1){
        int sum = getSum(di, dj);
        max = Math.max(max,sum);
        dj+=1;
    }
    di+=1;
}

⚡️1. 범위를 제대로 구하지 않았다.

넘겨짚어서 di + M < N 이라고 정했는데, 그려서 확인해보니 N+1 까지해야 제대로 된 범위가 잡혔다.

 

⚡️2. dj -> 초기화 해주지 않았다.

🔥  실수잡기

실수는 할 수 밖에 없다.

실수를 하더라도, 실수한 부분을 빠르게 캐치하고 수정하는 것이 중요하다.

 

그러러면 모듈화가 돼있어야 한다.

input -> 제대로 들어오는지

로직 1

로직 2

로직 3

 

어떤 로직에서 에러가 나는지. 어떤 로직은 괜찮은 지 확실히 확인하면서 넘어가는게 좋다.

📌 코드

package src.swexpert.sol_210804.kill_fly;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

// 범위값 제대로 확인 안해서 헤맴
public class Solution {

    static int[][] map;
    static int N;
    static int M;

    public static void main(String[] args) throws FileNotFoundException {
        System.setIn(new FileInputStream("src/swexpert/sol_210804/kill_fly/input.txt"));

        Scanner sc = new Scanner(System.in);

        int T = sc.nextInt();

        for(int t=1;t<=T;t++){
            int max = 0;
            N = sc.nextInt();
            M = sc.nextInt();

            map = new int[N][N];

            for(int i=0;i<N;i++){
                for(int j=0;j<N;j++){
                    map[i][j] = sc.nextInt();
                }
            }
                int di = 0;
                int dj;
                // N +1 까지임
                while(di+M<N+1){
                    dj=0;
                    while(dj+M<N+1){
                        int sum = getSum(di, dj);
                        max = Math.max(max,sum);
                        dj+=1;
                    }
                    di+=1;
                }
            System.out.println("#" + t + " " + max);
        }
    }
    static int getSum(int di,int dj){
        int temp = 0;
        for(int i=di;i<di+M;i++){
            for(int j=dj;j<dj+M;j++){
                temp += map[i][j];
            }
        }
        return temp;
    }
}

 

 

'SSAFY 6기 > 📌swexpert' 카테고리의 다른 글

swexpert: 3499번 퍼펙트 셔플  (0) 2021.08.07
swexpert: 1861번 정사각형 방  (0) 2021.08.07
swexpert : 미로1  (0) 2021.08.07
snail - swexpert 문제  (0) 2021.07.24

댓글