Hdu1242(bfs+优先队列or dfs)
来源:互联网 发布:指南针mc指标源码 编辑:程序博客网 时间:2024/05/18 18:14
题意:和最普通迷宫类问题差别在于,他的路上会有x,这个x会让他多停留一秒,而用用最基础的宽度搜索,永远搜的是地图上最短的路径,而忽略x的加成,如果那条最短的路上有很多x,加起来比远的没有x的路时间长多了。
所以,开始我就wrong了很多次,(果然太弱了),后来想啊,用广搜把每条都去的地方扫一下,选个时间最短的就好了,结果。。。。案例怎么广搜都只能扫出一条,别的自己想数据的也扫不全。。。后来想啊想。。。(太弱)我写的广搜的原理,里面有一条,走过的路标记后不能再走了。。有些不同的路有重叠的地方第二次他就不走了。。。所以广搜是无法把所有不同的路都找到。。。比较的。。。那就深搜啊~!深搜不就相当于枚举可以全排列,每条可行的路,选个时间最短的。还好这题数据小,没有TLE。
再后来,看了别人的题解,可以在广搜里面加个优先队列,每次进队列里,都默默排个序,选时间最短的那个点优先走他可能达到的下个点。
所以 这题最后 我还是用了两个不同的搜索写出来啦~
dfs
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;char M[210][210];int Min,Next[4][2]={{-1,0},{0,1},{1,0},{0,-1}},n,m;void dfs(int startx,int starty,int step){ if(M[startx][starty]=='r') { if(step<Min) Min=step; return; } else { for(int i=0;i<4;i++) { int tx=startx+Next[i][0]; int ty=starty+Next[i][1]; if(M[tx][ty]=='#'||tx<0||ty<0||tx>=n||ty>=m) continue; if(M[tx][ty]=='.') { M[tx][ty]='#'; dfs(tx,ty,step+1); M[tx][ty]='.'; } else if(M[tx][ty]=='x') { M[tx][ty]='.'; dfs(tx,ty,step+2); M[tx][ty]='x'; } else if(M[tx][ty]=='r') { if(step+1<Min) Min=step+1; return; } } return; }}int main(){ while(~scanf("%d %d",&n,&m)) { int fx,fy,num=0; Min=99999999; for(int i=0;i<n;i++) { scanf("%s",M[i]); for(int j=0;j<m;j++) { if(M[i][j]=='a') { fx=i,fy=j; } } } dfs(fx,fy,0); if(Min<99999999) printf("%d\n",Min); else printf("Poor ANGEL has to stay in the prison all his life.\n"); } return 0;}
bfs +优先队列
#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;char M[210][210];int n,m,book[210][210],Next[4][2]={{-1,0},{0,1},{1,0},{0,-1}},Min;struct point{ int x,y; int step; friend bool operator<(point n1,point n2) { return n2.step<n1.step; }};priority_queue<struct point>Q;void bfs(int X,int Y){ while(!Q.empty()) Q.pop(); memset(book,0,sizeof(book)); struct point first; first.x=X; first.y=Y; first.step=0; Q.push(first); struct point now,next; while(!Q.empty()) { now=Q.top(); Q.pop(); next=now; for(int i=0;i<4;i++) { int tx=now.x+Next[i][0]; int ty=now.y+Next[i][1]; if(M[tx][ty]=='#'||tx<0||ty<0||tx>=n||ty>=m) continue; if(M[tx][ty]=='.') { M[tx][ty]='#'; next.x=tx; next.y=ty; next.step=now.step+1; Q.push(next); } else if(M[tx][ty]=='x') { next.x=tx; next.y=ty; M[tx][ty]='.'; next.step=now.step+2; Q.push(next); } else if(M[tx][ty]=='r') { M[tx][ty]='.'; if(next.step+1<Min) Min=next.step+1; Q.push(next); } } }}int main(){ while(~scanf("%d %d",&n,&m)) { int fx,fy; Min=99999999; for(int i=0;i<n;i++) { scanf("%s",M[i]); for(int j=0;j<m;j++) { if(M[i][j]=='a') { fx=i,fy=j; } } } bfs(fx,fy); if(Min<99999999) printf("%d\n",Min); else printf("Poor ANGEL has to stay in the prison all his life.\n"); } return 0;}
两个时间差别还是挺大的,前者140ms ,后者15ms,感谢出题人数据给的小orz。
总结:广度搜索 只能搜出 最短的那条,而深搜是枚举所有可行。
0 0
- Hdu1242(bfs+优先队列or dfs)
- hdu1242 Rescue(BFS +优先队列 or BFS )
- hdu1242 Rescue(BFS +优先队列 or BFS )
- hdu1242(BFS+优先队列)
- hdu1242(bfs+优先队列)
- hdu1242优先队列BFS
- HDU1242 BFS+优先队列
- hdu1242 BFS+优先队列
- hdu1242优先队列+BFS
- hdu1242(bfs+优先队列)
- HDU1242 Rescue(优先队列加BFS)
- hdu1242 Rescue(优先队列bfs)
- Rescue HDU1242 (BFS+优先队列)
- HDU1242或ZOJ1649(BFS+优先队列)
- HDU1242:Rescue(BFS+优先队列)
- hdu1242 Rescue 优先队列BFS
- HDU1242 Rescue-BFS,优先队列
- HDU1242:Rescue(BFS+优先队列)
- 捷登为理芙宝品牌设计的视觉识别系统
- 数据库的最简单实现
- PAT 数据结构 01-复杂度2. Maximum Subsequence Sum (25)
- nginx----list
- Android SwipeRefreshLayout实现工作原理.
- Hdu1242(bfs+优先队列or dfs)
- KMP 字符串匹配
- 每天一个linux命令(70):man命令
- 代码进行自动约束
- 归并排序
- Android启动过程深入解析
- c程序代码:输出九九乘法表
- PowerDesigner如何将物理数据模型图生成图片?
- 细小知识点之Java