11624 Fire!
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
andC, separated by spaces, with 1R; C1000. The following Rlines of the test case each contain
one row of the maze. Each of these lines contains exactlyCcharacters, 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 oneJin 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
Universidad de Valladolid OJ:11624 – Fire! 2/2
Sample Output
3
IMPOSSIBLE
题意:给定一个图:#为墙 ; 。为草坪 ; J 为人 ; F为火 。
人只能走草坪,火可以从草坪蔓延,也会烧到人,问人能否顺利逃生,若能,输出最少步数,反之,输出IMPOSSIBLE。
思路:简单BFS 。先人后火。 先火后人都可。我这给出是的先人后火的代码,维护下细节即可。
此题需注意的是火源可能不唯一。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define CLR(arr,x) memset(arr,x,sizeof(arr))
using namespace std;
const int maxn = 1000+100 ;
const int inf = 1<<30 ;
char mapp[maxn][maxn] ;
int temp[maxn][maxn] ;
int n , m , num ;
int Next[4][2] = {{0,1} , {1,0} , {0,-1} , {-1,0} } ;
struct node{
int row , col ;
};
node ren , fire[maxn] ;
void init() {
CLR(temp,0) ;
num = 0 ;
}
bool judge(node p){
if(p.row>=0 && p.row<n && p.col >= 0&& p.col < m ) {
return true ;
}
return false ;
}
bool bfs() {
node init, temp2 ;
queue<node> Q ;
while(!Q.empty() ) {
Q.pop() ;
}
Q.push(ren) ;
for( int i = 0 ; i < num ; ++i ) {
Q.push(fire[i]) ;
}
while(!Q.empty()) {
temp2 = Q.front() ;
Q.pop() ;
for( int i = 0 ; i < 4 ; ++i ) {
init.row = temp2.row+Next[i][0] ;
init.col = temp2.col+Next[i][1] ;
bool flag = judge(init) ;
if( !flag && mapp[temp2.row][temp2.col] == 'J' ) {
cout << temp[temp2.row][temp2.col]+1 << endl ;
return true;
}
if( flag && mapp[temp2.row][temp2.col] == 'J' && mapp[init.row][init.col] == '.' ) {
Q.push(init) ;
temp[init.row][init.col] = temp[temp2.row][temp2.col] + 1 ;
mapp[init.row][init.col] = 'J' ;
}
else if( flag && mapp[temp2.row][temp2.col] == 'F' && mapp[init.row][init.col] != '#' && mapp[init.row][init.col]!='F') {
Q.push(init) ;
mapp[init.row][init.col] = 'F' ;
}
}
if(mapp[temp2.row][temp2.col] == 'J') mapp[temp2.row][temp2.col] = '*' ;
}
return false ;
}
int main() {
int t ;
cin >> t ;
while(t--){
init() ;
scanf("%d%d",&n,&m) ;
for(int i = 0 ; i < n ; ++i ){
scanf("%s",mapp[i]) ;
for( int j = 0 ; j < m ; ++j ) {
if(mapp[i][j] == 'J' ){
ren.row = i ;
ren.col = j ;
}
if( mapp[i][j] == 'F') {
fire[num].row = i ;
fire[num].col = j ;
num++;
}
}
}
if(!bfs()){
printf("IMPOSSIBLE\n") ;
}
}
return 0;
}