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

首先对火进行一次bfs,得到着火时间,然后对人进行一次bfs,只允许进入还没有着火的点

注意:出迷宫条件是从任何一墙出去,过墙需要1时间

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=1003;
const int inf=0x3fffffff;
char maz[maxn][maxn];
int time[maxn][maxn];
int dp[maxn][maxn];
int n,m; queue<int> que;
const int dx[8]={0,0,1,-1,1,1,-1,-1};
const int dy[8]={1,-1,0,0,1,-1,1,-1};
struct pnt {
int x,y;
pnt(){x=y=0;}
pnt(int tx,int ty){x=tx,y=ty;}
};
bool in(int x,int y){
return x>=0&&x<n&&y>=0&&y<m;
}
void bfs(){
while(!que.empty()){
int x=que.front()/maxn,y=que.front()%maxn;que.pop();
for(int i=0;i<4;i++){
int tx=x+dx[i],ty=y+dy[i];
if(in(tx,ty)&&maz[tx][ty]!='#'&&time[tx][ty]>time[x][y]+1){
time[tx][ty]=time[x][y]+1;
que.push(tx*maxn+ty);
}
}
}
}
int bfs2(int sx,int sy){
while(!que.empty())que.pop();
dp[sx][sy]=0;
que.push(sx*maxn+sy);
while(!que.empty()){
int x=que.front()/maxn,y=que.front()%maxn;que.pop();
for(int i=0;i<4;i++){
int tx=x+dx[i],ty=y+dy[i];
if(!in(tx,ty)){
return dp[x][y]+1;
}
if(maz[tx][ty]!='#'&&time[tx][ty]>dp[x][y]+1&&dp[tx][ty]>dp[x][y]+1){
dp[tx][ty]=dp[x][y]+1;
que.push(tx*maxn+ty);
}
}
}
return -1;
}
void init(){
while(!que.empty())que.pop();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
time[i][j]=inf;
dp[i][j]=inf;
}
} }
int main(){
int T;
scanf("%d",&T);
for(int ti=1;scanf("%d%d",&n,&m)==2&&ti<=T;ti++){
for(int i=0;i<n;i++){
scanf("%s",maz[i]);
}
init();
int sx,sy;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(maz[i][j]=='F'){
que.push(i*maxn+j);
time[i][j]=0;
}
else if(maz[i][j]=='J'){
sx=i;
sy=j;
}
}
}
bfs();
int ans=bfs2(sx,sy); if(ans!=-1)printf("%d\n",ans);
else puts("IMPOSSIBLE");
}
return 0;
}

  

UVA 11624 Fire! bfs 难度:0的更多相关文章

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

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

  2. UVA 11624 Fire! (bfs)

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

  3. UVA 11624 Fire! BFS搜索

    题意:就是问你能不能在火烧到你之前,走出一个矩形区域,如果有,求出最短的时间 分析:两遍BFS,然后比较边界 #include<cstdio> #include<algorithm& ...

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

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

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

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

  6. E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)

    E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ...

  7. UVA - 11624 多点bfs [kuangbin带你飞]专题一

    题意:某人身陷火场,总有k个点着火,着火点可向四周扩散,问此人能否逃离. 思路:可能有多个着火点,以这些着火点作为起点进行bfs,得到整个火场的最短距离,然后又以人所在坐标作为起点进行bfs,得到该人 ...

  8. UVA 11624 - Fire! 图BFS

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

  9. UVa 11624 Fire!(BFS)

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

随机推荐

  1. JAVA参数没有引用传递,只有值传递

    原文章地址:http://www.cnblogs.com/clara/archive/2011/09/17/2179493.html 当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性, ...

  2. 记一次踩坑:使用ksoap-android时造成的okhttp依赖冲突问题

    项目中需要调用webservice接口,android SDK中并没有直接访问webservice接口的方法,于是我引入了ksoap-android的jar包,来实现访问webservice接口.刚开 ...

  3. Python多进程编程(转)

    原文:http://www.cnblogs.com/kaituorensheng/p/4445418.html 阅读目录 1. Process 2. Lock 3. Semaphore 4. Even ...

  4. mysql 数据操作 单表查询 group by 练习

    小练习: 1. 查询岗位名以及岗位包含的所有员工名字 mysql> select post,group_concat(name) from employee group by post ; +- ...

  5. mysql 数据操作 单表查询 where约束 between and or

    WHERE约束 where字句中可以使用: 比较运算符:>< >=  <=  != between 80 and 100 值在80到100之间   >=80  <= ...

  6. python 面向对象 私有属性

    __init__构造函数 self.name = name # 属性, 实例变量,成员变量,字段 def sayhi()# 方法, 动态属性 私有属性不对外看到 前面加上__ class role() ...

  7. idea中添加模板。

    1:点击File>settings>live template 2: 在 Editor界面下,点击右上角 + 好, 如果想添加一个新类型的语言,点击templateGroup  输入组名. ...

  8. PAT 1143 Lowest Common Ancestor[难][BST性质]

    1143 Lowest Common Ancestor(30 分) The lowest common ancestor (LCA) of two nodes U and V in a tree is ...

  9. swoole gets

    控制器调用: function gets() { $model = Model('ap_pic'); $model->select = ' id, size_type '; $gets['pag ...

  10. HDU5183 hash 表

    做题的时候忘了 数据结构老师说的hash表了, 用二分找,还好过了, hash 表 对这题 更快一些 #include <iostream> #include <algorithm& ...