首页 > 其他 > 详细

UVA Problem B: Fire!

时间:2014-04-30 07:44:32      阅读:439      评论:0      收藏:0      [点我收藏+]

Problem B: Fire!

bubuko.com,布布扣 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 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
讲解:写了一下午还是没写出来,这种题还没有做过,的确很难想到用这种方法;
参考的别人的代码,两个搜索,比较人到这个地方时,火来了没有;
bubuko.com,布布扣
 1 #include<stdio.h>
 2 #include<string.h>
 3 int T,m,n,ans,ok,step[1005][1005],bu[1005][1005]; // step用来初始化火,bu用来记录人
 4 char mat[1005][1005];
 5 struct C
 6 {
 7     int x,y;
 8 }q[10000100];
 9 int dx[4]={1,0,-1,0};
10 int dy[4]={0,1,0,-1};
11 void fire()
12 {
13     int fr=0,re=0;
14     memset(step,-1,sizeof(step)); //相当于标记访问,同时又记录步数
15     for(int i=0;i<n;i++)
16         for(int j=0;j<m;j++)
17             {
18             if(mat[i][j]==F) 
19             {
20                 q[re].x=i;
21                 q[re++].y=j;
22                 step[i][j]=0;
23             }
24         }
25     while(fr<re) {
26         int x=q[fr].x;
27         int y=q[fr].y;
28         for(int i=0;i<4;i++)
29         {
30             int nx=x+dx[i];
31             int ny=y+dy[i];
32             if(nx>=n || nx<0 || ny>=m || ny<0) continue;
33             if(step[nx][ny]!=-1) continue;
34             if(mat[nx][ny]==#) continue;
35             step[nx][ny]=step[x][y]+1;
36             q[re].x=nx;
37             q[re++].y=ny;
38         }
39         fr++;
40     }
41 }
42 void bfs()
43 {
44     memset(bu,-1,sizeof(bu));  //同理
45     int fr=0,re=0;
46     for(int i=0;i<n;i++)
47         for(int j=0;j<m;j++)
48             if(mat[i][j]==J) {
49                 q[re].x=i;
50                 q[re++].y=j;
51                 bu[i][j]=0;
52             }
53     while(fr<re)
54     {
55         int nx=q[fr].x;
56         int ny=q[fr].y;
57         for(int i=0;i<4;i++)
58         {
59             int fx=nx+dx[i];
60             int fy=ny+dy[i];
61             if(nx==0 || nx==n-1 || ny==0 || ny==m-1)  {
62                 ok=1;
63                 ans=bu[nx][ny]+1;
64                 return ;
65             }
66             if(fx>=n || fx<0 || fy>=m || fy<0) continue;
67             if(bu[fx][fy]!=-1) continue;
68             if(mat[fx][fy]==#) continue;
69 
70         /* 须特别注意,火有可能被墙包围,使得火无法蔓延。下句的意思是,如果火蔓延到这个格子,
71         并且火到这个格子的步数小于或等于人到这个格子的步数,那么跳过此格子。假使火根本没有蔓延过来
72         ,理所当然可以人走这个格子。 */
73             if(step[fx][fy]!=-1 && bu[nx][ny]+1>=step[fx][fy]) continue; 
74             q[re].x=fx;
75             q[re++].y=fy;
76             bu[fx][fy]=bu[nx][ny]+1;
77 
78         }
79         fr++;
80     }
81 }
82 int main()
83 {
84     scanf("%d%*c",&T);
85     while(T--) {
86         scanf("%d %d%*c",&n,&m);
87         for(int i=0;i<n;i++)
88             gets(mat[i]);
89         fire();
90         ok=0;
91         bfs();
92         if(ok) printf("%d\n",ans);
93         else printf("IMPOSSIBLE\n");
94     }
95     return 0;
96 }
bubuko.com,布布扣

 

UVA Problem B: Fire!,布布扣,bubuko.com

UVA Problem B: Fire!

原文:http://www.cnblogs.com/lovychen/p/3689303.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!