uva 11624 - Fire!(bfs,3级)

来源:互联网 发布:淘宝男装最出名的店 编辑:程序博客网 时间:2024/05/18 05:26

Problem B: 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 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

24 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

3IMPOSSIBLE

Malcolm Sharpe, Ondřej Lhoták

思路:入门级BFS,加了fire,其实还是一样,只要预处理把所有fire点入队,然后再把起始点入队,进行BFS,搜到边界就有解。


#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;const int mm=1000+9;const int dx[]={-1,0,1,0};const int dy[]={0,-1,0,1};bool vis[mm][mm];class node{  public:int x,y,fire,step;};char g[mm][mm];int n,m,cas;node s,t;queue<node>q;void get(int n,int m){while(!q.empty())q.pop(); memset(vis,0,sizeof(vis)); char c; for(int i=0;i<n;++i) for(int j=0;j<m;++j) {  while(1)    {c=getchar();     switch(c)     {       case '.':g[i][j]=c;goto there;       case '#':g[i][j]=c;goto there;       case 'J':s.x=i;s.y=j;s.step=0;s.fire=0;g[i][j]=c;vis[i][j]=1;goto there;       case 'F':t.x=i;t.y=j;t.fire=1;t.step=0;q.push(t);g[i][j]=c;vis[i][j]=1;goto there;     }    }    there:; } q.push(s);}int bfs(){ node z;  while(!q.empty())  {    z=q.front();q.pop();    for(int i=0;i<4;++i)    {      t=z;t.x+=dx[i];t.y+=dy[i];t.step++;      if(z.fire)      {        if(t.x<0||t.x>=n||t.y<0||t.y>=m||vis[t.x][t.y]||g[t.x][t.y]=='#')continue;        q.push(t);vis[t.x][t.y]=1;      }      else      {        if(t.x<0||t.x>=n||t.y<0||t.y>=m)return t.step;        if(vis[t.x][t.y]||g[t.x][t.y]=='#')continue;        q.push(t);vis[t.x][t.y]=1;      }    }  }  return -1;}int main(){  while(~scanf("%d",&cas))  {    while(cas--)    {      scanf("%d%d",&n,&m);      get(n,m);      int ans=bfs();      if(ans<0)printf("IMPOSSIBLE\n");      else printf("%d\n",ans);    }  }  return 0;}




原创粉丝点击