Fire!

这道题目需要进行两次广搜,一次是对火的,一次是对Joe的,只要保证Joe在到达那点时火灾没有蔓延到,同时要注意火灾可能有多处(可能也存在没有火灾的情况),在写代码的时候只要Joe到达边界即可判定他成功逃脱,而不是他越过边界后才停止循环,我的两次Runtime Error就是这么造成的

Problem B: 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
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct list
{
	int x;
	int y;
	int step;
};
bool checkFire(int x,int y);
bool checkJoe(int x,int y,int t);
bool check(int x,int y);
char map[1001][1001];
int time[1001][1001];
bool book[1001][1001];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int r,c;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		queue<struct list> p,q;
		struct list n,F;
		memset(map,0,sizeof(map));
		memset(time,-1,sizeof(time));
		memset(book,false,sizeof(book));
		scanf("%d%d",&r,&c);
		for(int i=0;i<r;i++)
		{
			for(int j=0;j<c;j++)
			{
				scanf(" %c",&map[i][j]);
				if(map[i][j]=='J')
				{
					n.x=i;
					n.y=j;
					n.step=0;
					q.push(n);
				}
				if(map[i][j]=='F')
				{
					F.x=i;
					F.y=j;
					F.step=0;
					time[i][j]=0;
					p.push(F);
				}
			}
		}		
		while(!p.empty())
		{
			struct list P;
			P=p.front();
			p.pop();
			for(int i=0;i<4;i++)
			{				
				if(checkFire(P.x+dir[i][0],P.y+dir[i][1]))
				{
					F.x=P.x+dir[i][0];
					F.y=P.y+dir[i][1];
					F.step=P.step+1;
					time[P.x+dir[i][0]][P.y+dir[i][1]]=P.step+1;
					p.push(F);
				}
			}			
		}
		while(!q.empty()&&!check(q.front().x,q.front().y))
		{
			struct list Q;
			Q=q.front();
			for(int i=0;i<4;i++)
			{
				if(checkJoe(Q.x+dir[i][0],Q.y+dir[i][1],Q.step+1))
				{
					book[Q.x+dir[i][0]][Q.y+dir[i][1]]=true;
					n.x=Q.x+dir[i][0];
					n.y=Q.y+dir[i][1];
					n.step=Q.step+1;
					q.push(n);
				}
			}
			q.pop();
		}
		if(q.empty())
			printf("IMPOSSIBLE\n");
		else
			printf("%d\n",q.front().step+1);
	}
	return 0;
}
bool checkFire(int x,int y)
{
	if(x>=0&&x<r&&y>=0&&y<c)
		if(time[x][y]==-1)
			if(map[x][y]=='.'||map[x][y]=='J')
				return true;
	return false;
}
bool checkJoe(int x,int y,int t)
{
	if(t<time[x][y]||time[x][y]==-1)
		if(book[x][y]==false)
			if(map[x][y]=='.')
				return true;
	return false;
}
bool check(int x,int y)
{
	if(x==0||x==r-1||y==0||y==c-1)
		return true;
	return false;
}
	




下面是补充完整的代码: ``` // 你的代码将嵌入这里 class Bullet { private int count = 0; private boolean loaded = false; public synchronized void addBullet() { while (loaded) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } count++; System.out.println("lock and load~~~~"); loaded = true; notifyAll(); } public synchronized void shootBullet() { while (!loaded) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("fire!!!"); loaded = false; count--; notifyAll(); } public synchronized boolean isEmpty() { return count == 0; } } class AddBullet implements Runnable { private Bullet bullet; public AddBullet(Bullet bullet) { this.bullet = bullet; } @Override public void run() { while (true) { if (bullet.isEmpty()) { bullet.addBullet(); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } class ShootBullet implements Runnable { private Bullet bullet; public ShootBullet(Bullet bullet) { this.bullet = bullet; } @Override public void run() { int count = 0; while (count < 3) { if (!bullet.isEmpty()) { bullet.shootBullet(); count++; } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Main { public static void main(String[] args) { Bullet bullet = new Bullet(); AddBullet ab = new AddBullet(bullet); ShootBullet sb = new ShootBullet(bullet); Thread t1 = new Thread(ab); Thread t2 = new Thread(sb); t1.start(); t2.start(); } } ``` 运行结果: ``` lock and load~~~~ fire!!! lock and load~~~~ fire!!! lock and load~~~~ fire!!! ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值