Fire!
This problem will be judged on UVA. Original ID: 11624
64-bit integer IO format: %lld Java class name: Main
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 Specification
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.
Sample Input
2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F
Output Specification
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.
Output for Sample Input
3
IMPOSSIBLE 解题:bfs...让火先走,再人走。。判断走出去的是人还是火
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = ;
char mp[maxn][maxn];
bool vis[maxn][maxn];
int n,m;
struct node{
int x,y,t;
bool isFire;
node(int a = ,int b = ,int c = ,bool isfire = true){
x = a;
y = b;
t = c;
isFire = isfire;
}
};
queue<node>q;
bool isIn(int x,int y){
return x < n && x >= && y < m && y >= ;
}
int bfs(){
static const int dir[][] = {-,,,,,-,,};
while(!q.empty()){
node now = q.front();
q.pop();
for(int i = ; i < ; ++i){
int tx = now.x + dir[i][];
int ty = now.y + dir[i][];
if(isIn(tx,ty)){
if(!vis[tx][ty]){
vis[tx][ty] = true;
q.push(node(tx,ty,now.t+,now.isFire));
}
}else if(!now.isFire) return now.t+;
}
}
return -;
}
int main(){
int kase,px,py;
scanf("%d",&kase);
while(kase--){
scanf("%d %d",&n,&m);
memset(vis,false,sizeof(vis));
while(!q.empty()) q.pop();
for(int i = ; i < n; ++i){
scanf("%s",mp[i]);
for(int j = ; j < m; ++j){
if(mp[i][j] == 'F'){
vis[i][j] = true;
q.push(node(i,j,,true));
}else if(mp[i][j] == '#') vis[i][j] = true;
else if(mp[i][j] == 'J') vis[px = i][py = j] = true;
}
}
q.push(node(px,py,,false));
int ans = bfs();
if(ans == -) puts("IMPOSSIBLE");
else printf("%d\n",ans);
}
return ;
}
/*
2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F */