UVA-11624 fire! (two times BFS two methods)

Source: Internet
Author: User
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 test cases to follow. The first line of all test case contains the integers r and C, separated by spaces, with 1 <= R, C <= 1 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 Squar  E J, Joe's initial position in the maze, which are a passable square F, a square that's on fire there would be exactly one J in each test case.Sample Input

2 4 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 an integer giving the earliest time Joe can safely exit the maze, in minutes. Output for Sample Input
3
Impossible

Main topic:
There was a man trapped in the fire. The flames spread 1 of cells per minute, moving one cell per minute to find out if people would be trapped in the fire, or escape, if escaped, to find the shortest time.
which
' # ' denotes the wall, ' F ' denotes the flame, ' J ' denotes the initial position of the person, '. ' Represents a road.
Note: Don't be misled by the sample, it may just start with more than one fire.


Analytical:
There are two kinds of solutions.
Solution One,
First, the ignition time of each point is calculated and saved with a two-dimensional array.
Again to the person BFS once, seek to flee to the boundary of the least time, each time if the moving time >= that point fire time, then continue.


Solution Two,
Using a structure to mark a person or a fire, first the fire and the position of the person respectively BFS (first fire again people), if the fire on the BFS that point and ' F ' occupy position, if the person can only move in the '. ' Until the person arrives at the border or is surrounded by fire without a way to end.


Solution One

#include <iostream> #include <stdio.h> #include <string.h> #include <queue> using namespace std;
	struct Node {int R,c,lev;
		Node (int r,int c,int lev) {this->r = R;
		This->c = C;
	This->lev = Lev;
}
};
const int INF = 0X3F3F3F;
const int N = 1010;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0,-1, 0, 1};
int m,n;
Char Grid[n][n];
int vis[n][n];
int fire[n][n];
	void Bfs_fire () {memset (vis,0,sizeof (VIS));
	Queue<node> Q;
				for (int i = 0, i < m; i++) {for (int j = 0; J < N; j + +) {if (grid[i][j] = = ' F ') {vis[i][j] = true;
				FIRE[I][J] = 0;
			Q.push (Node (i,j,0));
	}}} int R,c,lev;
		while (!q.empty ()) {Node front = Q.front ();
		Q.pop ();
			for (int i = 0; i < 4; i++) {r = FRONT.R + Dr[i];
			c = front.c + dc[i];
			Lev = Front.lev + 1; 
			if (R < 0 | | | r >= m | | C < 0 | | c >= N | | grid[r][c] = = ' # ' | | grid[r][c] = = ' F ' | | vis[r][c]) {continue;
			} Vis[r][c] = true; FIRE[R][C] =Lev
		Q.push (Node (R,c,lev));
	}}} int bfs (int sr,int SC) {memset (vis,0,sizeof (VIS));
	Queue<node> Q;
	Q.push (Node (sr,sc,0));
	VIS[SR][SC] = true;
	int R,c,lev;
		while (!q.empty ()) {Node front = Q.front ();
		Q.pop ();
			for (int i = 0; i < 4; i++) {r = FRONT.R + Dr[i];
			c = front.c + dc[i];
			Lev = Front.lev + 1;
			if (R < 0 | | | r >= m | | C < 0 | | c >= N) {return lev;
			} if (Fire[r][c] <= Lev | | grid[r][c] = = ' # ' | | grid[r][c] = = ' F ' | | vis[r][c]) {continue;
			} Vis[r][c] = true;
		Q.push (Node (R,c,lev));
}} return 0;	
	} int main () {int t;
	int SR,SC;
			while (scanf ("%d", &t)! = EOF) {while (t--) {memset (grid,0,sizeof (grid));
			memset (Fire,0,sizeof (fire));
			scanf ("%d%d", &m,&n);
				for (int i = 0; i < m; i++) {scanf ("%s", Grid[i]);
						for (int j = 0; J < N; j + +) {Fire[i][j] = INF;//Initialize to Maximum if (grid[i][j] = = ' J ') {sr = i;
					sc = j;
			}}} bfs_fire (); InchT cnt = BFS (SR,SC);
			if (CNT) {printf ("%d\n", CNT);
			}else {printf ("impossible\n");
}}} return 0; }


Solution Two

#include <iostream> #include <stdio.h> #include <string.h> #include <queue> using namespace std;
    struct node{int R,c,lev;
    BOOL Fire;
        Node (int r,int c,int Lev,bool fire) {this->r = R;
        This->c = C;
        This->lev = Lev;
    This->fire = fire;
}
};
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0,-1, 0, 1};
const int N = 1010;
Char Grid[n][n];
int vis[n][n];
int m,n;
node* Q[n*n];
    int BFS (int sr,int sc) {int front,rear;
    Front = rear = 0; for (int i = 0, i < m; i++) {for (int j = 0; J < N; j + +) {if (grid[i][j] = = ' F ') {q[rear++] = new
			Node (i,j,0,true);
    }}} q[rear++] = new Node (sr,sc,0,false);
    VIS[SR][SC] = true;
    int R,c,lev;
        while (front < rear) {node* pre = q[front++];
                if (Pre->fire) {for (int i = 0; i < 4; i++) {r = Pre->r + Dr[i];
              c = pre->c + dc[i];  Lev = Pre->lev + 1; if (R < 0 | | | r >= m | | C < 0 | | c >= N | | grid[r][c] = = ' # ' | | grid[r][c] = = ' F ') {Continu
                E
                } Grid[r][c] = ' F ';
            q[rear++] = new Node (r,c,lev,true);
                }} else {for (int i = 0; i < 4; i++) {r = Pre->r + Dr[i];
                c = pre->c + dc[i];
                Lev = Pre->lev + 1;
                if (grid[r][c] = = ' # ' | | grid[r][c] = = ' F ' | | vis[r][c]) {continue;
                } if (r < 0 | | | r >= m | | C < 0 | | c >= N) {return lev;
                } Vis[r][c] = true;
             q[rear++] = new Node (r,c,lev,false);
}}} return 0;
    } int main () {int t;
    int SC,SR;
            while (scanf ("%d", &t)! = EOF) {while (t--) {memset (grid,0,sizeof (grid));memset (vis,0,sizeof (VIS));
            memset (q,null,sizeof (q));
            scanf ("%d%d", &m,&n);
                for (int i = 0; i < m; i++) {scanf ("%s", Grid[i]);
                        for (int j = 0; J < N; j + +) {if (grid[i][j] = = ' J ') {sr = i;
                    sc = j;
            }}} int cnt = BFS (SR,SC);
            if (CNT) {printf ("%d\n", CNT);
            } else {printf ("impossible\n");
}}} return 0; }



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.