PS

보물섬 [BOJ 2589]

깊게 생각하고 최선을 다하자 2022. 9. 10. 02:20

1. 문제에 대한 이해

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

- 문제가 무엇인가?

-> 보물이 묻혀 있는 두 곳 간의 최단 거리로 이동하는 시간을 구하라

 

- 문제가 무엇인가?

-> BFS를 어떻게 적용할 것인가?

-> BFS를 적용한다는 것이 무엇인가?

-> 

 

 

    

2. 계획

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

3. 실행

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



class Pair{
    int x; 
    int y;
    
    public Pair(int x, int y){
        this.x = x;
        this.y = y; 
    }
}

public class Main {
    
    public static int N;
    public static int M;
    public static char[][] map;
    public static int[][] count;
    public static int longestTime; 
    
    public boolean isInside(int x, int y){
        if(0<=x && x<N && 0<=y && y<M){
            return true;   
        }else{
            return false; 
        }
    }
    
    public static int[] dx = {-1, 1, 0, 0};
    public static int[] dy = {0, 0, -1, 1};
    
    public void BFS(int sx, int sy){
        
        Queue<Pair> q = new LinkedList<>();
        q.offer(new Pair(sx, sy));
        count[sx][sy] = 1;
        
        
        while(!q.isEmpty()){
            
            Pair curr = q.poll();
            int x = curr.x;
            int y = curr.y;
            
            for(int i=0; i<4; i++){
                int nx = x + dx[i];
                int ny = y + dy[i];
                
                if(isInside(nx, ny) && map[nx][ny] == 'L' && count[nx][ny] == 0){
                    count[nx][ny] = count[x][y] + 1;
                    q.offer(new Pair(nx, ny));
                }
            }
            
        }
        
        
        
        
        
    }
    
    public static int getLongestTime(){
        
        int longestTime = 0; 
        
        for(int i=0; i<N; i++){
            for(int j=0; j<M; j++){
                longestTime = Math.max(longestTime, count[i][j]);
            }
        }
        
        return longestTime;
    }
    
    
    
    public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      Main T = new Main();
      
      N = sc.nextInt();
      M = sc.nextInt();
      longestTime = Integer.MIN_VALUE; 
      map = new char[N][M];
      
      for(int i=0; i<N; i++){
          String str = sc.next();
          
          for(int j=0; j<M; j++){
              map[i][j] = str.charAt(j);
          }
      }
      
      
      for(int i=0; i<N; i++){
          for(int j=0; j<M; j++){
              if(map[i][j] == 'L'){
                  count = new int[N][M];
                  T.BFS(i, j);
                  
                  int answer = getLongestTime();
                  longestTime = Math.max(longestTime, answer);
              }
          }
      }
      
      System.out.println(longestTime-1);
      
      
    }
}

4. 반성

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

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

-> BFS를 여러 번 쓰는 것도 가능하다. 

-> -1을 써서 계산한 것이 키 포인트