UVA 11624 fire! (BFS)

Source: Internet
Author: User

fire!
Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld &%llu

Submit Status

Description

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 Rand C, separated by spaces, with 1 <= R, C<= 1000. The following RLines of the test case each contain one row of the maze. Each of these lines contains exactly Ccharacters, and each of the 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's on fire
There'll be exactly oneJIn each test case. Sample Input
4#### #JF # #. ##.. #3 3### #J. F
Output specificationfor Each test case, output a single line containingImpossibleIf Joe cannot exit the maze before the fire reaches him, or an integer giving the earliest time Joe can safely exit the MA Ze, in minutes. Output for Sample Input
3IMPOSSIBLE

Test Instructions:There are multiple sources of fire f,j and F can only move up and down one lattice, ask J to escape the minimum time, can not escape the output "impossible". The following:BFS, the ignition source first into the queue, and marked as having fire, then J enqueued, marked without fire, to the edge of ITCSC escape. CODE:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <string > #include <algorithm> #include <cstdlib> #include <set> #include <queue> #include <stack > #include <vector> #include <map> #define N 100010#define Mod 10000007#define Lson l,mid,idx<<1#  Define Rson mid+1,r,idx<<1|1#define LC idx<<1#define RC Idx<<1|1const Double EPS = 1e-11;const double PI = ACOs ( -1.0); const double E = 2.718281828;typedef long long ll;const int INF = 1000010;using namespace Std;int n,m;boo L Vis[1010][1010];char mp[1010][1010];int xx[4]= { -1,0,1,0};int yy[4]= {0,1,0,-1};int Fx,Fy,Jx,Jy;int flag;int len;    struct node{int x, y;    int num; int fire;};    Node Q[10000];queue<node>que;void BFS () {while (Que.size ()) Que.pop ();    memset (vis,0,sizeof Vis);    Node a,t;        for (int i=0; i<len; i++) {Que.push (q[i]);    Vis[q[i].x][q[i].y]=1;    } a.x=jx,a.y=jy,a.num=0,a.fire=0;Que.push (a);    Vis[jx][jy]=1;        while (Que.size ()) {A=que.front ();        Que.pop (); if (a.x==1| | a.x==n| | a.y==1| |            A.Y==M) &&a.fire==0) {cout<<a.num+1<<endl;            flag=0;        Return            } for (int i=0; i<4; i++) {t.x=xx[i]+a.x;            T.Y=YY[I]+A.Y; if (T.x>=1&&t.x<=n&&t.y>=1&&t.y<=m&&!vis[t.x][t.y]&&mp[t.x]                [t.y]!= ' # ') {t.num=a.num+1;                T.fire=a.fire;                Que.push (t);            Vis[t.x][t.y]=1;    }}}}int Main () {int T;    cin>>t;            {while (t--) {scanf ("%d%d", &n,&m);            GetChar ();            len=0; for (int i=1, i<=n; i++) {for (int j=1; j<=m; J + +) {Scan                    F ("%c", &mp[i][j]);          if (mp[i][j]== ' J ')          {jx=i,jy=j; } if (mp[i][j]== ' F ') {Q[len].x=i,q[len].y=j,q[len].num=0,q[le                    n++].fire=1;            }} getchar ();            } flag=1;            BFS ();        if (flag) cout<< "impossible\n"; }} return 0;}


UVA 11624 fire! (BFS)

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.