UVA 11624 Fire! BFS搜索
题意:就是问你能不能在火烧到你之前,走出一个矩形区域,如果有,求出最短的时间
分析:两遍BFS,然后比较边界
- #include<cstdio>
- #include<algorithm>
- #include<iostream>
- #include<cstring>
- #include<cmath>
- #include<map>
- #include<queue>
- #include<stdlib.h>
- #include<string>
- #include<set>
- using namespace std;
- typedef long long LL;
- const int maxn=;
- const int INF=0x3f3f3f3f;
- char s[maxn][maxn];
- int mp[maxn][maxn];
- int v[maxn][maxn];
- int n,m;
- struct Point
- {
- int x,y;
- Point() {}
- Point(int a,int b)
- {
- x=a,y=b;
- }
- } o,t;
- queue<Point>q,e;
- int dx[]= {,,-,};
- int dy[]= {-,,,};
- int main()
- {
- int T;
- scanf("%d",&T);
- while(T--)
- {
- scanf("%d%d",&n,&m);
- for(int i=; i<=n; ++i)
- scanf("%s",s[i]+);
- for(int i=; i<=n; ++i)
- {
- for(int j=; j<=m; ++j)
- {
- mp[i][j]=v[i][j]=-;
- if(s[i][j]=='F')q.push(Point(i,j)),mp[i][j]=;
- else if(s[i][j]=='J')e.push(Point(i,j)),v[i][j]=;
- }
- }
- while(!q.empty())
- {
- o=q.front();
- q.pop();
- for(int i=; i<; ++i)
- {
- t.x=o.x+dx[i];
- t.y=o.y+dy[i];
- if(t.x<||t.x>n||t.y<||t.y>m)continue;
- if(s[t.x][t.y]=='#'||mp[t.x][t.y]!=-)continue;
- mp[t.x][t.y]=mp[o.x][o.y]+;
- q.push(t);
- }
- }
- while(!e.empty())
- {
- o=e.front();
- e.pop();
- for(int i=; i<; ++i)
- {
- t.x=o.x+dx[i];
- t.y=o.y+dy[i];
- if(t.x<||t.x>n||t.y<||t.y>m)continue;
- if(s[t.x][t.y]=='#'||v[t.x][t.y]!=-)continue;
- v[t.x][t.y]=v[o.x][o.y]+;
- e.push(t);
- }
- }
- int ans=INF;
- for(int i=;i<=n;++i)
- {
- if(v[i][]!=-)
- {
- if(mp[i][]==-||v[i][]<mp[i][])
- ans=min(ans,v[i][]);
- }
- if(v[i][m]!=-)
- {
- if(mp[i][m]==-||v[i][m]<mp[i][m])
- ans=min(ans,v[i][m]);
- }
- }
- for(int i=;i<=m;++i)
- {
- if(v[][i]!=-)
- {
- if(mp[][i]==-||v[][i]<mp[][i])
- ans=min(ans,v[][i]);
- }
- if(v[n][i]!=-)
- {
- if(mp[n][i]==-||v[n][i]<mp[n][i])
- ans=min(ans,v[n][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裸体,关键是先把所有的着火点放入队列,分开一个一个做bfs会超时的 发现vis[][]是多余的,完全可以用 ...
- UVA 11624 Fire! (bfs)
算法指南白书 分别求一次人和火到达各个点的最短时间 #include<cstdio> #include<cstring> #include<queue> #incl ...
- UVA 11624 Fire! bfs 难度:0
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- BFS(两点搜索) UVA 11624 Fire!
题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...
- UVa 11624 Fire!(着火了!)
UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...
- UVA 11624 Fire!【两点BFS】
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...
- E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)
E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ...
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
随机推荐
- python学习笔记9(对文件的操作)
一.文件对象 我理解的文件对象就是一个接口,通过这个接口对文件进行相关操作. 二.相关函数 [1].内建函数:open() 提供了初始化输入/输出(I/O)操作的通用接口,成功打开一个文件后会返回一个 ...
- 集成“支付宝” -b
大致步骤 1.与支付宝签约获取相关参数 合作者身份 ID 与安全校验码 key2.下载需要导入的文件,做相应设置3.在自己的项目中集成支付的方法代码 详细步骤 1.获取合作者身份 ID 与安全校验码 ...
- org.hibernate.LazyInitializationException
1.org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.c ...
- asp.net单点登录(SSO)解决方案
前些天一位朋友要我帮忙做一单点登录,其实这个概念早已耳熟能详,但实际应用很少,难得最近轻闲,于是决定通过本文来详细描述一个SSO解决方案,希望对大家有所帮助.SSO的解决方案很多,但搜索结果令人大失所 ...
- chardet安装
1.下载 chardet-2.2.1.tar.gz (md5) https://pypi.python.org/pypi/chardet#downloads 2.解压至C:\Python27\Li ...
- 跨平台Unicode与UTF8互转代码
参考来源:http://blog.csdn.net/flying8127/article/details/1598521 在原来原基础上,将代码整理,并加强安全性. 并按照WindowsAPI设计, ...
- 【BZOJ 3190】 3190: [JLOI2013]赛车 (半平面交)
3190: [JLOI2013]赛车 Description 这里有一辆赛车比赛正在进行,赛场上一共有N辆车,分别称为个g1,g2--gn.赛道是一条无限长的直线.最初,gi位于距离起跑线前进ki的位 ...
- TCP长连接与短连接的区别
http://www.cnblogs.com/liuyong/archive/2011/07/01/2095487.html 1. TCP连接 当网络通信时采用TCP协议时,在真正的读写操作之前,se ...
- spring aop环绕通知
[Spring实战]—— 9 AOP环绕通知 假如有这么一个场景,需要统计某个方法执行的时间,如何做呢? 典型的会想到在方法执行前记录时间,方法执行后再次记录,得出运行的时间. 如果采用Sprin ...
- 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过
杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...