Fire! UVA - 11624
题目链接:https://vjudge.net/problem/UVA-11624
题意:一个迷宫,可能有一个或者多个地方着火了,每过1个时间消耗,火会向四周蔓延,问Joe能不能逃出迷宫,只要走出迷宫边界就算逃出,火和Joe都不能透过墙。
思路:人和火源分别跑bfs,人一张地图,火源一张地图,跑各自能到达点的时间,火源可能有多个,最后只需要判断迷宫的四个边中人和火源的时间消耗来得出最小答案,出不去输出“IMPOSSIBLE”,思路比较简单,代码稍微复杂点。
#include <iostream>
#include <cstring>
#include<vector>
#include<string>
#include <cmath>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
#define inf (1LL << 31) - 1
#define rep(i,j,k) for(int i = (j); i <= (k); i++)
#define rep__(i,j,k) for(int i = (j); i < (k); i++)
#define per(i,j,k) for(int i = (j); i >= (k); i--)
#define per__(i,j,k) for(int i = (j); i > (k); i--)
const int N = 1010;
int mv_x[] = { 0, 0, -1, 1 };
int mv_y[] = { 1, -1, 0, 0 };
char mp[N][N];
int Fire[N][N]; //火
bool vis[N][N];
int x[N]; //火源的x
int y[N]; //火源的y
int l; //火源的个数
int Joe[N][N]; //Joe
int n, m;
int pi, pj; //Joe的坐标
struct node{
int x, y, v;
};
inline void init(){
rep(i, 1, n) rep(j, 1, m){
Joe[i][j] = 0;
Fire[i][j] = inf;
}
}
inline void input(){
l = 0;
rep(i, 1, n) rep(j, 1, m){
cin >> mp[i][j];
//记录每个火源
if (mp[i][j] == 'J') pi = i, pj = j;
else if (mp[i][j] == 'F') x[l] = i, y[l++] = j;
}
}
inline bool check(int x, int y){
return x >= 1 && x <= n && y >= 1 && y <= m;
}
void bfs_p(){
queue<node> que;
Joe[pi][pj] = 1;
que.push(node{ pi, pj, 1 });
while (!que.empty()){
node tmp = que.front();
que.pop();
rep__(p, 0, 4){
int dx = tmp.x + mv_x[p];
int dy = tmp.y + mv_y[p];
if (check(dx, dy) && !Joe[dx][dy] && mp[dx][dy] != '#'){
Joe[dx][dy] = tmp.v + 1;
que.push(node{ dx, dy, tmp.v + 1 });
}
}
}
}
void bfs_f(int fi, int fj){
rep(i, 1, n) rep(j, 1, m) vis[i][j] = 0;
queue<node> que;
Fire[fi][fj] = 1;
vis[fi][fj] = true;
que.push(node{ fi, fj, 1 });
while (!que.empty()){
node tmp = que.front();
que.pop();
rep__(p, 0, 4){
int dx = tmp.x + mv_x[p];
int dy = tmp.y + mv_y[p];
if (check(dx, dy) && !vis[dx][dy] && mp[dx][dy] != '#'){
vis[dx][dy] = true;
if (tmp.v + 1 < Fire[dx][dy]) //比较与之前的火源,哪个最先烧到这个点
Fire[dx][dy] = tmp.v + 1, que.push(node{ dx, dy, tmp.v + 1 });
// cout << "Fire[][] " << Fire[dx][dy] << endl;
}
}
}
}
void search_fire(){
rep__(i, 0, l){
bfs_f(x[i], y[i]);
}
}
void get_ans(){
int ans = inf;
//先把Fire地图所有的inf,也就是无法到达的点赋值为0,方便比较
rep(i, 1, n) rep(j, 1, m) if (Fire[i][j] == inf) Fire[i][j] = 0;
//一种情况,Joe到达某点时间比Fire短,
//另一个情况,可能Fire到达不了那个点,于是Fire[x][y] == 0,所有有个特殊判断 Fire[x][y] == 0
//下面就是四个边界情况了
rep(i, 1, n){
if (i == 1 || i == n){
rep(j, 1, m){
if (Joe[i][j] < Fire[i][j] || (Fire[i][j] == 0 && Joe[i][j] != 0))
ans = min(ans, Joe[i][j]);
}
}
else {
if (Joe[i][1] < Fire[i][1] || (Fire[i][1] == 0 && Joe[i][1] != 0))
ans = min(ans, Joe[i][1]);
if (Joe[i][m] < Fire[i][m] || (Fire[i][m] == 0 && Joe[i][m] != 0))
ans = min(ans, Joe[i][m]);
}
}
if (ans == inf) cout << "IMPOSSIBLE" << endl;
else cout << ans << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
rep(i, 1, T){
cin >> n >> m;
init(); //初始化
input(); //输入
bfs_p(); //Joe的bfs
search_fire(); //所有火源的bfs
get_ans(); //得到答案
}
return 0;
}