uva_11624_fire! (BFS)

Source: Internet
Author: User

11624 fire!

Joe works in a maze. Unfortunately, portions of the maze has caught Onfire,and the owner of the maze neglected to create a firee scape 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

The first line of input contains a single integer, the number of 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 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 is 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


Test instructions: give you a maze, given the starting point, some of the points on fire, and the same as people to the up,down,left,right four directions to expand, when out of the border is successful. Ask if you can get out of the maze, if so, output the minimum number of steps, if not output "impossible".

Analysis: General BFS. Just add each fire to the queue first.

Title Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28833

Code Listing:

#include <map> #include <queue> #include <cmath> #include <stack> #include <ctime># include<cctype> #include <string> #include <cstdio> #include <cstring> #include <iostream    > #include <algorithm>using namespace std;typedef long long ll;const int MAXV = + + 5;struct edge{int x, y;    int step; int mark;}; int t,r,c;int Sx,sy,ex,ey;char Str[maxv][maxv];bool vis[maxv][maxv];int xy[4][2]={{-1,0},{0,-1},{1,0},{0,1}};bool    Judge (Edge p) {if (p.x>=0&&p.x<r&&p.y>=0&&p.y<c) return true; return false;}    Edge w,v;queue<edge>q;void BFs () {v.x=sx; v.y=sy; v.step=0; v.mark=1; Q.push (v);    Vis[sx][sy]=true;        while (Q.size ()) {W=q.front (); Q.pop (); if (w.mark==1&& (w.x==0| | w.x==r-1| | w.y==0| |            w.y==c-1)) {printf ("%d\n", w.step+1);        return;            } for (int i=0;i<4;i++) {v.x=w.x+xy[i][0];            V.Y=W.Y+XY[I][1]; V.step=w. step+1;            V.mark=w.mark; if (judge (v) &&!vis[v.x][v.y]&&str[v.x][v.y]== '. ')                {vis[v.x][v.y]=true;            Q.push (v); }}} printf ("impossible\n");}    int main () {scanf ("%d", &t);        while (t--) {while (Q.size ()) Q.pop ();        memset (vis,false,sizeof (VIS));        scanf ("%d%d", &r,&c);            for (int i=0;i<r;i++) {scanf ("%s", Str[i]);                for (int j=0;j<c;j++) {if (str[i][j]== ' J ') {sx=i; sy=j;}            if (str[i][j]== ' F ') {w.x=i; w.y=j; w.step=0; w.mark=0; vis[i][j]=true; Q.push (w);}    }} BFS (); }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.