UVA--11624(多起点遍历)

Fire!

技术分享 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 Specification

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.

Sample Input

2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F

Output Specification

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.

Output for Sample Input

3
IMPOSSIBLE

解体思路:只想说我知道火的个数是未知的,但我还是先WA到死,然后是超时,有这么悲崔的吗?发火大哭.我一开始是让J先遍历,然后再一一遍历火.给几个这个思路WA的样例,大家可以参考:

2

3 3

.F.
FJF
.F.  
3 3
###
#J#
###
IMPOSSIBLE
IMPOSSIBLE
后来换了一个思路,让火都入队,同时遍历,求出每个位置火到达的最小时间,最后遍历J,这样就省了很多时间。而且需要讨论的情况也变少了。
AC代码如下:
#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f3f
#include<queue>
using namespace std;
int vist[1010][1010];
char mark[1010][1010];
int fire[1010][1010];
int n,m;
int dis[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct stu{
	int x,y,t;
};
queue<stu>q;
void bfs1(){
	stu temp,star;
	while(!q.empty()){
		star=q.front();
		q.pop();
		for(int i=0;i<4;i++){
			temp.x=star.x+dis[i][0];
			temp.y=star.y+dis[i][1];
			temp.t=star.t+1;
			if(temp.x<0||temp.y<0||temp.x>=n||temp.y>=m)continue;
			if(vist[temp.x][temp.y])continue;
			if(mark[temp.x][temp.y]=='#')continue;
			if(temp.t<fire[temp.x][temp.y])fire[temp.x][temp.y]=temp.t;
		    vist[temp.x][temp.y]=1;
		    q.push(temp);
		}
	}
}
int bfs2(int x,int y){
	memset(vist,0,sizeof(vist));
	vist[x][y]=1;
	while(!q.empty()) q.pop();
	stu temp,star;
	temp.x=x;temp.y=y;temp.t=0;
	q.push(temp);
	while(!q.empty()){
		star=q.front();
		q.pop();
		if(star.x==0||star.y==0||star.x==n-1||star.y==m-1){
			return star.t+1;
		}
		for(int i=0;i<4;i++){
			temp.x=star.x+dis[i][0];
			temp.y=star.y+dis[i][1];
			temp.t=star.t+1;
			if(temp.x<0||temp.y<0||temp.x>=n||temp.y>=m)continue;
			if(vist[temp.x][temp.y])continue;
			if(mark[temp.x][temp.y]=='#')continue;
			if(temp.t>=fire[temp.x][temp.y])continue;
			vist[temp.x][temp.y]=1;
		    q.push(temp);
		}
	}
	return 0;
}
int main(){
	int t,x,y;
	stu node;
	scanf("%d",&t);
	while(t--){
		memset(vist,0,sizeof(vist));
		while(!q.empty()) q.pop();
		scanf("%d%d",&n,&m);
		for(int i=0;i<n;i++){
			getchar();
		    for(int j=0;j<m;j++){
		   	scanf("%c",&mark[i][j]);
		   	fire[i][j]=INF;
		    if(mark[i][j]=='F'){
		    	node.x=i;node.y=j;node.t=0;
		    	vist[i][j]=1;
		    	fire[i][j]=0;
		    	q.push(node);
		    }
		    else if(mark[i][j]=='J'){
		    	x=i;y=j;
		    }
			}	
		}
		bfs1();
		int ans=bfs2(x,y);
		if(ans)printf("%d\n",ans);
		else printf("IMPOSSIBLE\n");
	
	}
	return 0;
}


再附上我那悲催的代码,人家就是想让bfs()合并成一个嘛
代码如下:
#include<stdio.h>
#include<string.h>
#include<queue>
#define INF 0x3f3f3f3f
int n,m,ans,val;
using namespace std;
char mark[1010][1010];
int time[1010][1010];
int vist[1010][1010];
int dis[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct stu{
	int x,y,t;
};
bool check(stu temp){
	if(temp.x<0||temp.y<0||temp.x>=n||temp.y>=m)return false;
	else if(vist[temp.x][temp.y])return false;
	else if(mark[temp.x][temp.y]=='#')return false;
	return true;
}
void bfs(int x,int y,int k){
	queue<stu>q;
	memset(vist,0,sizeof(vist));
	while(!q.empty()){
		q.pop();
	}
	stu temp,star;
	temp.x=x;temp.y=y;temp.t=1;
	vist[x][y]=1;
	if(!k)
	time[x][y]=1;
	q.push(temp);
	while(!q.empty()){
		star=q.front();
		q.pop();
		for(int i=0;i<4;i++){
			temp.x=star.x+dis[i][0];
			temp.y=star.y+dis[i][1];
			if(check(temp)){
				if(!k&&mark[temp.x][temp.y]=='F')continue;
				temp.t=star.t+1;
				vist[temp.x][temp.y]=1;
				if(!k){
					time[temp.x][temp.y]=temp.t;
				    if(temp.x==0||temp.y==0||temp.x==n-1||temp.y==m-1){
				    	if(temp.t<val)val=temp.t;
				    }
				}
				else if(temp.t>time[temp.x][temp.y]&&(temp.x==0||temp.y==0||temp.x==n-1||temp.y==m-1)){
					ans=time[temp.x][temp.y];
				}
				q.push(temp);
			}
		}
	}
}
int main(){
	int t;
	int x,y;
	scanf("%d",&t);
	while(t--){
		int cas=0;
		scanf("%d%d",&n,&m);
	    for(int i=0;i<n;i++){
	    	getchar();
			for(int j=0;j<m;j++){
	    		scanf("%c",&mark[i][j]);
	    	     if(mark[i][j]=='J'){
	    			x=i;
	    			y=j;
	    		}
	    		else if(mark[i][j]=='F'){
	    			cas=1;
	    		}
	    	}
	    }
	    val=INF;
	    bfs(x,y,0);
	    int flag=0;
	    int max=-1;
	    if(cas){
	    	for(int i=0;i<n;i++){
	    	if(flag)break;
	    	for(int j=0;j<m;j++){
	    		if(mark[i][j]=='F'){
	    			ans=0;
	    			bfs(i,j,1);
	    			if(ans==0){
	    				flag=1;
	    				break;
	    			}
	    			if(ans>max)max=ans;
	    		}
	    	}
	    }
	    }
	    if(flag||val>=INF) printf("IMPOSSIBLE\n");
	    else if(flag==0&&max==-1)printf("%d\n",val);
	    else if(flag==0&&max!=-1)printf("%d\n",max);
	}
	return 0;
}





                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值