Coding Test - cpp/BFS
[백준 16918: 봄버맨] - C++
에드윈H
2021. 3. 22. 20:43
#include<iostream>
#include<string>
#include<queue>
using namespace std;
string map[200][200];
int dir[4][2] = {
{-1,0}
,{0,1}
,{1,0}
,{0,-1}
};
int main() {
int r, c, n;
string input;
cin >> r >> c >> n;
//입력
for (int i = 0; i < r; i++)
{
cin >> input;
for (int j = 0; j < c; j++)
{
map[i][j] = input[j];
}
}
int curTime = 1; //첫 폭탄 1초 경과
queue<pair<int, int>> q;
while (curTime < n)
{
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if (map[i][j] == "O")
{
q.push({ i,j });
}
else
{
map[i][j] = "O";
}
}
}
if (n <= ++curTime) //새로운 폭탄 1초 경과
{
break;
}
while (!q.empty())
{
int curX = q.front().first;
int curY = q.front().second;
q.pop();
map[curX][curY] = '.';
for (int i = 0; i < 4; i++)
{
int nextX = curX + dir[i][0];
int nextY = curY + dir[i][1];
if (nextX < 0 || nextY < 0 || r <= nextX || c <= nextY)
continue;
if (map[nextX][nextY] == "O")
{
map[nextX][nextY] = '.';
}
}
}
curTime++; //놓여진 폭탄 터진 후 1초
}
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cout << map[i][j];
}
cout << "\n";
}
return 0;
}