Post

[BaekJoon] 5427번 - 불 [Java][C++]

[BaekJoon] 5427번 - 불 [Java][C++]

문제 링크


1. 아이디어


최단 거리 BFS를 활용하면 해결할 수 있는 문제로 BFS로 상근이를 이동시키면서 동시에 불도 이동을 시켜주는 방식으로 해결했다. 불이 붙으려는 칸으로도 이동할 수 없으므로 불을 먼저 이동시키고 상근이를 이동시켜줬다.


2. 코드


1. 풀이 [Java]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.io.*;
import java.util.*;

public class Main {

    static int[] dr = {-1, 0, 1, 0};
    static int[] dc = {0, 1, 0, -1};
    static int h, w;
    static char[][] grid;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        StringTokenizer st;

        int t = Integer.parseInt(br.readLine());
        while (t-- > 0) {
            st = new StringTokenizer(br.readLine());
            w = Integer.parseInt(st.nextToken());
            h = Integer.parseInt(st.nextToken());

            grid = new char[h][w];
            int sr = -1;
            int sc = -1;
            Queue<int[]> q = new ArrayDeque<>();

            for (int i = 0; i < h; i++) {
                grid[i] = br.readLine().toCharArray();
                for (int j = 0; j < w; j++) {
                    if (grid[i][j] == '@') {
                        sr = i;
                        sc = j;
                    } else if (grid[i][j] == '*') {
                        q.offer(new int[]{i, j});
                    }
                }
            }

            int res = bfs(sr, sc, q);
            if (res == -1) {
                sb.append("IMPOSSIBLE\n");
            } else {
                sb.append(res).append("\n");
            }
        }

        System.out.println(sb);
    }

    static int bfs(int sr, int sc, Queue<int[]> fire) {
        Queue<int[]> q = new ArrayDeque<>();
        q.offer(new int[]{sr, sc});

        boolean[][] vis = new boolean[h][w];
        vis[sr][sc] = true;

        int dist = 0;

        while (!q.isEmpty()) {
            int sz = fire.size();

            while (sz-- > 0) {
                int[] cur = fire.poll();

                for (int d = 0; d < 4; d++) {
                    int nr = cur[0] + dr[d];
                    int nc = cur[1] + dc[d];

                    if (nr < 0 || nr >= h || nc < 0 || nc >= w) continue;
                    if (grid[nr][nc] == '#' || grid[nr][nc] == '*') continue;

                    fire.offer(new int[]{nr, nc});
                    grid[nr][nc] = '*';
                }
            }

            sz = q.size();
            while (sz-- > 0) {
                int[] cur = q.poll();
                if (cur[0] == 0 || cur[0] == h - 1 || cur[1] == 0 || cur[1] == w - 1) return dist + 1;

                for (int d = 0; d < 4; d++) {
                    int nr = cur[0] + dr[d];
                    int nc = cur[1] + dc[d];

                    if (nr < 0 || nr >= h || nc < 0 || nc >= w) continue;
                    if (grid[nr][nc] != '.' || vis[nr][nc]) continue;

                    q.offer(new int[]{nr, nc});
                    vis[nr][nc] = true;
                }
            }

            dist++;
        }

        return -1;
    }
}


2. 풀이 [C++]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <bits/stdc++.h>
using namespace std;

int dr[4] = {-1, 0, 1, 0};
int dc[4] = {0, 1, 0, -1};
int h, w;
char grid[1001][1001];
bool vis[1001][1001];

int bfs(int sr, int sc, queue<pair<int, int>>& fire) {
    queue<pair<int, int>> q;
    q.push({sr, sc});

    vis[sr][sc] = true;

    int dist = 0;

    while (!q.empty()) {
        int sz = fire.size();

        while (sz--) {
            auto [r, c] = fire.front();
            fire.pop();

            for (int d = 0; d < 4; d++) {
                int nr = r + dr[d];
                int nc = c + dc[d];

                if (nr < 0 || nr >= h || nc < 0 || nc >= w) continue;
                if (grid[nr][nc] == '#' || grid[nr][nc] == '*') continue;

                fire.push({nr, nc});
                grid[nr][nc] = '*';
            }
        }

        sz = q.size();
        while (sz--) {
            auto [r, c] = q.front();
            q.pop();

            if (r == 0 || r == h - 1 || c == 0 || c == w - 1) return dist + 1;

            for (int d = 0; d < 4; d++) {
                int nr = r + dr[d];
                int nc = c + dc[d];

                if (nr < 0 || nr >= h || nc < 0 || nc >= w) continue;
                if (grid[nr][nc] != '.' || vis[nr][nc]) continue;

                q.push({nr, nc});
                vis[nr][nc] = true;
            }
        }

        dist++;
    }

    return -1;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;

    while (t--) {
        cin >> w >> h;

        memset(vis, 0, sizeof(vis));

        int sr, sc;
        queue<pair<int, int>> q;

        for (int i = 0; i < h; i++) {
            cin >> grid[i];
            for (int j = 0; j < w; j++) {
                if (grid[i][j] == '@') {
                    sr = i;
                    sc = j;
                } else if (grid[i][j] == '*') {
                    q.push({i, j});
                }
            }
        }

        int res = bfs(sr, sc, q);
        if (res == -1) {
            cout << "IMPOSSIBLE\n";
        } else {
            cout << res << '\n';
        }
    }
}

3. 디버깅


없음.


4. 참고


없음.


This post is licensed under CC BY 4.0 by the author.