UVA 11624 Fire!(BFS)

版权声明:本文为博主原创文章,欢迎转载。如有问题,欢迎指正。 https://blog.csdn.net/weixin_42172261/article/details/88362328

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
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.
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
Output for Sample Input
3
IMPOSSIBLE

像这种题真的是毫无思路,一看题解又觉得很简单,无语了。
两次广搜,分别搜火和人,maze记录迷宫,a记录火到达[i][j]处所用时间,b记录人到达[i][j]处所用时间。需要注意的是如果人和火同时到达一个地方,就会被火烧了,所以应该先广搜火,然后人到达某处的条件是,火不会到达,或者是火能到达并且火到达的时间大于人到达的时间(等于就不行)。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

int n, m, t;
int nextt[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
char maze[1005][1005];

int check(int x, int y)//检查点是否在迷宫范围里 
{
	if (x>=0 && x<n && y>=0 && y<m)
		return 1;
	return 0;
}

int a[1005][1005];//a[i][j]表示火到ij位置所用时间 
queue<pair<int, int> >q;

void bfs_fire()
{
	while (!q.empty())
		q.pop();
	memset(a, -1, sizeof(a));
	for (int i=0; i<n; i++){
		for (int j=0; j<m; j++){
			if (maze[i][j]=='F'){//火的位置可能不止一处 
				a[i][j]=0; 
				q.push(make_pair(i, j));
			}
		}
	}
	while (!q.empty()){
		pair<int, int> tmp;
		tmp=q.front();
		q.pop();
		for (int k=0; k<4; k++){
			int x=tmp.first+nextt[k][0];
			int y=tmp.second+nextt[k][1];
			if (check(x, y)==0 || a[x][y]!=-1 || maze[x][y]=='#')
				continue;
			a[x][y]=a[tmp.first][tmp.second]+1;
			q.push(make_pair(x, y));
		}
	}
}

int b[1005][1005];//b[i][j]表示人到达ij的时间 

void bfs_people()
{
	while (!q.empty())
		q.pop();
	memset(b, -1, sizeof(b));
	for (int i=0; i<n; i++){
		for (int j=0; j<m; j++){
			if (maze[i][j]=='J'){
				q.push(make_pair(i, j));
				b[i][j]=0;
				break;
			}
		}
	}
	while (!q.empty()){
		pair<int, int> tmp;
		tmp=q.front();
		q.pop();
		if (tmp.first==0||tmp.first==n-1||tmp.second==0||tmp.second==m-1){
			printf("%d\n", b[tmp.first][tmp.second]+1);
			return;
		}
		for (int k=0; k<4; k++){
			int x=tmp.first+nextt[k][0];
			int y=tmp.second+nextt[k][1];
			if (check(x, y)==0 || b[x][y]!=-1 || maze[x][y]=='#')
				continue;
			if (a[x][y]!=-1&&b[tmp.first][tmp.second]+1>=a[x][y])
				continue;//先着火人后到达 
			b[x][y]=b[tmp.first][tmp.second]+1;
			q.push(make_pair(x, y));
		}
	}
	printf("IMPOSSIBLE\n");
	return;
}
int main()
{
	scanf("%d", &t);
	while (t--){
		scanf("%d %d", &n, &m);
		for (int i=0; i<n; i++)
			scanf("%s", maze[i]);
		bfs_fire();
		bfs_people();
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42172261/article/details/88362328