ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [5427] 불
    BOJ 2021. 10. 19. 12:06

    https://www.acmicpc.net/problem/5427

     

    5427번: 불

    상근이는 빈 공간과 벽으로 이루어진 건물에 갇혀있다. 건물의 일부에는 불이 났고, 상근이는 출구를 향해 뛰고 있다. 매 초마다, 불은 동서남북 방향으로 인접한 빈 공간으로 퍼져나간다. 벽에

    www.acmicpc.net

    <문제>

    "[4179] 불!"이랑 사실상 똑같은 문제다. bfs를 두 개 돌려주면 되는데,

    첫 번째에는 불을 기준으로 해당 정점까지의 최단거리를 저장해둔다. 도달하지 못하는 정점은 INF를 넣는다.

    두 번째에는 "현재까지의 턴 + 1" < "불의 최단거리"를 만족하는 조건을 추가로 달아 상근이를 이동시켜주면 된다.

     

    <소스코드>

    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
    #include <bits/stdc++.h>
    using namespace std;
    const int INF = 987654321;
    typedef struct {
        int y, x;
    } state;
    int n, m, jy, jx, dp[1001][1001], dy[4= {010-1}, dx[4= {10-10};
    char a[1001][1001];
    bool visited[1001][1001];
    int main(void) {
        int i, j, t;
        cin >> t;
        while (t--) {
            cin >> m >> n;
            queue<state> q;
            fill(&dp[0][0], &dp[1000][1000], INF);
            memset(visited, falsesizeof(visited));
            memset(a, 0sizeof(a));
            for (i = 0; i < n; i++) {
                for (j = 0; j < m; j++) {
                    cin >> a[i][j];
                    if (a[i][j] == '*') {
                        q.push({i, j});
                        dp[i][j] = 0;
                    } else if (a[i][j] == '@') {
                        jy = i;
                        jx = j;
                    }
                }
            }
            while (!q.empty()) {
                state cur = q.front();
                q.pop();
                for (i = 0; i < 4; i++) {
                    int y = cur.y + dy[i];
                    int x = cur.x + dx[i];
                    if (y < 0 || x < 0 || y >= n || x >= m) continue;
                    if (dp[y][x] == INF && a[y][x] != '#') {
                        dp[y][x] = dp[cur.y][cur.x] + 1;
                        q.push({y, x});
                    }
                }
            }
            typedef struct {
                int y, x, turn;
            } st;
            queue<st> Q;
            Q.push({jy, jx, 0});
            visited[jy][jx] = true;
            while (!Q.empty()) {
                st cur = Q.front();
                Q.pop();
                if (cur.y == 0 || cur.y == n - 1 || cur.x == 0 || cur.x == m - 1) {
                    cout << cur.turn + 1 << '\n';
                    goto end;
                }
                for (i = 0; i < 4; i++) {
                    int y = cur.y + dy[i];
                    int x = cur.x + dx[i];
                    if (y < 0 || x < 0 || y >= n || x >= m) continue;
                    if (visited[y][x] == false && a[y][x] != '#' &&
                        dp[y][x] > cur.turn + 1) {
                        Q.push({y, x, cur.turn + 1});
                        visited[y][x] = true;
                    }
                }
            }
            cout << "IMPOSSIBLE\n";
        end:;
        }
        return 0;
    }
    cs

     

    'BOJ' 카테고리의 다른 글

    [16920] 확장 게임  (0) 2021.10.19
    [6593] 상범 빌딩  (0) 2021.10.19
    [1213] 팰린드롬 만들기  (0) 2021.10.19
    [2502] 떡 먹는 호랑이  (0) 2021.10.17
    [3197] 백조의 호수  (0) 2021.10.17

    댓글