본문 바로가기
알고리즘 문제/구현

[개쉬운 풀이] 백준 16918 봄버맨 CPP (16일차)

by odaebum 2024. 12. 7.
728x90

문제

 

생각

1. 처음에는 3초마다 행동이 진행되는 줄 알았다.

2. 그러나 2초간격으로 생각하는 것이 맞다.

3. 봄버맨은 2초에 한번씩 폭탄을 배치하고

4. 폭탄은 3초에 한번씩 터진다.

5. 따라서 매 time이 증가할때마다 폭탄이 터지는지 안터지는지 확인하면서,  2초에 한번씩 설치하면 된다.

 

풀이

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

const int MAX = 201;

int R, C, N;
int map[MAX][MAX];
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};

void input(){
    cin >> R >> C >> N;
    for(int i = 0; i < R; i++){
        for(int j = 0; j < C; j++){
            char c;
            cin >> c;
            if(c == 'O') {
                map[i][j] = 1;
            }
        }
    }
}

void plant_bombs(){
    for(int i = 0; i < R; i++){
        for(int j = 0; j < C; j++){
            if(map[i][j] == 0) map[i][j] = 1;
        }
    }
}

void print(){
    for(int i = 0; i < R; i++){
        for(int j = 0; j < C; j++){
            if(map[i][j] == 0) cout << '.';
            else cout << 'O';
        }
        cout << endl;
    }
}

void explosion(){
    queue<pair<int,int>> bombs;
    for(int i = 0; i < R; i++){
        for(int j = 0; j < C; j++){
            if(map[i][j] != 0) map[i][j] += 1;
            if(map[i][j] == 3) bombs.push(make_pair(i,j));
        }
    }

    while(!bombs.empty()){
        auto pos = bombs.front();
        bombs.pop();
        map[pos.first][pos.second] = -1;

        for(int i = 0; i < 4; i++){
            int nx = pos.first + dx[i];
            int ny = pos.second + dy[i];

            if(nx < 0 || ny < 0 || nx >= R || ny >= C) continue;
            map[nx][ny] = -1;
        }
    }
}

void sol(){
    int time = 0;
    while(time < N){
        time++;
        explosion();
        if(time % 2 == 0) plant_bombs();
        //cout << time << endl;
        //print();
    }
}

int main(){
    input();
    sol();
    print();
    return 0;
}
728x90