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[][]是多余的,完全可以用 ...
随机推荐
- Linux 下安装python软件包
1.安装setuptools 下载地址:https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py 解压: tar zxvf set ...
- uva 10894 - Save Hridoy
#include <iostream> #include <string> #include <cstring> #include <cmath> us ...
- C# 调用浏览器打开网址
private void button1_Click(object sender, EventArgs e) { //调用系统默认的浏览器 System.Diagnostics.Process.Sta ...
- angular-route 里面templeteUrl 动态加载
https://segmentfault.com/q/1010000002524964
- code first 尝试
建表: 1.先用EF连接数据库,配置connectionStrings <configSections> <!-- For more information on Entity Fr ...
- git新手碰到的各种奇葩问题之一
git 操作错误: <1>.情景描述:当在git commit --amend 更新上一次提交时,而此时提交日志会跳转到别人的日志中.,会出现错误:如下 弥补操作: 1.git fetc ...
- android 数据存储的几种方式
总体的来讲,数据存储方式有三种:一个是文件,一个是数据库,另一个则是网络.其中文件和数据库可能用的稍多一些,文件用起来较为方便,程序可以自己定义格式:数据库用起稍烦锁一些,但它有它的优点,比如在海量数 ...
- SQL Fetch size
JDBC performance tuning with optimal fetch size February 1, 2009 31 Comments Tuning performance usin ...
- Linux企业级项目实践之网络爬虫(1)——项目概述及准备工作
我们在学习了Linux系统编程之后,需要一些实战项目来提高自己的水平,本系列我们通过编写一个爬虫程序,将我们学习的知识进行综合应用,同时在实现项目的过程中逐渐养成一些有用的思维方式,并具有初步的软件开 ...
- hdu4085
http://acm.hdu.edu.cn/showproblem.php?pid=4085 斯坦纳树. 用状压DP. 一共有2K个关键点:1,2...,K和N-K+1,N-K+2...,N,我们用一 ...