Description

  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.

  题目就是迷宫问题,然后加了火,不过这个题被坑了,原来火不只是一个,可以有很多个。。。

  这个题先从火BFS一遍,这样就可以知道火多长时间后烧到某一块,然后从人BFS,当走到这时火已经烧过来的话就算不能通过就好了。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath> using namespace std; bool map1[][];
int rem[][];
int Frem[][];
int N,M;
int Si,Sj,Fi[],Fj[];
int cou; bool judge(int x,int y,int temp)
{
if(x<=||y<=||x>N||y>M)
return ; if(!map1[x][y])
return ; if(rem[x][y])
return ; if(Frem[x][y]>-&&temp>=Frem[x][y])
return ; return ;
} bool judge1(int x,int y)
{
if(x<=||y<=||x>N||y>M)
return ; if(!map1[x][y])
return ; if(Frem[x][y]!=-)
return ; return ;
} int slove()
{
queue <int> que;
int temp,t1,t2; que.push(Si*+Sj);
rem[Si][Sj]=; while(!que.empty())
{
temp=que.front();
que.pop(); t1=temp/;
t2=temp%;
temp=rem[t1][t2]; if(t1==||t2==||t1==N||t2==M)
return temp; --t1;
if(judge(t1,t2,temp))
{
que.push(t1*+t2);
rem[t1][t2]=temp+;
}
t1+=;
if(judge(t1,t2,temp))
{
que.push(t1*+t2);
rem[t1][t2]=temp+;
}
--t1;
--t2;
if(judge(t1,t2,temp))
{
que.push(t1*+t2);
rem[t1][t2]=temp+;
}
t2+=;
if(judge(t1,t2,temp))
{
que.push(t1*+t2);
rem[t1][t2]=temp+;
}
} return -;
} void init()
{
queue <int> que;
int temp,t1,t2;
for(int i=;i<cou;++i)
{
que.push(Fi[i]*+Fj[i]);
Frem[Fi[i]][Fj[i]]=;
} while(!que.empty())
{
temp=que.front();
que.pop(); t1=temp/;
t2=temp%;
temp=Frem[t1][t2]; --t1;
if(judge1(t1,t2))
{
que.push(t1*+t2);
Frem[t1][t2]=temp+;
}
t1+=;
if(judge1(t1,t2))
{
que.push(t1*+t2);
Frem[t1][t2]=temp+;
}
--t1;
--t2;
if(judge1(t1,t2))
{
que.push(t1*+t2);
Frem[t1][t2]=temp+;
}
t2+=;
if(judge1(t1,t2))
{
que.push(t1*+t2);
Frem[t1][t2]=temp+;
}
}
} int main()
{
ios::sync_with_stdio(false); int T;
int ans;
char c;
cin>>T; while(T--)
{
cou=;
cin>>N>>M;
for(int i=;i<=N;++i)
for(int j=;j<=M;++j)
{
rem[i][j]=;
Frem[i][j]=-;
cin>>c;
if(c=='#'||c=='F')
map1[i][j]=;
else
map1[i][j]=; if(c=='J')
Si=i,Sj=j;
if(c=='F')
{
Fi[cou]=i;
Fj[cou++]=j;
}
} init(); ans=slove(); if(ans==-)
cout<<"IMPOSSIBLE\n";
else
cout<<ans<<endl;
} return ;
}

(简单) UVA 11624 Fire! ,BFS。的更多相关文章

  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 难度:0

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

  4. UVA 11624 Fire! BFS搜索

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

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

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

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

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

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

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

  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. 【jsp exception】如何处理jsp页面的错误

    根据jsp对错误的处理方式不同可以将其分为局部异常处理和全局异常处理.局部异常处理适用于个别jsp页面,当这些页面发生错误后,采取特殊的处理方式:全局异常处理适用于所有jsp页面,当所有页面发生某些指 ...

  2. initWithFrame、initWithCoder、awakeFromNib的区别和调用次序 & UIViewController生命周期 查缺补漏

    当我们创建或者自定义一个UI控件时,就很可能会调用awakeFromNib.initWithCoder .initWithFrame这些方法.三者的具体区别如下: initWithFrame: 通过代 ...

  3. keyboardWillChangeFrameNotification 引发的思考 是的 思考了很久终于出结果

    func keyboardWillChangeFrameNotification(note: NSNotification) { // TODO 添加键盘弹出的事件 let userinfo = no ...

  4. 关于SVN更新注意

    SVN更新时其实大部分都可以直接更新但是只有在 当然为0就没有事但是如果不是0的话就要注意了表示你修改的和别人修改了相同的文件先点开 次文件然后看看那些不一样主要看如果是这样就直接点更新就好了,如果有 ...

  5. JMeter-使用Badboy录制Web测试脚本

    JMeter是纯Java编写的软件功能和性.能测试工具,其录制脚本过于笨拙和复杂.而Badboy是用C++开发的动态应用测试工具,其拥有强大的屏幕录制和回放功能,同时提供图形结果分析功能,刚好弥补了J ...

  6. HDU-1548--A strange lift--(BFS,剪枝)

    A strange lift   Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...

  7. UVALive - 4026 Difficult Melody(暴力)

    我这个英语学渣又把题给翻译错了……(话说,六级差十分没有过,好心疼T T),题目中说的P和Q都是计算game的个数,我以为是出现的次数,各种wa..后来调整了以后又是各种wa,原来是double型的数 ...

  8. 把Ubuntu用户目录下的目录名改成英文

    直接改名字是不行的,一重启就回去了 方法一: 把中文文件夹改成相应的英文文件夹,再修改配置文件 ~/.config/user-dirs.dirs XDG_DESKTOP_DIR="$HOME ...

  9. 项目中常用js方法整理common.js

    抽空把项目中常用js方法整理成了common.js,都是网上搜集而来的,大家一起分享吧. var h = {}; h.get = function (url, data, ok, error) { $ ...

  10. 一种快速查询多点DS18B20温度的方法(转)

    源:http://hi.baidu.com/james_xiao/item/79b961c90623093e45941623 一种快速查询多点DS18B20温度的方法 引言      为了满足实时性要 ...