题面:
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze. Given Joe’s location in the maze and which squares of the maze are on fire, you must determine whether Joe can exit the maze before the fire reaches him, and how fast he can do it. Joe and the fire each move one square per minute, vertically or horizontally (not diagonally). The fire spreads all four directions from each square that is on fire. Joe may exit the maze from any square that borders the edge of the maze. Neither Joe nor the fire may enter a square that is occupied by a wall.
Input:
The first line of input contains a single integer, the number of test cases to follow. The first line of each test case contains the two integers R and C, separated by spaces, with 1 ≤ R,C ≤ 1000. The following R lines of the test case each contain one row of the maze. Each of these lines contains exactly C characters, and each of these characters is one of:
• #, a wall
• ., a passable square
• J, Joe’s initial position in the maze, which is a passable square
• F, a square that is on fire
There will be exactly one J in each test case.
Output:
For each test case, output a single line containing ‘IMPOSSIBLE’ if Joe cannot exit the maze before the fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.
Sample Input:
2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F
Sample Output:
3
IMPOSSIBLE
分析:
J是Joe所在的地方,F是起火的地方,题意大致是迷宫里有些地点起火了,火势会向四联通方向扩大,而且其后被点燃的地方还会作为中心继续往外扩大火势,一个恐怖的连锁作用,和BFS的操作刚好也是一样的,Joe呢想要逃出去,很容易想到只要Joe跑到迷宫边缘还没被烧成烤肉他就绝地求生成功了。
很有意思的一个题,要进行两次BFS,先对火进行BFS预处理出每个地点被套上点燃(不)的时间,然后对人进行BFS看到底能不能大吉大利今晚吃鸡。读题弱再次遇到了个坑,就是起火点可能不止一个的!!(划重点) ,最开始想到的是最简单粗暴的方法,也就是每个起火点做一个BFS,果然不负众望地TLE了……后来参考了别人的题解……!!把queue开成全局变量,先把所有起火点入队,然后一次性来做BFS!妙啊。WA了一晚上最后终于AC了。
发现有时候代码长不一定就很难,以前看见很长的代码习惯性先怂为敬……诶以后要学会硬着头皮看长代码了。
AC代码:
#include
#include
#include
#include
using namespace std;
int t, r, c;
const int maxn = 1005;
const int inf = 0x3f3f3f3f;
char maze[maxn][maxn];
int fire[maxn][maxn], dis[maxn][maxn];
bool book[maxn][maxn], flag;
int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
struct node{
int x, y;
};
queueq;
bool law(int x, int y){
if(x < 0 || x >= r || y < 0 || y >= c || book[x][y]) return false;
return true;
}
void BFSfire(){
node front;
while(!q.empty()){
front = q.front();
q.pop();
for(int i = 0; i < 4; i++){
node next = front;
next.x += dir[i][0];
next.y += dir[i][1];
if(law(next.x, next.y) && (maze[next.x][next.y] == '.' || maze[next.x][next.y] == 'J')){
q.push(next);
book[next.x][next.y] = true;
if(fire[next.x][next.y] > fire[front.x][front.y] + 1)
fire[next.x][next.y] = fire[front.x][front.y] + 1;
}
}
}
}
int BFShuman(int bx, int by){
while(!q.empty()) q.pop();
node front;
front.x = bx; front.y = by;
q.push(front);
book[bx][by] = true;
dis[bx][by] = 0;
while(!q.empty()){
front = q.front();
q.pop();
if(front.x == 0 || front.x == r-1 || front.y == 0 || front.y == c-1) {
flag = true;
return dis[front.x][front.y];
}
for(int i = 0; i < 4; i++){
node next = front;
next.x += dir[i][0];
next.y += dir[i][1];
if(law(next.x, next.y) && maze[next.x][next.y] == '.'){
dis[next.x][next.y] = dis[front.x][front.y] + 1;
if(dis[next.x][next.y] < fire[next.x][next.y]){
book[next.x][next.y] = true;
q.push(next);
}
}
}
}
}
int main(){
int jx, jy;
cin>>t;
while(t--){
while(!q.empty()) q.pop();
cin>>r>>c;
for(int i = 0; i < r; i++) scanf("%s", &maze[i]);
memset(book, 0, sizeof(book));
memset(fire, inf, sizeof(fire));
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
if(maze[i][j] == 'J'){
jx = i; jy = j;
}
else if(maze[i][j] == 'F'){
node temp;
temp.x = i;
temp.y = j;
book[i][j] = true;
fire[i][j] = 0;
q.push(temp);
}
}
}
BFSfire();
memset(dis, inf, sizeof(dis));
memset(book, 0, sizeof(book));
flag = false;
int ans = BFShuman(jx, jy);
if(flag) cout<