BFS [UVA 11624] fire!

Source: Internet
Author: User

J- fire! Time limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & ; %llu Problem b:fire! Joe works in a maze. Unfortunately, portions of the maze has 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 is on fire, you must determine whether Joe can exit the MA Ze before the fire reaches him, and what fast he can do it.

Joe and the fire all move one square per minute, vertically or horizontally (not diagonally). The fire spreads all four directions from each square, that's on fire. Joe may exit the maze from any square that borders the edge of the maze. Neither Joe nor the fire could enter a square that's occupied by a wall.

Input specificationthe first line of input contains a single integer, the number of the test cases to follow. The first line of all test case contains the 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 the these lines contains exactly C characters, and each of the these characters isone of:
    • #, a wall
    • ., a passable square
    • J, Joe ' s initial position in the maze, which is a passable square
    • F, a square that's on fire
There'll is exactly one J in each test case.Sample Input
4#### #JF # #. ##.. #3 3### #J. F
Output specificationfor each test case, output a single line containing impossible If Joe cannot exit the maze before The fire reaches him, or a integer giving the earliest time Joe can safely exit the maze, in minutes. Output for Sample Input
3IMPOSSIBLE
Malcolm Sharpe, Ond?ej lhoták Level 6 to come, before I said if after, 6 levels will be able to, and then AC, bless my 6, haha ~ at the beginning of the misunderstanding, there may be many fire, WA a lot of times. Then change, encounter an F on the BFS once, helpless timeout. finally thought can first put all f into the team, in a BFS, so much faster.
#include <iostream>#include<cstdio>#include<cstring>#include<queue>using namespacestd;#defineINF 1<<30#defineN 1010structjf{intx,y,t; JF () {} JF (intXintYintt): X (x), Y (y), T (t) {}};intn,m;intJx,jy;queue<JF>Q;intVis[n][n];intFire[n][n];//Fire[i][j] Indicates the shortest time for fire to i,j positionCharMpt[n][n];intdir[4][2]={1,0,-1,0,0,1,0,-1};BOOLJudgeintXinty) {    if(x<1|| X>n | | y<1|| Y&GT;M)return 0; if(mpt[x][y]=='#')return 0; return 1;}voidbfs1 () {JF now,next;  while(!Q.empty ()) { Now=Q.front ();        Q.pop ();  for(intI=0;i<4; i++) {Next=Now ; NEXT.T++; Next.x+=dir[i][0]; Next.y+=dir[i][1]; if(judge (Next.x,next.y)) {if(next.t<Fire[next.x][next.y]) {Fire[next.x][next.y]=next.t;                Q.push (next); }            }        }    }}voidBFS2 () {Queue<JF>Q; memset (Vis,0,sizeof(VIS));    JF Now,next; now.x=JX; Now.y=JY; NOW.T=0; VIS[JX][JY]=1;    Q.push (now);  while(!Q.empty ()) { Now=Q.front ();        Q.pop (); if(now.x==1|| Now.x==n | | now.y==1|| now.y==m) {cout<<now.t+1<<Endl; return; }         for(intI=0;i<4; i++) {Next=Now ; NEXT.T++; Next.x+=dir[i][0]; Next.y+=dir[i][1]; if(Judge (NEXT.X,NEXT.Y) &&!vis[next.x][next.y] && fire[next.x][next.y]-next.t>=1) {Vis[next.x][next.y]=1;            Q.push (next); } }} cout<<"impossible\n";}intMain () {intT; scanf ("%d",&T);  while(t--) {scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++) {scanf ("%s", mpt[i]+1); }         for(intI=1; i<=n;i++)        {             for(intj=1; j<=m;j++) {Fire[i][j]=INF; if(mpt[i][j]=='F') {Fire[i][j]=0; Q.push (JF (I,j,0)); }                Else if(mpt[i][j]=='J') {JX=i; JY=J;        }}} BFS1 ();    BFS2 (); }    return 0;}

BFS [UVA 11624] fire!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.