Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- XR Interaction Toolkit
- 시뮬레이션
- 알고리즘
- VR
- 유니티
- 투 포인터
- 문자열
- BFS
- 다이나믹 프로그래밍
- 유니온 파인드
- 자료구조
- 정렬
- 그래프
- 누적 합
- 트리
- DFS
- 우선순위 큐
- 구현
- c++
- 재귀
- Team Fortress 2
- 그리디 알고리즘
- 다익스트라
- 백준
- 수학
- 브루트포스
- 스택
- 백트래킹
- ue5
- Unreal Engine 5
Archives
- Today
- Total
1일1알
백준 5427번 불 C++ 본문
불이 붙으려는 칸으로 이동할 수 없다고 했기 때문에 큐에 불을 먼저 넣고 상근이의 위치를 넣은 뒤 bfs탐색을 통해 풀었다.
#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 w, h;
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);
int t;
cin >> t;
while (t--) {
cin >> w >> h;
board = vector<vector<char>>(h, vector<char>(w));
found = vector<vector<bool>>(h, vector<bool>(w, false));
vector<pair<int, int>> fire;
pair<int, int> start;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> board[i][j];
if (board[i][j] == '*')
fire.push_back({ i,j });
else if (board[i][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 >= h || nextCol < 0 || nextCol >= w) {
if (curr.type == Player) {
ans = curr.moveCnt + 1;
loop = false;
break;
}
continue;
}
if (board[nextRow][nextCol] == '#') continue;
if (board[nextRow][nextCol] == '*') continue;
if (curr.type == Fire) board[nextRow][nextCol] = '*';
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";
}
}
'알고리즘' 카테고리의 다른 글
백준 17471번 게리맨더링 C++ (0) | 2022.08.20 |
---|---|
백준 2458번 키 순서 C++ (0) | 2022.08.19 |
백준 1062번 가르침 C++ (0) | 2022.08.17 |
백준 4485번 녹색 옷 입은 애가 젤다지? C++ (0) | 2022.08.16 |
백준 13913번 숨바꼭질 4 C++ (0) | 2022.08.15 |