카테고리 없음

백준 Java | BOJ #3055 탈출

토마토. 2023. 1. 31. 09:32
package P3055;

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

class Point{
    public int row;
    public int col;
    public String type;
    Point(int row, int col, String type){
        this.row=row;
        this.col=col;
        this.type=type;
    }

    @Override
    public String toString(){
        return "(" + this.row + ", " + this.col + ")";
    }
}
public class Main {
    static int R;
    static int C;
    static char[][] map;
    static int[][] visited;
    static int[] ROW = {-1, 1, 0, 0};
    static int[] COL = {0, 0, -1, 1};
    static int round;
    static Queue<Point> queue;
    static String ANIMAL = "gosum";
    static String WATER = "water";

    public static void main(String[] args) throws FileNotFoundException {
        System.setIn(new FileInputStream("src/P3055/input.txt"));
        Scanner scanner = new Scanner(System.in);
        R = scanner.nextInt();
        C = scanner.nextInt();
        map = new char[R][C];
        visited = new int[R][C];
        round = 0;
        queue = new LinkedList<>();

        Point gosum = null;
        for (int r=0;r<R;r++){
            String line = scanner.next();
            for (int c=0; c<C;c++){
                map[r][c] = line.charAt(c);
                if (map[r][c] == '*'){
                    queue.add(new Point(r, c, WATER));
                } else if (map[r][c] == 'S'){
                    gosum = new Point(r, c, ANIMAL);
                }

            }
        }
        queue.add(gosum);

        boolean answer = false;
        while (!queue.isEmpty()){
            // 1. queue에서 꺼냄
            Point current = queue.poll();
            // 2. 목적지인가-> 고슴이만 해당됨
            if (current.type.equals(ANIMAL) && map[current.row][current.col]=='D'){
                System.out.println(visited[current.row][current.col]);
                answer = true;
                break;
            }
            // 3. 연결된 곳 순회 -> 상하좌우
            for (int i=0;i<4;i++){
                int next_row = current.row + ROW[i];
                int next_col = current.col + COL[i];
                if (next_row >= 0 && next_row < R && next_col >= 0 && next_col < C){
                    // 4. 갈 수 있는가?
                    // 공통 -> 맵 범위
                    // 고슴이 -> D도 가능
                    if (current.type.equals(ANIMAL)){
                        if (map[next_row][next_col]=='.'||map[next_row][next_col]=='D'
                            && visited[next_row][next_col] == 0){
                            // 5. 체크인 (중복 방지)
                            // 고슴이 ->
                            visited[next_row][next_col] = visited[current.row][current.col] + 1;
                            // 6. 큐에 넣음
                            queue.add(new Point(next_row, next_col, ANIMAL));
                        }
                    } else {
                        // 물 -> .만 가능, S도 가능함
                        if (map[next_row][next_col]=='.' || map[next_row][next_col]=='S'){
                            // 5. 체크인 (중복 방지) - 물
                            map[next_row][next_col] = '*';
                            // 6. 큐에 넣음
                            queue.add(new Point(next_row, next_col, WATER));
                        }
                    }
                }
            }
        }

        if (!answer){
            System.out.println("KAKTUS");
        }
    }
}