UVa 11624 (BFS) Fire!
也是一个走迷宫的问题,不过又有了点变化。
这里迷宫里有若干把火,而且火每秒也是向四个方向蔓延的。问人是否能走出迷宫。
我用了两遍BFS,第一遍把所有着火的格子加入队列,然后计算每个格子着火的时间。
第二遍便是走迷宫,只有当这个格子不是墙,而且当前时间在这个格子着火之前才能拓展。当然,并不是所有的空格都一定会着火。
- #include <cstdio>
- #include <cstring>
- #include <queue>
- using namespace std;
- struct Node
- {
- int x, y, t;
- Node(int x=, int y=, int t=):x(x), y(y), t(t) {}
- };
- Node st;
- const int maxn = + ;
- int row, col;
- char maze[maxn][maxn];
- int time[maxn][maxn];
- bool vis[maxn][maxn];
- int dx[] = { , , , - };
- int dy[] = { , , -, };
- inline bool in(int x, int y)
- { return x >= && x < row && y >= && y < col; }
- inline bool border(int x, int y)
- { return x == || x == row- || y == || y == col-; }
- void init()
- {
- memset(time, -, sizeof(time));
- queue<Node> Q;
- for(int i = ; i < row; i++)
- for(int j = ; j < col; j++)
- {
- if(maze[i][j] == 'F') { Q.push(Node(i, j, )); time[i][j] = ; }
- if(maze[i][j] == 'J') { st.x = i; st.y = j; st.t = ; }
- }
- while(!Q.empty())
- {
- Node now = Q.front(); Q.pop();
- for(int i = ; i < ; i++)
- {
- int x = now.x + dx[i];
- int y = now.y + dy[i];
- int t = now.t + ;
- if(in(x, y) && time[x][y] < && maze[x][y] != '#')
- {
- Q.push(Node(x, y, now.t + ));
- time[x][y] = t;
- }
- }
- }
- }
- int BFS()
- {
- memset(vis, false, sizeof(vis));
- queue<Node> Q;
- Q.push(st);
- vis[st.x][st.y] = true;
- while(!Q.empty())
- {
- Node now = Q.front(); Q.pop();
- if(border(now.x, now.y)) return now.t + ;
- for(int i = ; i < ; i++)
- {
- int x = now.x + dx[i];
- int y = now.y + dy[i];
- int t = now.t + ;
- if(in(x, y) && !vis[x][y] && maze[x][y] != '#')
- {
- if(time[x][y] >= && time[x][y] <= t) continue;
- vis[x][y] = true;
- Q.push(Node(x, y, t));
- }
- }
- }
- return ;
- }
- int main()
- {
- //freopen("in.txt", "r", stdin);
- int T; scanf("%d", &T);
- while(T--)
- {
- scanf("%d%d", &row, &col);
- for(int i = ; i < row; i++) scanf("%s", maze[i]);
- init();
- int ans = BFS();
- if(ans) printf("%d\n", ans);
- else puts("IMPOSSIBLE");
- }
- return ;
- }
代码君
UVa 11624 (BFS) Fire!的更多相关文章
- UVA - 11624 J - Fire! (BFS)
题目传送门 J - Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ...
- uva 11624(bfs)
11624 - Fire! Time limit: 1.000 seconds Joe works in a maze. Unfortunately, portions of the maze hav ...
- UVA 11624 BFS的妙用
题意: 迷宫里起火了,有若干个障碍物,有多个起火点,起火点每经过一个时间间隔就向它的上下左右相邻的格子扩散. 有个倒霉的人好像叫做“Joe”,他要逃出来,他每次可以向上下左右任意移动一格,但是即要避开 ...
- 【UVA - 11624】Fire!
-->Fire! 直接上中文 Descriptions: 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块 ...
- (UVA 11624)Fire!
题目链接 http://vjudge.net/contest/121377#problem/J Joe works in a maze. Unfortunately, portions of the ...
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- BFS(两点搜索) UVA 11624 Fire!
题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...
- E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)
E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ...
- UVA 11624 Fire! (bfs)
算法指南白书 分别求一次人和火到达各个点的最短时间 #include<cstdio> #include<cstring> #include<queue> #incl ...
随机推荐
- sao/jsp
sao/i18n/message/ Messages-Client.xml Messages-Server.xml sao/wsdl Verification.wsdl IProcessS ...
- 在字符串S1中删除字符串S2中所包含的字符
/************************************************************************* > File Name: test.c &g ...
- Using the viewport meta tag to control layout on mobile browsers
A typical mobile-optimized site contains something like the following: <meta name="viewport& ...
- amcharts简单封装
只是简单是封装了一下,目前只能输出线图(折现,圆滑线等),柱状图. 代码如下: ;!function(win,$,AC,undefined){ var DDcharts = function(o){ ...
- 浅谈Spark Kryo serialization
原创文章,转载请注明: 转载自http://www.cnblogs.com/tovin/p/3833985.html 最近在使用spark开发过程中发现当数据量很大时,如果cache数据将消耗很多的内 ...
- centOS学习part1:操作系统安装
0 linux作为服务器的主要操作系统,在处理速度以及安全性上都要优于windows,虽然需要很多命令要记,但是一般常用的命令不多,用多了就熟悉了,而且现在很多都要图形界面,也降低了学习成本. cen ...
- 李洪强经典iOS面试题11
#import 跟#include 又什么区别,@class呢, #import<> 跟 #import””又什么区别? #import是Objective-C导入头文件的关键字, ...
- Android核心分析之十八Android电话系统之RIL-Java
Android RIL-Java 123.jpg (2.09 KB, 下载次数: 1) 下载附件 保存到相册 2012-3-21 10:47 上传 RIL-Java在本质上就是一个RIL代理,起 ...
- mysql+heartbeat+DRBD+LVS集群
- ios开发--企业帐号发布
这两天需要发布一个ipa放到网上供其他人安装,需要用到企业级开发者账号. 首先详细说明一下我们的目标,我们需要发布一个ipa放到网上,所有人(包括越狱及非越狱设备)可以直接通过链接下载安装,不需要通过 ...