문제링크 : https://www.acmicpc.net/problem/3055
3055번: 탈출
사악한 암흑의 군주 이민혁은 드디어 마법 구슬을 손에 넣었고, 그 능력을 실험해보기 위해 근처의 티떱숲에 홍수를 일으키려고 한다. 이 숲에는 고슴도치가 한 마리 살고 있다. 고슴도치는 제
www.acmicpc.net
접근 과정 :
- 일반적인 BFS 방식으로 접근
- 물이 찰 공간으로 고슴도치가 이동할 수 없다.
- 시간++ -> 물 이동 -> 고슴도치 이동 순으로 반복해서 가장 빠른 시간을 탐색
- 만약 고슴도치가 더 이상 이동할 수 없다면, 비버의 집에 갈 수 없다!!
소스 코드 및 결과 :
package BOJ;
/* 
    탈출 
*/
import java.io.*;
import java.util.*;
public class BOJ3055 {
    static int[][] move = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int R = Integer.parseInt(st.nextToken());
        int C = Integer.parseInt(st.nextToken());
        char[][] forest = new char[R][C];
        Queue<int[]> hedgehog = new LinkedList<>();
        Queue<int[]> water = new LinkedList<>();
        boolean[][] visit = new boolean[R][C];
        for (int i = 0; i < R; i++) {
            String input = br.readLine();
            for (int j = 0; j < C; j++) {
                forest[i][j] = input.charAt(j);
                if (input.charAt(j) == 'S') {
                    hedgehog.add(new int[] { i, j });
                    forest[i][j] = '.';
                    visit[i][j] = true;
                } else if (input.charAt(j) == '*') {
                    water.add(new int[] { i, j });
                }
            }
        }
        int count = 0;
        while (!hedgehog.isEmpty()) {
            count++;
            // 물 이동
            int size = water.size();
            while (size-- > 0) {
                int[] w = water.poll();
                for (int i = 0; i < 4; i++) {
                    int wr = w[0] + move[i][0];
                    int wc = w[1] + move[i][1];
                    if (wr < 0 || wr >= R || wc < 0 || wc >= C || forest[wr][wc] != '.') {
                        continue;
                    }
                    forest[wr][wc] = '*';
                    water.add(new int[] { wr, wc });
                }
            }
            // 고슴도치 이동
            size = hedgehog.size();
            while (size-- > 0) {
                int[] h = hedgehog.poll();
                for (int i = 0; i < 4; i++) {
                    int hr = h[0] + move[i][0];
                    int hc = h[1] + move[i][1];
                    if (hr < 0 || hr >= R || hc < 0 || hc >= C || visit[hr][hc] || forest[hr][hc] == '*'
                            || forest[hr][hc] == 'X') {
                        continue;
                    }
                    if (forest[hr][hc] == 'D') {
                        System.out.println(count);
                        return;
                    }
                    visit[hr][hc] = true;
                    hedgehog.add(new int[] { hr, hc });
                }
            }
        }
        System.out.println("KAKTUS");
    }
}
P.S
물이 없는 경우가 존재하나 봄
티떡숲에 홍수를 일으킨다는데..., 티떡숲 지도에 물이 주어지지 않다니...
'Algorithm' 카테고리의 다른 글
| [백준 Platinum 5] 6549 히스토그램에서 가장 큰 직사각형 - Java (0) | 2021.12.19 | 
|---|---|
| [백준 Silver 3] 5397 키로거 - Java (0) | 2021.12.18 | 
| [백준 Gold 4] 1600 말이 되고픈 원숭이 - Java (0) | 2021.12.18 | 
| [백준 Platinum 5] 3197 백조의 호수 - Java (0) | 2021.12.17 | 
| [백준 Silver 3] 15270 친구 팰린드롬 - Java (0) | 2021.12.16 | 
 
                    
                   
                    
                   
                    
                  