๊ณจ๋“œ4 : bfs ๋ฌธ์ œ์ด๋‹ค.

์ƒ๊ฐ

๋‹จ์ˆœํ•œ ๊ตฌํ˜„ ๋ฌธ์ œ์ด๋‹ค. ์ด ๋•Œ, ์ˆœ์„œ๋ฅผ ์ž˜ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. ๋จผ์ €, ๋ถˆ์„ ๋ฒˆ์ง€๊ฒŒ ํ•œ ์ƒํƒœ์—์„œ, ์‚ฌ๋žŒ์˜ ํ˜„์žฌ ์œ„์น˜๋กœ ๋ถ€ํ„ฐ ์–ด๋””๋กœ ๊ฐ€๋Š” ๊ฒƒ์ด ์ข‹์€์ง€๋ฅผ ํŒ๋‹จํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‚ฌ๋žŒ์ด ๊ฐ€์žฅ ์ž๋ฆฌ์— ๋„์ฐฉํ•œ๋‹ค๋ฉด, ๋‹ค์Œ๋ฒˆ์งธ์— ํƒˆ์ถœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

Code

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <string.h>
using namespace std;
// BFS๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถˆ์„ ๋ถ™์ธ๋‹ค.
// ๋ถˆ์„ ๋ถ™์ด๋Š” ํ–‰์œ„๋Š” ์–ธ์ œ ๊นŒ์ง€ ํ•˜๋ฉด ๋ผ? -> ํƒˆ์ถœํ•˜๋ฉด ๋ถˆ ์•ˆ๋ถ™์—ฌ๋„ ๋ผ
// ๊ทธ๋ ‡๋‹ค๋ฉด ์‚ฌ๋žŒ์ด ์›€์ง์ด๋Š” ๊ฒƒ์— ๋ถˆ์„ ๋ถ™์ด๋Š” ํ–‰์œ„๋Š” ์˜์กด์ 
// ๊ทธ๋Ÿผ q๋ฅผ ํ•˜๋‚˜๋งŒ ์‚ฌ์šฉํ•˜๋ฉด ๋ ๊นŒ?
// ์•„๋‹ˆ์•ผ ๋ถˆ์€ ๋”ฐ๋กœ ์›€์ง์—ฌ์•ผํ•ด
// ์ด๋ ‡๊ฒŒ ํ•˜์ž. ๋ถˆ์˜ ์‹œ์ž‘ ์œ„์น˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” q, ์‚ฌ๋žŒ์˜ ์‹œ์ž‘ ์œ„์น˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” q๋ฅผ ๋‘๊ฐœ๋ฅผ ๋งŒ๋“ค์ž.
// ๊ทธ๋ฆฌ๊ณ  ์‚ฌ๋žŒ q์•ˆ์ด ๋น„์–ด์žˆ์„ ๋•Œ๊นŒ์ง€ ๋ฃจํ”„๋ฅผ ๋Œ๋ฆฌ๋Š”๋ฐ, ๊ทธ๋•Œ ๋ถˆ q๋ฅผ ๋Œ๋ฆฌ์ž.
 
 
const int MAX = 1000;
struct Dir{
    int y;
    int x;
};
 
Dir moveDir[4] = { {0, 1}, {1, 0}, {0, -1}, {-1, 0}};
 
int W, H;
string graph[MAX];
bool visited[MAX][MAX];
 
pair<int, int> start;
vector<pair<int, int>> fire;
 
void printG(){
    for (int i = 0; i < H; i++) {
        cout << graph[i] << '\n';
    }
    cout << '\n';
}
 
int BFS(){
    int ans = 0;
    queue<pair<int, int>> q;
    queue<pair<int, int>> fire_q;
    q.push(start);
    for (int i = 0; i < int(fire.size()); i++) {
        fire_q.push(fire[i]);
    }
 
    while (!q.empty()) {
        // ๋ถˆ์„ ๋จผ์ € ๋ถ™์ธ๋‹ค.
        int fireSize = int(fire_q.size());
        for (int i = 0; i < fireSize; i++) {
            int y = fire_q.front().first;
            int x = fire_q.front().second;
            fire_q.pop();
 
            for (int j = 0; j < 4; j++) {
                int nextY = y + moveDir[j].y;
                int nextX = x + moveDir[j].x;
 
                if (0 <= nextY && nextY < H && 0 <= nextX && nextX < W){
                    if (graph[nextY][nextX] == '.') {
                        graph[nextY][nextX] ='*';
                        fire_q.push(make_pair(nextY, nextX));
                    }
                }
            }
        }
//        printG();
 
        // ์‚ฌ๋žŒ์„ ์ด๋™์‹œํ‚จ๋‹ค.
        int manSize = int(q.size()); //  ์™€ ์ด๊ฑฐ ์•ˆํ•ด์ฃผ๋ฉด q๊ฐ€ ๋™์ ์œผ๋กœ ํฌ๊ธฐ๊ฐ€ ๋ฐ”๋€Œ์–ด์„œ ์ด์ƒํ•˜๊ฒŒ๋จ..
        for (int i = 0; i < manSize; i++) {
            int y = q.front().first;
            int x = q.front().second;
            q.pop();
 
            // ๊ฐ€์žฅ ์ž๋ฆฌ์— ์žˆ์„ ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ๋ฒˆ์— ํƒˆ์ถœ์ด๋‹ค.
            if (y == 0 || y == H-1 || x == 0 || x == W-1)
                return ans + 1;
 
            for (int i = 0; i < 4; i++) {
                int nextY = y + moveDir[i].y;
                int nextX = x + moveDir[i].x;
 
                if (0 <= nextY && nextY < H && 0 <= nextX && nextX < W){
                    if (visited[nextY][nextX] == false && graph[nextY][nextX] != '*' && graph[nextY][nextX] != '#') {
                        visited[nextY][nextX] = true;
                        q.push(make_pair(nextY, nextX));
                    }
 
                }
            }
 
 
        }
        ans++;
//        printG();
    }
    return -1;
}
 
 
 
 
int main(){
    int T;
    cin >> T;
 
    for (int tc = 0; tc < T; tc++) {
        // tc ๋ฌธ์ œ์—์„œ๋Š” ๋ฐฐ์—ด์„ ์ดˆ๊ธฐํ™” ํ•ด์ฃผ๋Š” ๊ฒƒ์„ ๊นŒ๋จน์œผ๋ฉด ์•ˆ๋œ๋‹ค.
        fire.clear();
        memset(visited, false, sizeof(visited));
 
        cin >> W >> H;
 
        for (int i = 0; i < H; i++) {
            cin >> graph[i];
            for (int j = 0; j < W; j++) {
                if (graph[i][j] == '@') {
                    start = make_pair(i, j);
                    visited[i][j] = true;
                }
                else if (graph[i][j] == '*') fire.push_back(make_pair(i, j));
            }
        }
 
        int ans = BFS();
 
        if (ans == -1) cout << "IMPOSSIBLE" << '\n';
        else cout << ans << '\n';
 
    }
 
    return 0;
}
 

Reference