본문 바로가기

PS

치즈 [BOJ 2636]

1. 문제에 대한 이해

  • 우리가 풀어야 할 문제는 무엇인가?
  • 주어진 자료는 무엇인가?
  • 조건은 무엇인가?
  • 우리가 문제를 풀기 위해 주어진 자료가 충분한가?
  • 숨겨진 조건이나 자료가 있는가? 그렇다면 그 것을 다른 방법으로 해석해보라. 

    

2. 계획

  • 전에 비슷한 문제를 알고 있는가?
  • 이 문제를 푸는데 있어서 유용하게 쓸 수 있는 지식은 무엇인가?
  • 비슷한 문제를 풀어본 적이 있다면 그 것을 활용할 수 있는가?
  • 만약 문제를 풀 수 없다면 문제를 더 단순하게 하기 위해서 주어진 조건을 버려보아라
  • 주어진 자료로부터 유용한 것을 이끌어 낼 수 있는가?
  • 자료는 모두 사용했는가?
  • 조건을 모두 사용했는가?
  • 문제에 포함된 핵심적인 개념은 모두 고려했는가?

3. 실행

  • 풀이 계획을 실행하고, 각 단계가 올바른지 점검하라.
import java.util.*;

public class Main {
    
    public static int N;
    public static int M;
    public static int[][] map;
    public static boolean[][] visited;
    public static int[] dx = {-1, 1, 0, 0};
    public static int[] dy = {0, 0, -1, 1};
    
    public boolean isInside(int x, int y){
        if(0<=x && x <N && 0<=y && y<M){
            return true;
        }else{
            return false;
        }
    }
    
    
    public boolean checkMap(){
        
        for(int i=0; i<N; i++){
            for(int j=0; j<M; j++){
                if(map[i][j] != 0){
                    return false;
                }
            }
        }
        
        return true;
        
    }
    
    public void dfs(int x, int y){
        
        visited[x][y] = true;
        
        for(int i=0; i<4; i++){
            int nx = x + dx[i];
            int ny = y + dy[i];
            
            if(isInside(nx, ny) && visited[nx][ny] == false && map[nx][ny] == 0){
                dfs(nx, ny);
            }
        }
    }
    
    public void markHole(){
           
          for(int i=0; i<N; i++){
              for(int j=0; j<M; j++){
                  if(visited[i][j] == false && map[i][j] == 0){
                      map[i][j] = 2;
                  }
              }
          }
    }
    
    public boolean checkMeltable(int x, int y){
        
        for(int i=0; i<4; i++){
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(map[nx][ny] == 0){
                return true;
            }
        }
        
        return false; 
        
    }
    
    
    public void initMap(){
        
        
        for(int i=0; i<N; i++){
            for(int j=0; j<M; j++){
                if(map[i][j] == 2 || map[i][j] == 3){
                    map[i][j] = 0; 
                }
            }
        }
    }
    
    
    
    public static void main(String args[]) {
      
      Scanner sc = new Scanner(System.in);
      Main T = new Main();
      N = sc.nextInt();
      M = sc.nextInt();
      map = new int[N][M];
      
      int time = 0; 
      ArrayList<Integer> meltedCheeses = new ArrayList<>();
      
      
      for(int i=0; i<N; i++){
          for(int j=0; j<M; j++){
              int num = sc.nextInt();
              map[i][j] = num;
          }
      }
      
      
      
      while(true){
          
          boolean allZero = T.checkMap();
          
          if(allZero){
              break;
          }
          
          visited = new boolean[N][M];
          
          T.dfs(0, 0);
          
          T.markHole();
          
          int meltedCheese = 0; 
          
          for(int i=0; i<N; i++){
              for(int j=0; j<M; j++){
                  if(map[i][j] == 1){
                   boolean result = T.checkMeltable(i, j);   
                   
                   if(result){
                       meltedCheese++;
                       map[i][j] = 3; 
                   }
                   
                   
                  }
              }
          }
          
          meltedCheeses.add(meltedCheese);
          
          T.initMap();
          
          time++; 
          
          
      } 
     
      
      System.out.println(time);
      System.out.println(meltedCheeses.get(meltedCheeses.size()-1));
      
    }
}

4. 반성

  • 문제를 다른 방식으로 해결할 수 있는가?
  • 결과나 방법을 어떤 다른 문제에 활용할 수 있는가?
  • 어떻게 하면 더 효율적으로 문제를 해결할 수 있는가?
  • 어떻게 하면 더 효과적으로 문제를 해결할 수 있는가?

- 어떻게 하면 더 효율적으로 문제를 해결할 수 있는가?

-> 문제 정의 과정이 중요하다

-> 문제에 주어진 조건을 잘 이해하는 것이 중요하다

-> 그리고 한 번에 제대로 된 설계를 하는 것이 중요하다

-> 문제에 대한 설계를 할 때, 에지 케이스를 잘 고려하는 것이 중요하다

-> 이 역량이 매우 매우 중요하다

 

- 골드 4문제이고, 푸는데 1시간 10분 정도 소요되었다.

-> 문제를 체계적인 방법에 의해 접근하도록 하자. 

'PS' 카테고리의 다른 글

수학숙제 [BOJ 2870]  (0) 2022.09.05
비밀번호 발음하기 [BOJ 4659]  (0) 2022.09.05
사과 담기 게임 [BOJ 2828]  (0) 2022.09.04
연구소 [BOJ 14502]  (0) 2022.09.04
쿼드 트리 [BOJ 1992]  (0) 2022.09.04