HOJ 2979 Escape from Pyramids --------BFS求最小的步数
来源:互联网 发布:md204lv4编程软件 编辑:程序博客网 时间:2024/06/08 09:06
HOJ 2979 Escape from Pyramids
//题意:按照题中给的金字塔的位置 ,s代表起点 ,*代表障碍,@代表此路可走// D代表出口(注意出口不止一个),在规定的时间内判断是否能走出金字塔//思路:BFS求最短的路径上面所走的步数,注意搜索失败的时候队列是空的,判断一下子//调试注意:// 1.注意步数是1这种特殊情况(对应题目中所给样例的case3) 输出的是1 minute 而不是1 minutes// 2.注意是两个案例之间有空格,最后一个案例后面没有空格// 3.printf()在很多输出时候就显示出优势来啦,不改啦,下次a题的时候用printf(),cout太恶心啦!#include<iostream>#include<cstring>#include<queue>#define maxlen 110using namespace std;struct node{ int x,y,step;};int mat[maxlen][maxlen];int dir[6][2]= {{0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,-1}};int BFS(node s,node e[],int count,int m){ int i; queue<node> q; node ol,ne;//旧的状态和新的状态 while(!q.empty()) { q.pop(); }//建立空队列 s.step=0; q.push(s);//搜索起点入队 mat[s.x][s.y]=1; while(!q.empty()) { ol=q.front(); q.pop(); for(i=0; i<count; i++) { if(ol.x==e[i].x&&ol.y==e[i].y) { return ol.step; } }//搜索到出口,直接返回,因为是BFS,故最先搜索到的尾最小 for(i=0; i<6; i++) { ne.x=ol.x+dir[i][0]; ne.y=ol.y+dir[i][1]; ne.step=ol.step; if(!(ne.x>=0&&ne.y>=0&&ne.x<m&&ne.y<m&&ne.y<=ne.x&&!mat[ne.x][ne.y])) continue; else { mat[ne.x][ne.y]=1; ne.step++; q.push(ne); } } } if(q.empty())return -1;//搜索失败 else return ne.step;}int main(){ node s,e[maxlen];//出口有可能不止一个,设数组 int m,n,N=0,tem=0,i,j,count,minl; char k; while(cin >>m >>n) { if(tem==1) cout << endl; tem=1;//两个样例之间有一空格,最后一个样例后面没有 count=0; memset(mat,0,sizeof(mat)); N++;//样例的个数 for (i=0; i<m; i++) { for (j=0; j<=i; j++) { cin>> k; if(k=='S') { s.x=i; s.y=j; } else if(k=='*') mat[i][j]=1;//不能走 else if(k=='D') { e[count].x=i; e[count].y=j; count++;//出口的个数 } else mat[i][j]=0;//可以走 } } minl=BFS(s,e,count,m); if(minl>1&&minl<=n) cout << "Maze #" << N << " :" << endl << "Hurry up, You need " << minl << " minutes to escape!" << endl ; else if(minl==1&&minl<=n) cout <<"Maze #" << N << " :" << endl << "Hurry up, You need 1 minute to escape!" << endl; else cout << "Maze #" << N << " :" << endl <<"Oh No, I'm afraid that you don't have enough time to escape!" << endl ; //注意1的情况,恶心啦一天啊 } return 0;}
下面发一下林2B的代码,确实写得不错:
#include <iostream>#include <stdio.h>#include <cstring>#include <queue>using namespace std;bool vis[101][101];char map[101][101];int dx[]= {-1,-1,0,0,1,1};int dy[]= {-1,0,-1,1,0,1};int T,n;struct point{ int x,y,t;};int main(){ char c; int cases=1; int tmp=0; while(scanf("%d%d",&n,&T)==2) { if(tmp==1) printf("\n"); tmp=1; memset(vis,false,sizeof(vis)); point start; queue<point> v; for(int i=1; i<=n; i++) { for(int j=1; j<=i; j++) { scanf(" %c",&c); map[i][j]=c; if(c=='S') { start.x=i; start.y=j; start.t=0; vis[i][j]=true; } } } v.push(start); int ans=10000000; while(!v.empty()) { point cur,pre; pre=v.front(); if(map[pre.x][pre.y]=='D') { ans=pre.t; break; } v.pop(); for(int i=0; i<6; i++) { cur.x=pre.x+dx[i]; cur.y=pre.y+dy[i]; if(cur.x>0&&cur.x<=n&&cur.y>0&&cur.y<=cur.x&&!vis[cur.x][cur.y]&&map[cur.x][cur.y]!='*') { cur.t=pre.t+1; vis[cur.x][cur.y]=true; v.push(cur); } } } printf("Maze #%d :\n",cases++); if(ans>T) printf("Oh No, I'm afraid that you don't have enough time to escape!\n"); else if(ans==1) printf("Hurry up, You need 1 minute to escape!\n"); else printf("Hurry up, You need %d minutes to escape!\n",ans); } return 0;}
- HOJ 2979 Escape from Pyramids --------BFS求最小的步数
- Hoj 2979 Escape from Pyramids
- HOJ 1440 Knight Moves -------简单搜索 BFS 求l两点之间最小的到达步数
- NYoj 58 最小的步数 (BFS
- POJ-3126 Prime Path(BFS 求最小步数)
- [BFS]HOJ2979Escape from Pyramids
- 走出迷宫的最小步数 (bfs)
- 最小步数 n 58 bfs
- BFS——最小步数 续
- NYOJ 58 最小步数【bfs】
- nyoj 58 最小步数(bfs)
- NYOJ 58 最小步数【bfs&dfs】
- NYOJ 58 最小步数(BFS)
- nyoj 58 最小步数(入门bfs)
- NYOJ 58 最小步数(BFS && DFS)
- nyoj 58 最小步数问题 BFS
- NYOJ 58 最小步数(bfs)
- NYOJ-最小步数(bfs)
- 杭电 1846
- c++中 exit(1)
- hdu 1385 floyd 最短路
- C语言中的整型字节序——int 到 char[4]的转换
- ECShop2.5.1_Beta upload 的目录
- HOJ 2979 Escape from Pyramids --------BFS求最小的步数
- 自制Qt IP编辑控件
- dup2 linux C函数
- 三层架构初步了解
- 德模神医
- SVN 1.6.5安装手记
- 鲁迅的《故乡》
- dup linux C
- 楼天成回忆录