uva11624 Fire! BFS搜索 TWT Tokyo Olympic 2combo-2

Joe works in a maze. Unfortunately, portions of the maze have
caught on re, and the owner of the maze neglected to create a re
escape plan. Help Joe escape the maze.
Given Joe's location in the maze and which squares of the maze
are on re, you must determine whether Joe can exit the maze before
the re reaches him, and how fast he can do it.
Joe and the re each move one square per minute, vertically or
horizontally (not diagonally). The re spreads all four directions
from each square that is on re. Joe may exit the maze from any
square that borders the edge of the maze. Neither Joe nor the re
may enter a square that is occupied by a wall.


Input


The rst line of input contains a single integer, the number of test
cases to follow. The rst 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 re
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
re 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


卧槽。。。。果然快速A了一题后人品就变得很糟糕。。。。wa到死。。。。

wa到zhangjiatao1来写作业。。。。

直接影响zhangjiatao1写作业拉他过来帮忙看代码。。。。并没有找出什么鬼wa点。。。

坑啊。。。。自己都觉得自己写的很对,这样的代码也wa真是天理难容啊。。。

于是上网查题解,并没有什么启发,老子写的已经很完善了好吗!!!

写完了数值计算的作业。。。。

。。。。卡题的感觉真的很难受啊。

。。

。。。

。。。。

这题的wa点居然是!!!!!火源不只有一个!!!!!!!!!!!!!!神坑啊!!!!!!

为啥A了的人都不说一声啊!!!那样误导人的样例是个啥玩意儿啊!!!(摔键盘)

下面代码:

/*
 ━━━━━┒
 ┓┏┓┏┓┃μ'sic foever!!
 ┛┗┛┗┛┃\○/
 ┓┏┓┏┓┃ /
 ┛┗┛┗┛┃ノ)
 ┓┏┓┏┓┃
 ┛┗┛┗┛┃
 ┓┏┓┏┓┃
 ┛┗┛┗┛┃
 ┓┏┓┏┓┃
 ┛┗┛┗┛┃
 ┓┏┓┏┓┃
 ┃┃┃┃┃┃
 ┻┻┻┻┻┻
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
const int maxn=1020;
const int INF=80000000;
int d[maxn][maxn];
char save[maxn][maxn];
struct unit{
	int x,y;
}fire[2000000];
int fire_n=0,fire_start;
int xj,yj,xf,yf;
int r,c;
int move_x[4]={1,-1,0,0};
int move_y[4]={0,0,1,-1};
int result;
bool operator<(unit a , unit b){
    return d[a.x][a.y]>d[b.x][b.y];
}
void bfs(){
	queue<unit> que;
	//priority_queue<unit> que; 
	int i,j;
	for(i=0;i<=r+1;i++){
		for(j=0;j<=c+1;j++){
			d[i][j]=INF;
		}
	}
	unit now;
	now.x=xj,now.y=yj;
	que.push(now);
	d[xj][yj]=0;
	bool flag=true;
	int before=0;
	while(que.size()){
		now=que.front();
		//cout<<now.x<<" "<<now.y<<" "<<d[now.x][now.y]<<endl;
		que.pop();
		if(now.x<1||now.x>r||now.y<1||now.y>c){
			result=min(result,d[now.x][now.y]);
			break;
		}
		int nx,ny;
		
		//if(d[now.x][now.y]>before){
		if(before==0){
			flag=true;
		}else if(d[que.front().x][que.front().y]>before){
			flag=true;
		}else{
			flag=false;
		}
		if(flag){
			int ans=0;
			for(i=fire_start;i<fire_n;i++){
				for(int count=0;count<4;count++){
					int fx=fire[i].x+move_x[count],fy=fire[i].y+move_y[count];
						if(fx>=1&&fx<=r&&fy>=1&&fy<=c&&save[fx][fy]=='.'){
							unit fire_now;
							fire_now.x=fx;
							fire_now.y=fy;
							fire[fire_n+ans]=fire_now;
							ans++;
							save[fx][fy]='F';
						}
				}
			}
			fire_start=fire_n;
			fire_n+=ans;
			before++;
		}
		
		for(i=0;i<4;i++){
			nx=now.x+move_x[i],ny=now.y+move_y[i];
			if(nx>=0&&nx<=r+1&&ny>=0&&ny<=c+1&&save[nx][ny]=='.'&&d[nx][ny]==INF){
				unit change;
				change.x=nx;
				change.y=ny;
				save[now.x][now.y]='.';
				d[nx][ny]=d[now.x][now.y]+1;
				que.push(change);
			}
		}
		
		
		
	}
}
int main(){
    int t,i,j;
    scanf("%d",&t);
    while(t--){
    	scanf("%d%d",&r,&c);
    	result=INF;
		fire_n=0;
		fire_start=0;
		unit killme;
    	for(i=1;i<=r;i++){
    		for(j=1;j<=c;j++){
    			cin>>save[i][j];
    			if(save[i][j]=='J'){
    				xj=i,yj=j;
				}
				if(save[i][j]=='F'){
					xf=i,yf=j;
					killme.x=xf;
					killme.y=yf;
					fire[fire_n]=killme;
					fire_n++;
				}
			}
		}
		for(i=1;i<=c;i++){
			save[0][i]='.';
			save[r+1][i]='.';
		}
		for(i=1;i<=r;i++){
			save[i][0]='.';
			save[i][c+1]='.';
		}
		bfs();
		if(result==INF){
			cout<<"IMPOSSIBLE"<<endl;
		}else{
			cout<<result<<endl;	
		}
	}
    return 0;
}


/*
2
18 14
.#.....#.#.##.
.......#..#...
....#........#
..#.#.#.#....#
....#..##.....
##......#....#
#.#.#..##..###
#..#........##
#.##....#..##.
F#..#....J##..
....##........
..............
...##..#......
#...#.#.....##
....#..##...#.
..........##..
...##..##.##..
..#...#.......
21 13
#.......#.#..
..#.##.#...##
.#.##.#.#....
......#......
.#..###.##.#.
.......#.....
..#.........#
#..#..#......
##......#....
#...#.....#..
.#F..#.#.....
.#.#..##.#.#.
......#....#.
.............
#.##..J#....#
##......#....
.....#..#..#.
..........#..
#..#.#...#.#.
..#....#.#.#.
#..........##
*/


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值