先对火BFS一次,求出每个点的最小着火时间。

再对人BFS一次,求出走到边界的最少时间。

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int n,m,bz[1005][1005],qihuo[1005][1005];
char map[1005][1005];
int h[4][2]={-1,0,1,0,0,-1,0,1};
struct point
{
int x,y,step;
};
queue<point> q;
void bfs_huo()
{
int i,j,x,y;
point t,tt;
while(!q.empty())
{
t=q.front(); q.pop();
for(i=0;i<4;i++)
{
x=t.x+h[i][0]; y=t.y+h[i][1];
if(x>=0&&x<n&&y>=0&&y<m&&(map[x][y]=='.'||map[x][y]=='J')&&!bz[x][y])
{
bz[x][y]=1;
tt.x=x; tt.y=y; tt.step=t.step+1; qihuo[x][y]=tt.step;
q.push(tt);
}
}
}
}
int bfs_men(point s)
{
int i,j,x,y;
point t,tt;
while(!q.empty()) q.pop();
q.push(s); bz[s.x][s.y]=1;
while(!q.empty())
{
t=q.front(); q.pop();
if(t.x<=0||t.x==n-1||t.y<=0||t.y==m-1) return t.step; //??磬?0
for(i=0;i<4;i++)
{
x=t.x+h[i][0]; y=t.y+h[i][1];
if(x>=0&&x<n&&y>=0&&y<m&&(map[x][y]=='.'||map[x][y]=='J')&&!bz[x][y])
{
if(qihuo[x][y]<=t.step+1) continue;
bz[x][y]=1;
tt.x=x; tt.y=y; tt.step=t.step+1;
q.push(tt);
}
}
}
return -1;
}
int main(int argc, char *argv[])
{
int nn,i,j;
point s,t;
cin>>nn;
while(nn--)
{
cin>>n>>m;
while(!q.empty()) q.pop();
memset(bz,0,sizeof(bz));
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
cin>>map[i][j]; qihuo[i][j]=1000000000; //???
if(map[i][j]=='J') {s.x=i; s.y=j; s.step=0;}
if(map[i][j]=='F') {t.x=i; t.y=j; t.step=0; q.push(t); bz[i][j]=1; qihuo[i][j]=0;}
}
bfs_huo();
memset(bz,0,sizeof(bz));
int ans=bfs_men(s);
if(ans>=0) cout<<ans+1<<endl;
else cout<<"IMPOSSIBLE"<<endl;
}
return 0;
}

UVA 11624 Fire!(二次BFS)的更多相关文章

  1. BFS(两点搜索) UVA 11624 Fire!

    题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...

  2. UVa 11624 Fire!(着火了!)

    UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...

  3. UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次

    UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...

  4. UVA - 11624 Fire! 【BFS】

    题意 有一个人 有一些火 人 在每一秒 可以向 上下左右的空地走 火每秒 也会向 上下左右的空地 蔓延 求 人能不能跑出来 如果能 求最小时间 思路 有一个 坑点 火是 可能有 多处 的 样例中 只有 ...

  5. UVA 11624 - Fire! 图BFS

    看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...

  6. UVa 11624 Fire!(BFS)

    Fire! Time Limit: 5000MS   Memory Limit: 262144KB   64bit IO Format: %lld & %llu Description Joe ...

  7. UVA 11624 Fire! (bfs)

    算法指南白书 分别求一次人和火到达各个点的最短时间 #include<cstdio> #include<cstring> #include<queue> #incl ...

  8. (简单) UVA 11624 Fire! ,BFS。

    Description Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ow ...

  9. UVA 11624 Fire! bfs 难度:0

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

随机推荐

  1. linux find grep使用

    在当前目录下所有文件中查找内容包含 string 的文件: find ./ -name "*" -exec grep "string" {} \; 注意:在最后 ...

  2. Installing the .NET Framework 4.5, 4.5.1

    This article provides links for installing the .NET Framework 4.5 and 4.5.1 on your computer.  If yo ...

  3. JDK1.8 Lambda

    1.模拟Model /** * Author:JsonLu * DateTime:16/12/8 14:01 * Email:jsonlu@qq.com * Desc: */ public class ...

  4. 【原创教程】一、Angular教程系列之认识angular

    为什么我会准备写这个原创教程系列? 写下这个标题之后,看着屏幕上空白的内容区,不知从何下手,想说的似乎有很多,似乎又没啥说的.有时候就会陷入这种矛盾中,有时候就是这样,于是,我下定决心这一次一定要把这 ...

  5. [配置文件] C#修改App.config,Web.config文件帮助类,ConfigHelper (转载)

    点击下载 ConfigHelper-sufei.rar 主要功能如下 .根据Key取Value值 .根据Key修改Value .添加新的Key ,Value键值对 .根据Key删除项 /// < ...

  6. 关于wordpress中更换CKEditor编辑器

    wordpress中自带的编辑器实在是功能太简,连插入表格都没有,使用插件的方式太过于麻烦,干脆就直接更换编辑器了,在网上找了一些方法,下文引自http://down.chinaz.com/try/2 ...

  7. 解决IE无法解析json的方法

    一.原始方法eval() 二.json插件--github开源插件:json-js/json2.js https://github.com/douglascrockford/JSON-js/blob/ ...

  8. C# 之【线程与进程】

    1.  引言 先来个比喻手法: 如果把上课的过程比作进程,那么每个学生就是一个线程,他们共享教室,即线程共享进程的内存空间.每一个时刻,只能一个学生问老师问题,老师回答完毕,轮到下一个.即线程在一个时 ...

  9. prmopt 提示框接收字符串,输入后按确定弹出警告框,警告内容为逆序的字符串

    虽然已经找到offer,但因为公司还没安排实习,所以在学校的时间多了很多.好吧,这段时间我用来备考四级啦(好悲催,还没过),然后这一天,闲着无聊,就帮妹妹看了这样子一道题目啦. 题目内容: 编制一个从 ...

  10. Java 设计模式_复合模式(2016-08-31)

    一.什么是复合模式? 在形式上,复合模式确实是多个模式的组合,但满足了这一条并不一定是复合模式,注意它的定义: 将多个模式结合起来形成一个“框架”,以解决一般性问题 一提到“框架”,可能最容易联想到的 ...