UVA11624---Fire(雙bfs)

                                                  Fire

                                                                Time limit   1000 ms

Joe works in a maze. Unfortunately,portions of the maze have caught on fire, and the owner of the maze neglectedto create a fire escape plan. Help Joe escape the maze.

Given Joe's location in the maze and which squares of themaze are on fire, you must determine whether Joe can exit the maze before thefire 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 fourdirections from each square that is on fire. Joe may exit the maze from anysquare that borders the edge of the maze. Neither Joe nor the fire may enter asquare that is occupied by a wall.

Input Specification

The first line of input contains a single integer, thenumber of test cases to follow. The first line of each test case contains thetwo integers R and C, separated byspaces, with 1 <= R,C <= 1000. Thefollowing R lines of the test case each contain one row of themaze. Each of these lines contains exactly C characters, andeach of these characters is one of:

·  #, a wall

·  ., a passable square

·  J, Joe's initialposition in the maze, which is a passable square

·  F, a square that is onfire

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

Sample Output

3

IMPOSSIBLE

這是十個月之前的題( ̄ェ ̄;) ,今天終於給補上了

看到這道題的第一想法是在一個bfs裏面 人和火一起跑,火更新地圖,如果人和火撞到一起就gg,寫了一會,不好下手,那就只能分開成兩個bfs了,因爲火會影響人的路線,但是火的路線和到達某一個點的時間是固定的,所以很容易想到的思路就是先讓火跑遍整個圖,做好預處理,記下來火到達每個點的時間,然後在讓人出發,如果人到達這個地方的時間小於火到達這個地方的時間,那麼是可以通過的,否則這個點是不能走的,如果人可以跑到地圖之外,就安全撤離了

但是這道題始終沒能AC,去網上扒拉了一下才知道坑點在題目第一行的那個單詞 portions 就是說起火點不一定只有一個,可能是多個,就不能簡單的存火的起始點了,那就直接入隊列吧,但是全局隊列不要忘了初始化

還有就是不要用time作爲變量名,應該是個保留字吧,編譯過不去,反正別用就對了。。。。

#include<cstdio>
#include<string>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
inline int _read() {
    char ch = getchar();
    int sum = 0;
    while (!(ch >= '0' && ch <= '9'))ch = getchar();
    while (ch >= '0' && ch <= '9')sum = sum * 10 + ch - 48, ch = getchar();
    return sum;
}
const int inf=0x3f3f3f3f;
const int mm=1005;

int t;
int n,m;
char mp[mm][mm];
int book[mm][mm];
int tim[mm][mm];
int to[4][2]={0,1,1,0,0,-1,-1,0};
int sx,sy,fx,fy; 
struct node{
	int x,y;
	int step;
	node(int a,int b,int c):x(a),y(b),step(c){}
};
queue<node>q;

void bfs_F(){
	while(!q.empty()){
		node pre=q.front();
		q.pop();
		for(int i=0;i<4;i++){
			int nx=pre.x+to[i][0];
			int ny=pre.y+to[i][1];
			if(nx<0||nx>=n||ny<0||ny>=m||book[nx][ny]||mp[nx][ny]=='#')
				continue;
			book[nx][ny]=1;
			tim[nx][ny]=pre.step+1;
			q.push(node(nx,ny,pre.step+1)); 
		}
	}
}

int bfs_J(){
	while(!q.empty())
		q.pop();
	q.push(node(sx,sy,0));
	while(!q.empty()){
		node pre=q.front();
		q.pop();
		if(pre.x<0||pre.x>=n||pre.y<0||pre.y>=m)
			return pre.step;
		for(int i=0;i<4;i++){
			int nx=pre.x+to[i][0];
			int ny=pre.y+to[i][1];
			if(mp[nx][ny]!='#'&&pre.step+1<tim[nx][ny]){
				mp[nx][ny]='#';
				q.push(node(nx,ny,pre.step+1));
			}
		}	
	}
	return -1;
}

int main()
{
	cin>>t;
	while(t--){
		while(!q.empty())
			q.pop();
		mem(book,0);
		mem(tim,inf);
		scanf("%d%d",&n,&m);
		for(int i=0;i<n;i++){
			scanf("%s",mp[i]);
			for(int j=0;j<m;j++)
				if(mp[i][j]=='J')
					sx=i,sy=j;
				else if(mp[i][j]=='F'){//可能有多個起火點!!! 
					q.push(node(i,j,0));
					book[i][j]=1; 
					tim[i][j]=0;
				}
		}
		bfs_F();	
		int res=bfs_J();
		if(res==-1)
			printf("IMPOSSIBLE\n");
		else 
			printf("%d\n",res);
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章