UVA 11624 Fire! (bfs)
算法指南白书
分别求一次人和火到达各个点的最短时间
- #include<cstdio>
- #include<cstring>
- #include<queue>
- #include<algorithm>
- using namespace std;
- const int INF = ;
- const int maxr = + ;
- const int maxc = + ;
- int R, C;
- char maze[maxr][maxc];
- struct Cell {
- int r, c;
- Cell(int r, int c):r(r),c(c) {}
- };
- const int dr[] = {-,,,};
- const int dc[] = {,,-,};
- int d[maxr][maxc][], vis[maxr][maxc][];//0表示人,1表示火走
- queue<Cell> Q;
- void bfs(int kind) {
- while(!Q.empty()) {
- Cell cell = Q.front();
- Q.pop();
- int r = cell.r, c = cell.c;
- for(int dir = ; dir < ; dir++) {
- int nr = r + dr[dir], nc = c + dc[dir];
- if(nr >= && nr < R && nc >= && nc < C && maze[nr][nc] == '.' && !vis[nr][nc][kind]) {
- Q.push(Cell(nr, nc));
- vis[nr][nc][kind] = ;
- d[nr][nc][kind] = d[r][c][kind] + ;
- }
- }
- }
- }
- int ans;
- void check(int r, int c) {
- if(maze[r][c] != '.' || !vis[r][c][]) return; // 必须是Joe可达的边界格子
- if(!vis[r][c][] || d[r][c][] < d[r][c][]) ans = min(ans, d[r][c][] + ); // Joe必须先于火到达
- }
- int main() {
- int T;
- scanf("%d", &T);
- while(T--) {
- scanf("%d%d", &R, &C);
- int jr, jc;
- vector<Cell> fires;
- for(int i = ; i < R; i++) {
- scanf("%s", maze[i]);
- for(int j = ; j < C; j++)
- if(maze[i][j] == 'J') {
- jr = i;
- jc = j;
- maze[i][j] = '.';
- } else if(maze[i][j] == 'F') {
- fires.push_back(Cell(i,j));
- maze[i][j] = '.';
- }
- }
- memset(vis, , sizeof(vis));
- // Joe
- vis[jr][jc][] = ;
- d[jr][jc][] = ;
- Q.push(Cell(jr, jc));
- bfs();
- // Fire
- for(int i = ; i < fires.size(); i++) {
- vis[fires[i].r][fires[i].c][] = ;
- d[fires[i].r][fires[i].c][] = ;
- Q.push(fires[i]);
- }
- bfs();
- // 计算答案
- ans = INF;
- for(int i = ; i < R; i++) {
- check(i,);
- check(i,C-);
- }
- for(int i = ; i < C; i++) {
- check(,i);
- check(R-,i);
- }
- if(ans == INF) printf("IMPOSSIBLE\n");
- else printf("%d\n", ans);
- }
- return ;
- }
UVA 11624 Fire! (bfs)的更多相关文章
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- UVA 11624 Fire! bfs 难度:0
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA 11624 Fire! BFS搜索
题意:就是问你能不能在火烧到你之前,走出一个矩形区域,如果有,求出最短的时间 分析:两遍BFS,然后比较边界 #include<cstdio> #include<algorithm& ...
- BFS(两点搜索) UVA 11624 Fire!
题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...
- UVa 11624 Fire!(着火了!)
UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...
- E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)
E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ...
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- UVa 11624 Fire!(BFS)
Fire! Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %lld & %llu Description Joe ...
- (简单) UVA 11624 Fire! ,BFS。
Description Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ow ...
随机推荐
- csqlite编译相关配置问题
csqlite是非常好用的数据库,同时该数据库是开源的,基于一定原因可能需要编译自己需要的csqlite版本,那么下面介绍内容也会你就会感兴趣了. 这里要实现的目标是使用VS工具能够正确编译csqli ...
- find命令使用, -exec xargs
find [path] [expression] 例如:find /home -name \*.o -exec rm '{}' \; find: 实时精确,支持众多查找标准,遍历指定目录中 ...
- 网络ip
国际规定:把所有的IP地址划分为 A,B,C,D,E A类地址:范围从0-127,0是保留的并且表示所有IP地址,而127也是保留的地址,并且是用于测试环回用的.因此 A类地址的范围其实是从1-126 ...
- H5 required 改变错误提示oninvalid、oninput、onforminput
<input type="text" name="password" oninvalid="this.setCustomValidity('XX ...
- CentOS使用ufw的方法
ufwはファイアウォールの管理ツールで.Ubuntuで標準的に使われています.ufw allow 80/tcp のような簡単なコマンドでポートを開け閉めできます. CentOS用のパッケージは用意され ...
- TDirectory.GetCreationTime、TDirectory.SetCreationTime获取和设置文件夹创建时间
使用函数: System.IOUtils.TDirectory.GetCreationTime//获取创建时间 System.IOUtils.TDirectory.SetCreationTime//设 ...
- Soy文件生成JS文件 - 一个使用Google soy模板的例子
1.下载工具包,后解压. http://closure-templates.googlecode.com/files/closure-templates-for-javascript-latest.z ...
- logger.debug,logger.info,logger.warn,logger.error,logger.fatal的区别
logger.debug,logger.info,logger.warn,logger.error,logger.fatal的区别 logger.debug,logger.info,logger.wa ...
- Android 获取手机联系人信息
//获取联系人 Uri rawContacts = Uri.parse("content://com.android.contacts/raw_contacts"); Conten ...
- 【POJ3169 】Layout (认真的做差分约束)
Layout Description Like everyone else, cows like to stand close to their friends when queuing for ...