본문 바로가기
PS/BOJ

[백준] 4179. 불!(c++)

by backend 개발자 지망생 2025. 2. 24.

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

지훈이가 빠져나갈 수 있게,, bfs를 통해서 불이 지나간 자리보다 지훈이가 더 빨리 갈 수 있는 경우만 찾는 문제였다.

그럴 수 있으려면

1. 불이 지나간 자리를 bfs로 구해서 칸마다의 시간을 구한다.

2. 지훈이가 지나갈 자리가 불이 이미 지나쳤다면 건너뛰고 아닌 경우에만 진행해본다.

 


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <limits.h>
#include <queue>
#include <set>
#include <math.h>
#include <stack>
#include <deque>

using namespace std;

int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};

int arr[1001][1001] = {0};
int fire_visited[1001][1001] = {0};
int visited[1001][1001] = {0};

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

    int n, m, ans = 99999999;

    pair<int, int> fire;
    pair<int, int> jihun;

    cin >> n >> m;
    cin.ignore();

    queue<pair<int, int> > q;
    vector<string> arr_str(n);

    for (int i = 0; i < n; i++) {
        getline(cin, arr_str[i]);
        for (int j = 0; j < m; j++) {
            if (arr_str[i][j] == '#') {
                arr[i][j] = -1;
            } else if (arr_str[i][j] == '.') {
                arr[i][j] = 0;
            } else if (arr_str[i][j] == 'J') {
                arr[i][j] = 1;
                jihun = {i, j};
            } else {
                arr[i][j] = 2;
                q.push({i, j});
                fire_visited[i][j] = 1;
            }
        }
    }


    while (!q.empty()) {
        int y = q.front().first;
        int x = q.front().second;
        q.pop();

        for (int i = 0; i < 4; i++) {
            int ny = y + dy[i];
            int nx = x + dx[i];

            if (ny < 0 || ny >= n || nx < 0 || nx >= m) continue;
            if (fire_visited[ny][nx] || arr[ny][nx] == -1) continue;

            fire_visited[ny][nx] = fire_visited[y][x] + 1;
            q.push({ny, nx});
        }
    }

    q.push(jihun);
    visited[jihun.first][jihun.second] = 1;

    while (!q.empty()) {
        int y = q.front().first;
        int x = q.front().second;
        q.pop();

        for (int i = 0; i < 4; i++) {
            int ny = y + dy[i];
            int nx = x + dx[i];
            if (ny < 0 || ny >= n || nx < 0 || nx >= m) {
                ans = min(ans, visited[y][x] + 1);
                continue;
            }
            if (visited[ny][nx] || arr[ny][nx] == -1 || arr[ny][nx] == 2)
                continue;
            if (fire_visited[ny][nx] > visited[y][x] + 1 || !fire_visited[ny][nx]) {
                visited[ny][nx] = visited[y][x] + 1;
                q.push({ny, nx});
            }
        }
    }

    if (ans != 99999999)
        cout << ans - 1;
    else
        cout << "IMPOSSIBLE";

    return 0;
}

'PS > BOJ' 카테고리의 다른 글

[백준]1012. 유기농배추(c++)  (0) 2025.02.24
[백준] 1697. 숨바꼭질(c++)  (0) 2025.02.24
[백준] 7576. 토마토(c++)  (0) 2025.02.24
[백준] 2178. 미로탐색(c++)  (0) 2025.02.24
[백준] 1926. 그림(c++)  (0) 2025.02.24