UVA 11624 Fire!
题目大意:
F代表火焰
J代表人
一个人想在火焰烧到自己之前逃出着火地区
. 为路,人可以走,火可以燃烧(当然如果火先烧到就不能走了)
#为墙,不可走
如果能逃出,输出时间,不能,输出IMPOSSIBLE
每次移动上下左右(人火都是, 花费1)
解题思路:
简单广搜两次就行,先对火广搜,保存下步数,在对人广搜,看看走到此点花费的时间是不是比火小,小的话可以走,不然不能走,走到边界为逃出条件
具体实现用一个二维数组F
先对火焰进行广搜,用来保存每个点火燃烧到时花费的步数,初始值为0;
第二次广搜人走到此点需要的步数,如果小于此时此点保存的则证明可以走,走过之后赋值为-1;
火燃烧出发点赋值为-1;
代码:
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack> using namespace std;
#define INF 0xfffffff
#define N 1010
int m, n;
char maps[N][N];
int dir[][] = {,, ,-, ,, -,};
int F[N][N];
/*F, 先对火焰进行广搜,用来保存每个点火燃烧到时花费的步数,初始值为0;
第二次广搜人走到此点需要的步数,如果小于此时此点保存的则证明可以走,走过之后赋值为-1;
火燃烧出发点赋值为-1,
*/ struct node
{
int x, y;
int step;
}s, e, a[N], q1; void Init()//初始化
{
memset(maps, , sizeof(maps));
memset(F, , sizeof(F));
memset(a, , sizeof(a));
} void BFS(int c)
{
queue<node>q; for(int i = ; i < c; i++) {
q.push(a[i]);
F[a[i].x][a[i].y] = -;
} while(q.size()) {//对火焰的广搜
q1 = q.front();
q.pop(); for(int i = ; i < ; i++) {
e.x = q1.x + dir[i][];
e.y = q1.y + dir[i][];
e.step = q1.step + ;
if(e.x >= && e.y >= && e.x < m && e.y < n && F[e.x][e.y] == && maps[e.x][e.y] == '.') {
F[e.x][e.y] = e.step;
q.push(e);
}
}
} q.push(s);
F[s.x][s.y] = -; while(q.size()) {//对人的广搜
q1 = q.front();
q.pop();
if(q1.x == || q1.y == || q1.x == m - || q1.y == n - ) {//达到出去条件, 输出到达此地步数+1
printf("%d\n", q1.step + );
return;
} for(int i = ; i < ; i++) {
e.x = q1.x + dir[i][];
e.y = q1.y + dir[i][];
e.step = q1.step + ;
if(c != ){
if(e.x >= && e.y >= && e.x < m && e.y < n && F[e.x][e.y] != - && e.step < F[e.x][e.y] && maps[e.x][e.y] == '.') {
F[e.x][e.y] = -;
q.push(e);
}
}
else if(e.x >= && e.y >= && e.x < m && e.y < n && F[e.x][e.y] != - && maps[e.x][e.y] == '.') {
F[e.x][e.y] = -;
q.push(e);
}
} } printf("IMPOSSIBLE\n");//未满足条件 } int main()
{
int T;
scanf("%d", &T);//T个样例 while(T--) {
Init();//初始化
int c = ;
scanf("%d %d", &m, &n);
for(int i = ; i < m; i++) {
scanf(" ");
for(int j = ; j < n; j++) {
scanf("%c", &maps[i][j]);
if(maps[i][j] == 'F') {//读入数据同时进行初始化 保存人和火焰的下标
a[c].x = i;
a[c].y = j;
a[c++].step = ;
}
if(maps[i][j] == 'J') {
s.x = i;
s.y = j;
s.step = ;
}
}
}
BFS(c);//开广搜 }
}
/*
2
4 4
####
#J.#
#..#
#..#
3 3
###
#J.
#.F
*/
UVA 11624 Fire!的更多相关文章
- UVa 11624 Fire!(着火了!)
UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...
- BFS(两点搜索) UVA 11624 Fire!
题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- 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[][]是多余的,完全可以用 ...
- UVA 11624 Fire! (bfs)
算法指南白书 分别求一次人和火到达各个点的最短时间 #include<cstdio> #include<cstring> #include<queue> #incl ...
- (简单) UVA 11624 Fire! ,BFS。
Description Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ow ...
- UVA 11624 Fire! bfs 难度:0
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
随机推荐
- Oracle数据库的表结构 简单表的建立
- SQL 对时间的处理
--获取当前日期(如:yyyy-mm-dd)Select Datename(year,GetDate())+'-'+Datename(month,GetDate())+'-'+Datename(day ...
- 体验阿里云SSD云盘+ECS(I/O优化)的性能
阿里云推出SSD云盘+I/O优化的ECS已有一段时间,这个功能优化可以为ECS服务器与SSD云盘提供更好的网络能力.据阿里云官网介绍,SSD云盘最高提供20000次随机读写IOPS.256MB/S吞吐 ...
- 讯时网关IP对接PBX
先配置呼入 1.在网关的中继线绑定号码 2.在路由表写入到PBX 路由到 pbx IP 绑定的号码和路由的 fxo后面的数字要一致 3.在PBX 建一个sip中继,host为网关IP 4.创 ...
- GET方法和POST方法
package com.hanqi.cunchu; import android.app.ProgressDialog; import android.support.v7.app.AppCompat ...
- GitHub注册账号
username : hcloudypassword : hujunyun3174email : 370284221@qq.com
- 备份MySQL数据库
备份MySQL数据库脚本: #!/bin/bash # description: MySQL buckup shell script # author: lmj # web site: http:// ...
- 相机位姿估计0:基本原理之如何解PNP问题
关键词:相机位姿估计 PNP问题求解 用途:各种位姿估计 文章类型:原理 @Author:VShawn(singlex@foxmail.com) @Date:2016-11-18 @Lab: CvLa ...
- Javascript中的集合
集合是由一组无序且唯一(即不能重复)的项组成 function Set() { var items={}; this.has=function(value){ //return value in it ...
- FlexSlider jQuery滑动切换插件 参数
demo:http://www.sucaihuo.com/jquery/0/6/demo/ FlexSlider是一个非常出色的jQuery滑动切换插件,它支持所有主流浏览器,并有淡入淡出效果.适合所 ...