Recent Posts
Notice
No Rules Rules
불 (feat. 백준, 5427번) 본문
728x90
반응형
불
https://www.acmicpc.net/problem/5427
반응형
// woohyeon.kim
// kim519620.tistory.com
#include <iostream>
#include <string.h>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int H, W, dx[4], dy[4];
char arr[1001][1001];
bool visit[1001][1001];
int ans[1001][1001];
vector<pair<int, int>> fires;
int bfs(register int x, register int y) {
memset(visit, false, sizeof(visit));
queue<pair<int, int>> q;
for (auto& fire : fires) {
q.push(fire);
visit[fire.first][fire.second] = true;
ans[fire.first][fire.second] = -1;
}
q.push(make_pair(x, y));
visit[x][y] = true;
ans[x][y] = 1;
if (x == 1 || x == H || y == 1 || y == W)
return ans[x][y];
while (!q.empty()) {
auto p = q.front(); q.pop();
x = p.first, y = p.second;
for (register int d = 0, nx, ny; d < 4; ++d) {
nx = x + dx[d], ny = y + dy[d];
if (1 <= nx && nx <= H && 1 <= ny && ny <= W && !visit[nx][ny] && arr[nx][ny] != '#') {
visit[nx][ny] = true;
ans[nx][ny] = ans[x][y];
if (ans[nx][ny] != -1)
ans[nx][ny] += 1;
if((nx == 1 || nx == H || ny == 1 || ny == W) && (ans[nx][ny] != -1))
return ans[nx][ny];
q.push(make_pair(nx, ny));
}
}
}
return -1;
}
int main() {
ios::sync_with_stdio(false), cin.tie(NULL);
dx[0] = 1, dx[1] = -1, dx[2] = dx[3] = 0;
dy[0] = dy[1] = 0, dy[2] = 1, dy[3] = -1;
register int T;
cin >> T;
for (register int t = 0, x, y; t < T; ++t) {
fires.clear();
cin >> W >> H;
for (register int h = 1, w; h <= H; ++h)
for (w = 1; w <= W; ++w) {
cin >> arr[h][w];
if (arr[h][w] == '@')
x = h, y = w;
else if (arr[h][w] == '*')
fires.push_back(make_pair(h, w));
}
auto ans = bfs(x, y);
if (ans < 0)
cout << "IMPOSSIBLE";
else
cout << ans;
cout << "\n";
}
return 0;
}
// *&)*@*
아래 "불!" 과 동일한 문제입니다.
728x90
반응형
'생활 > 코테' 카테고리의 다른 글
금고 테스트 (feat. 백준, 2266번) (0) | 2022.09.08 |
---|---|
Fly me to the Alpha Centauri (0) | 2022.09.08 |
불! (feat. 백준, 4179번) (0) | 2022.09.07 |
단어 수학 (feat. 백준, 1339번) (0) | 2022.09.07 |
신비로운 수 (feat. 백준, 17433번) (0) | 2022.09.06 |
Comments