题目:
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
思路:
首先需要注意的是可能有多个点火点,即存在多个F。我的思路是BFS,刚开始将人和火都push进队列,人在前面,火在后面,这样每轮都是先处理人,后处理火,人只能走空位置,
火可以走空位置和人走过的位置,每次pop出人的位置后先检查一下有没有被火走过,如果被火走过这个位置就不会存在了,直接continue就行了
pop出人的位置后检查一下是不是在边界上,如果是就说明逃离成功。
这道题还有一种方法,就是预先搜索出所有的位置上火到达的时间,然后在开始搜索人的路径,只要人到达的某个位置的时间小于火到达这个位置的时间就行。
代码:
1 #include<iostream> 2 #include<queue> 3 using namespace std; 4 5 struct Pos{ 6 int x,y; 7 int type; //类型为人(0)还是火(1) 8 int step; 9 }; 10 11 int R=0,C=0; 12 int Fnum=0; //火的数量 13 Pos JF[200]; //0位置上存储J的Pos,后面存F的Pos 14 queue<Pos> q; //BFS队列 15 16 int main(){ 17 int T=0; 18 cin >>T; 19 for(int t=0;t<T;t++){ 20 cin >>R>>C; 21 int **b=new int*[R]; //R和C的范围比较大,所以选择动态数组 22 for(int r=0;r<R;r++){ 23 b[r]=new int[C]; 24 } 25 //建立棋盘 26 for(int i=0;i<R;i++){ 27 for(int j=0;j<C;j++){ 28 char c; 29 cin >>c; 30 if(c==‘#‘){ 31 b[i][j]=-1; 32 }else if(c==‘.‘){ 33 b[i][j]=0; 34 }else if(c==‘J‘){ 35 b[i][j]=1; //人走过的标记为1 36 Pos pos; 37 pos.x=i;pos.y=j;pos.type=0;pos.step=0; 38 JF[0]=pos; 39 }else if(c==‘F‘){ 40 b[i][j]=2; //火烧过的标记为2 41 Pos pos; 42 pos.x=i;pos.y=j;pos.type=1;pos.step=0; 43 JF[++Fnum]=pos; 44 } 45 } 46 } 47 48 int X[]={-1,1,0,0}; 49 int Y[]={0,0,-1,1}; 50 51 //BFS 52 bool succ=false; 53 q.push(JF[0]); 54 for(int i=1;i<=Fnum;i++){ 55 q.push(JF[i]); 56 } 57 while(!q.empty()){ 58 Pos p=q.front(); 59 q.pop(); 60 if(p.type==0&&b[p.x][p.y]==1){ 61 if(p.x==0||p.x==R-1||p.y==0||p.y==C-1){ //如果到达边界了,就成功了 62 cout <<p.step+1<<endl; 63 succ=true; 64 break; 65 } 66 } 67 for(int i=0;i<4;i++){ 68 Pos pp; 69 pp.x=p.x+X[i];pp.y=p.y+Y[i];pp.type=p.type; 70 pp.step=p.step+1; 71 if(pp.x>=0&&pp.x<R&&pp.y>=0&&pp.y<C){ 72 if(pp.type==0&&b[pp.x][pp.y]==0){ 73 //如果类型为人,只能走自己没走过的空地 74 b[pp.x][pp.y]=1; 75 q.push(pp); 76 }else if(pp.type==1&&(b[pp.x][pp.y]==0||b[pp.x][pp.y]==1)){ 77 //如果类型为火,可以走空地和人走过的地方 78 b[pp.x][pp.y]=2; 79 q.push(pp); 80 } 81 } 82 } 83 } 84 //把队列清空 85 while(!q.empty()){ 86 q.pop(); 87 } 88 if(!succ){ 89 cout <<"IMPOSSIBLE"<<endl; 90 } 91 //为下一个case进行初始化 92 Fnum=0; 93 for(int i=0;i<R;i++){ 94 delete b[i]; 95 } 96 delete b; 97 } 98 99 system("pause"); 100 return 0; 101 }
原文:https://www.cnblogs.com/shiyu-coder/p/13156820.html