J - Fire!
#include<queue>
#include<stdio.h>
#include<string.h>
using namespace std; #define maxn 1005
const int oo = 0xfffffff; struct node{int x, y;};
node JoeSite, FireSite[maxn*maxn];//因为火可能不止一处,所以开一个数组保存所有的火焰的位置
char G[maxn][maxn];
int vJoe[maxn][maxn], vFire[maxn][maxn];
int dir[][] = { {,},{,-},{,},{-,} };
int M, N, nFire; void BfsFire()
{
node s, q;
queue<node> Q;
int i; for(i=; i<nFire; i++)
{
Q.push(FireSite[i]);
vFire[ FireSite[i].x ][ FireSite[i].y ] = ;
} while(Q.size())
{
s = Q.front();Q.pop(); for(i=; i<; i++)
{
q = s;
q.x += dir[i][];
q.y += dir[i][]; if(q.x>=&&q.x<M && q.y>=&&q.y<N && G[q.x][q.y]!='#' && vFire[q.x][q.y]==oo)
{
vFire[q.x][q.y] = vFire[s.x][s.y] + ;
Q.push(q);
}
}
}
} int IsBorder(int x, int y)//判断是否是边界并且是否比火焰先到达
{
if( (x== || x==M- || y== || y==N-) && vJoe[x][y] < vFire[x][y] )
return ;
return ;
} int BfsJoe()
{
queue<node> Q;
node s, q;
int i; vJoe[JoeSite.x][JoeSite.y] = ;
Q.push(JoeSite); while(Q.size())
{
s = Q.front();Q.pop(); if(IsBorder(s.x, s.y) == )
return vJoe[s.x][s.y]; for(i=; i<; i++)
{
q = s;
q.x += dir[i][];
q.y += dir[i][]; if(q.x>=&&q.x<M && q.y>=&&q.y<N && G[q.x][q.y]!='#' && vJoe[q.x][q.y]==)
{
vJoe[q.x][q.y] = vJoe[s.x][s.y] + ;
Q.push(q);
}
}
} return -;
} int main()
{
int T; scanf("%d", &T); while(T--)
{
int i, j, ans; scanf("%d%d", &M, &N); nFire = ; for(i=; i<M; i++)
{
scanf("%s", G[i]);
for(j=; j<N; j++)
{
vJoe[i][j] = ;
vFire[i][j] = oo; if(G[i][j] == 'J')
JoeSite.x = i, JoeSite.y = j;
if(G[i][j] == 'F')
{
FireSite[nFire].x = i;
FireSite[nFire++].y = j;
}
}
} BfsFire();
ans = BfsJoe(); if(ans == -)
printf("IMPOSSIBLE\n");
else
printf("%d\n", ans);
} return ;
}
J - Fire!的更多相关文章
- UVA - 11624 J - Fire! (BFS)
题目传送门 J - Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ...
- J - Fire!---UVA 11624
题目链接 题意:J代表Joe的位置,F代表火的起点,下一刻火将会向四周扩散,求Joe逃离的最短时间,如果不能逃离输出IMPOSSIBLE; 注意火的起点可能不止一处 可以用两次bfs分别求出人到达某个 ...
- Fire逃生
Description: You are trapped in a building consisting of open spaces and walls. Some places are on f ...
- Fire! -两次dfs
题目描述: Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of ...
- Fire! 又是图 bfs
Joe works in a maze. Unfortunately, portions of the maze havecaught on re, and the owner of the ma ...
- UVA - 11624 Fire! 【BFS】
题意 有一个人 有一些火 人 在每一秒 可以向 上下左右的空地走 火每秒 也会向 上下左右的空地 蔓延 求 人能不能跑出来 如果能 求最小时间 思路 有一个 坑点 火是 可能有 多处 的 样例中 只有 ...
- 【UVA - 11624】Fire!
-->Fire! 直接上中文 Descriptions: 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块 ...
- kuangbin专题 专题一 简单搜索 Fire! UVA - 11624
题目链接:https://vjudge.net/problem/UVA-11624 题意:一个迷宫,可能有一个或者多个地方着火了,每过1个时间消耗,火会向四周蔓延,问Joe能不能逃出迷宫,只要走出迷宫 ...
- UVA11624Fire!(BFS)
题目链接 题意:帮助joe走出一个大火蔓延的迷宫,其中joe每分钟可往上下左右四个方向之一走,所有着火的格子都会蔓延(空格与着火格有公共边,下一分钟这个空格也会着火).迷宫中有一些障碍格,joe和火都 ...
随机推荐
- 那些年,我们一起学WCF--(7)PerSession实例行为
这一节,大家了解下PerSession实例行为,PerSession表示会话实例行为,当客户端调用服务器后,服务器端会为客户端分配一个新的服务实例,这个实例在服务器端SESSION时间过期后将失效.客 ...
- Dictionary 总结
foreach (KeyValuePair<int, string> kvp in myDictionary) {...} Dictionary<string, string> ...
- 浅析angular框架的cookie
相信接触过网页编程的基本上都知道cookie这个东西吧,一个毫不起眼,但是又十分的重要的东西,今天我们就来分析一下这个小东西,我们都知道客服端通过发送http请求到服务器请求我们的数据,当我们的服务器 ...
- 算法的优化(C语言描述)
算法的优化 算法的优化分为全局优化和局部优化两个层次.全局优化也称为结构优化,主要是从基本控制结构优化.算法.数据结构的选择上考虑:局部优化即为代码优化,包括使用尽量小的数据类型.优化表达式.优化赋值 ...
- 《chkconfig命令》-linux命令五分钟系列之四
本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc 希望您能通过捐款的方式支持Linux大棚博客的运行和发展.请见“关于捐款” == ...
- ICE学习第四步-----客户端请求服务器返回数据
这次我们来做一个例子,流程很简单:客户端向服务器发送一条指令,服务端接收到这条指令之后,向客户端发送数据库中查询到的数据,最终显示在DataGridView上. 根据上一篇文章介绍的Slice语法,我 ...
- 关于css中透明度继承的问题
今天工作中发现了一个问题,透明度的继承问题,如下图所示: 容器div2就“继承了”外面容器div1的透明度,也变成了70%的透明.容器里面的字体颜色和图片都“继承”了div1,具体代码如下: 可是设计 ...
- Bootstrap_Javascript_弹出框
HTML: <button type="button" class="btn btn-default" data-container="body ...
- DEDECMS栏目自定义字段添加
用到的文件: catalog_add.htm 路径:\dede\templets\ catalog_edit.htm 路径:\dede\templets\ catalog_add.php 路径 ...
- MySQL zip版安装配置
文章出处:http://www.cnblogs.com/winstic/,请保留此连接 这段时间在学习Python 数据库操作知识,简单整理MySQL zip文件安装方法 下载 在MySQL官网htt ...