알고리즘
백준 4179번 불! C++
영춘권의달인
2022. 8. 21. 09:31
https://kjhcocomi.tistory.com/301
백준 5427번 불 C++
불이 붙으려는 칸으로 이동할 수 없다고 했기 때문에 큐에 불을 먼저 넣고 상근이의 위치를 넣은 뒤 bfs탐색을 통해 풀었다. #include #include #include #include #include #include #include #include #include..
kjhcocomi.tistory.com
이문제랑 거의 똑같다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int r, c;
vector<vector<char>> board;
vector<vector<bool>> found;
int dRow[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };
enum Type {
Player,
Fire
};
struct Info {
int row;
int col;
int moveCnt;
Type type;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> r >> c;
board = vector<vector<char>>(r, vector<char>(c));
found = vector<vector<bool>>(r, vector<bool>(c, false));
vector<pair<int, int>> fire;
pair<int, int> start;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> board[i][j];
if (board[i][j] == 'F')
fire.push_back({ i,j });
else if (board[i][j] == 'J')
start = { i,j };
}
}
queue<Info> q;
for (auto a : fire) {
q.push({ a.first,a.second,0,Fire });
}
q.push({ start.first,start.second,0,Player });
found[start.first][start.second] = true;
int ans = -1;
bool loop = true;
while (!q.empty()) {
auto curr = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int nextRow = curr.row + dRow[i];
int nextCol = curr.col + dCol[i];
if (nextRow < 0 || nextRow >= r || nextCol < 0 || nextCol >= c) {
if (curr.type == Player) {
ans = curr.moveCnt + 1;
loop = false;
break;
}
continue;
}
if (board[nextRow][nextCol] == '#') continue;
if (board[nextRow][nextCol] == 'F') continue;
if (curr.type == Fire) board[nextRow][nextCol] = 'F';
else {
if (found[nextRow][nextCol]) continue;
found[nextRow][nextCol] = true;
}
q.push({ nextRow,nextCol,curr.moveCnt + 1,curr.type });
}
if (!loop) break;
}
if (ans == -1) cout << "IMPOSSIBLE";
else cout << ans;
cout << "\n";
}