关闭

uva11624 Fire! (双bfs)

标签: 双bfsuva11624fire
1825人阅读 评论(0) 收藏 举报
分类:

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


题目链接

思路:由于题中没有说有几个起火点 所以 要用一个队列来维护。

当在T秒时人移动过后 对火点进行扩散

#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
char map[1005][1005];
bool vis[1005][1005];
bool vis1[1005][1005];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
int n,m,st_x,st_y;

struct node
{
	int x,y,t;
	friend bool operator <(node a,node b)
	{
		return a.t>b.t;
	}
};
struct node1
{
	int x,y,t;
};
bool limit(int x,int y)
{
	if(x<0||x==n||y<0||y==m||vis[x][y]||map[x][y]=='#'||map[x][y]=='F')
	return false;
	return true;
}
queue<node1>fire;
void fire_spread()
{
	node1 temp1,temp2;
	int T=fire.front().t;
	while(!fire.empty())
	{
		temp1=temp2=fire.front();
	//	printf("**%d %d %d**\n",temp1.x,temp1.y,temp1.t);
		if(temp1.t>T)
		break;
		fire.pop();
		for(int i=0;i<4;i++)
		{
			int xx=temp1.x+dir[i][1];
			int yy=temp1.y+dir[i][0];
			if(!vis1[xx][yy]&&map[xx][yy]!='#'&&xx>=0&&xx<n&&yy>=0&&yy<m)
			{
			//	printf("%d %d %d\n",xx,yy,temp1.t);
				vis1[xx][yy]=true;
				vis[xx][yy]=true;
				temp1.x=xx;temp1.y=yy;temp1.t++;
				fire.push(temp1);
			}
			temp1=temp2;
		}
	}
}
int bfs()
{
	node temp1,temp2;
	priority_queue<node>s;
	while(!s.empty()) s.pop(); 
	temp1.x=st_x,temp1.y=st_y,temp1.t=0;
	s.push(temp1);
	while(!s.empty()) 
	{
		temp1=temp2=s.top();s.pop();
		//要让所有t时间的人走完  才对火进行扩散 
		if(temp1.t>fire.front().t)
		fire_spread();
		if(vis[temp1.x][temp1.y]) continue;
		if(temp1.x==0||temp1.x==n-1||temp1.y==0||temp1.y==m-1)
		return temp1.t;
		vis[temp1.x][temp1.y]=true;
		for(int i=0;i<4;i++)
		{
			int xx=temp1.x+dir[i][0];
			int yy=temp1.y+dir[i][1];
			if(limit(xx,yy))
			{
				temp1.x=xx;temp1.y=yy;temp1.t++;
				s.push(temp1);
			}
			temp1=temp2;
		}
	}
	return -1;
}
int main()
{
	int ncase;
	scanf("%d",&ncase);
	while(ncase--)
	{
		memset(map,false,sizeof(map));
		memset(vis,false,sizeof(vis));
		memset(vis1,false,sizeof(vis1));
		while(!fire.empty()) fire.pop();
		scanf("%d %d",&n,&m);
		for(int i=0;i<n;i++)
		{
			getchar();
			for(int j=0;j<m;j++)
			{
				scanf("%c",&map[i][j]);
				if(map[i][j]=='F')
				{
					node1 temp;
					vis[i][j]=vis1[i][j]=true;
					temp.x=i,temp.y=j,temp.t=0;
					fire.push(temp);
				}
				if(map[i][j]=='J')
				st_x=i,st_y=j;
			}
		}
		int result=bfs();
		if(result==-1)
		printf("IMPOSSIBLE\n");
		else
		printf("%d\n",result+1);
	}
	return 0;
} 


0
0
查看评论

UVA11624 Fire! 两次BFS 读懂题意很重要

Fire! 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. He...
  • wr132
  • wr132
  • 2015-04-30 21:17
  • 1877

UVA11624-Fire!(两次bfs)

题目链接 题意:你的任务是帮助J走出一个大火蔓延的迷宫。J每分钟可以超上下左右四个方向移动,而所有着火的格子都会往四周蔓延。迷宫中有一些障碍,J和火都无法进入。当J走到一个迷宫的边界格子时,我们认为他已经走出了迷宫。 思路:这是大白上面的一道题目,其实只要将每个格子什么时间着火处理出来就可...
  • u011345461
  • u011345461
  • 2014-09-24 22:51
  • 898

UVa 11624 - Fire!

题目:一个平面迷宫中有一个人,迷宫中有些点起火了,火和人每个单位时间只能向相邻的格子移动,             其中有一些空间被墙壁占据,问这个人在不背或烧到的情况下,离开迷宫的最快时间。 分析:搜索。迷宫中的最短路,首先就会想到b...
  • mobius_strip
  • mobius_strip
  • 2014-03-19 14:58
  • 3243

UVA 11624 Fire!

题意: J要逃出迷宫,F代表火焰,点着后会向上下左右四个方向蔓延,火蔓延到的地方F都不能走,问F是否可以逃出去 思路: 可以先广搜一遍标记火药到达每个地方的最短时间,再跑F,F只能在标记的时刻之前走过(白书方法) 其实这题就是求J走出迷宫的最短路,只是多了一个火焰的限制条件,那么我们可以把F...
  • Doris1104
  • Doris1104
  • 2016-01-25 22:28
  • 397

UVA10300(理解题意就容易切)

Ecological Premium Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem....
  • u013447865
  • u013447865
  • 2014-11-30 12:56
  • 498

uva 11624 - Fire!(BFS)

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...
  • ocgcn2010
  • ocgcn2010
  • 2014-07-17 12:50
  • 789

[kuangbin带你飞]专题一 简单搜索 J

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2671 Joe works in a maze. Unfortunately, ...
  • zjy2015302395
  • zjy2015302395
  • 2016-11-19 18:49
  • 545

uva11624

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=65959#problem/J 发个题目链接吧,文章复制不成功,就是给一个二维图,#  .    F   J  ,# 表示墙,.表示路,F表...
  • yuanjunlai141
  • yuanjunlai141
  • 2016-03-11 14:31
  • 204

UVA11624大火蔓延的迷宫

题意:      给1个n*m的网格,上面有的点能走,有的点不能走(墙),然后有的点是火源,火源和人一样,每次都是上下左右四个方向蔓延,速度一样是1,火也不可以从墙上跨过去,给你人的起点,终点是只要走到边界就行,就是走出矩阵,问你最小逃生时间。 思路: &...
  • u013761036
  • u013761036
  • 2014-10-16 16:00
  • 1442

uva 11624 大火蔓延的迷宫 Fire!(两次bfs)

 题目:一个平面迷宫中有一个人,迷宫中有些点起火了,火和人每个单位时间只能向相邻的格子移动,             其中有一些空间被墙壁占据,问这个人在不背或烧到的情况下,离开迷宫的最快时间。 思路是先用bfs预处理每个格子起火...
  • u014664226
  • u014664226
  • 2015-05-15 23:39
  • 1043
    个人资料
    • 访问:1429942次
    • 积分:21095
    • 等级:
    • 排名:第441名
    • 原创:602篇
    • 转载:18篇
    • 译文:0篇
    • 评论:376条
    你只管努力 其它交给时光

    点图片联系我


    唐唐女神

    点图片联系我

    访问统计
    Flag Counter
    博客专栏
    最新评论