uva Fire!
算法:BFS
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
注意开始有多个火堆;
代码:
#include <iostream>
#include <cstring>
#include <iomanip>
#include <stdio.h>
#include <queue>
#include <algorithm>
#define INF 100000000
using namespace std;
char ch[1005][1005];
int b[1005][1005],c[1004][1005],qq,cnt,d[1005][2];
int n,m,a[4][2]={-1,0,0,-1,0,1,1,0},p,q;
struct dot
{
int x,y,step;
};
void bfs()
{ memset(c,0,sizeof(c));
queue<dot>que;
dot cur,loer;
for(int i=0;i<cnt;i++)
{
cur.x=d[i][0];
cur.y=d[i][1];
cur.step=0;
b[cur.x][cur.y]=0;
c[cur.x][cur.y]=1;
que.push(cur);
}
while(que.size())
{
loer=que.front();
que.pop();
for(int i=0;i<4;i++)
{
int dx,dy;
dx=loer.x+a[i][0];
dy=loer.y+a[i][1];
if(dx>=0&&dx<n&&dy>=0&&dy<m&&ch[dx][dy]=='.'&&!c[dx][dy])
{
cur.x=dx;cur.y=dy;cur.step=loer.step+1;
b[dx][dy]=min(cur.step,b[dx][dy]);
c[dx][dy]=1;
que.push(cur);
}
}
}
}
void bsf()
{ memset(c,0,sizeof(c));
queue<dot>que;
dot cur,loer;
cur.x=p;
cur.y=q;
cur.step=0;
c[p][q]=1;
que.push(cur);
while(que.size())
{
loer=que.front();
que.pop();
if(loer.x==0||loer.x==n-1||loer.y==0||loer.y==m-1)
{ qq=1;
cout<<loer.step+1<<endl;
break;
}
for(int i=0;i<4;i++)
{
int dx,dy;
dx=loer.x+a[i][0];
dy=loer.y+a[i][1];
if(dx>=0&&dx<n&&dy>=0&&dy<m&&ch[dx][dy]=='.'&&!c[dx][dy]&&b[dx][dy]>loer.step+1)
{
cur.x=dx;
cur.y=dy;
cur.step=loer.step+1;
c[dx][dy]=1;
que.push(cur);
}
}
}
}
int main()
{
int T,i,j,k;
cin>>T;
while(T--)
{
cin>>n>>m;
cnt=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>ch[i][j];
b[i][j]=INF;
if(ch[i][j]=='J')
{
p=i;q=j;
ch[i][j]='.';
}
else if(ch[i][j]=='F')
{
d[cnt][0]=i;
d[cnt++][1]=j;
}
}
}
qq=0; bfs();
bsf();
if(qq==0) cout<<"IMPOSSIBLE"<<endl;
}
return 0;
}
uva Fire!的更多相关文章
- UVa 11624 Fire!(着火了!)
UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- BFS(两点搜索) UVA 11624 Fire!
题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...
- UVA 11642 Fire!
Fire! Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 116 ...
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)
E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ...
- UVA 11624 Fire!(广度优先搜索)
题目大意:在一个N*M的迷宫内,J代表某人(只有一个),F代表火(可能不只一个),#代表墙,火每分钟会向四周除了墙以外的地方扩散一层,问人能否在没被火烧到 之前逃出迷宫,若能逃出输出最短时间.很明显的 ...
- UVa 11624 Fire!(BFS)
Fire! Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %lld & %llu Description Joe ...
- uva 11624 Fire!(搜索)
开始刷题啦= = 痛并快乐着,学到新东西的感觉其实比看那些无脑的小说.电视剧有意思多了 bfs裸体,关键是先把所有的着火点放入队列,分开一个一个做bfs会超时的 发现vis[][]是多余的,完全可以用 ...
随机推荐
- shell的string operator
${varname:-word} 如果varname存在并且不为nil,那么返回varname的值,否则返回word.这个常用来在varname未定义时返回默认值 ${varname:=word} 如 ...
- HTML学习笔记之meta标签
一.meta标签的组成 meta标签共有两个属性,它们分别是http-equiv属性和name属性,不同的属性又有不同的参数值,这些不同的参数值就实现了不同的网页功能. 1.name属性 name属性 ...
- php 初学笔记
1.变量定义和使用 php中定义变量名为:$aa 在类中一般定义一个新变量需要添加var字,如var $aaa. 但是过程或函数中是不需要添加var 关键字,如$aaa=$_POST['aaaa'], ...
- 如何在symfony 控制器里面创建soap web service
通过一些工具将一个控制器设置成一个soap服务将会非常简单.首先,你必须安装了php soap扩展.由于php soap扩展现在不能生成wsdl,你要么自己从头开始创建要模使用第三方生成器. php中 ...
- UCOS 内存管理理解 创建任务
OS_MEM *OSMemCreate (void *addr, INT32U nblks, INT32U blksize, INT8U *err) { ................... ...
- iOS设备后台播放音乐方法
iOS设备后台播放音乐方法 1 在设置Capabliites中打开Background Modes,选择Audio And AirPlay 2 在控制viewDidLoad中添加下面代码 AVAudi ...
- 数据结构&&算法基础知识
写本篇主要是为了将基础知识梳理一遍,天天加一些基本东西,以后复习时可以返回来看看. 数据结构&&基础算法: 基本算法: 二分查找 二叉树: 二叉树的各种遍历 位操作: 排序: 排序算法 ...
- 基于QT开发的第三方库
基于Qt开发的第三方库 分类: Qt2014-02-12 11:34 1738人阅读 评论(0) 收藏 举报 QT第三方库 目录(?)[+] 文章来源:http://blog.csdn.net ...
- linq 跨库查询
可以用多个DBContext,例如有DBContext1和DBContext2,但是不能将两个DBContext用在同一个查询中,可以分开,先用一个查出结果集1,再在第二个查询中使用结果集1就可以了
- bzoj 1195
http://www.lydsy.com/JudgeOnline/problem.php?id=1195 状压DP. 首先去掉被包含的字符串. 对于字符串i和j,我们求出 当字符串j的左端点在字符串i ...