第5周实践项目5 迷宫问题(栈和队列实现)
2017-10-07 11:22
344 查看
#include <iostream> #include <malloc.h> #define maxsize 100 using namespace std; #define M 8 #define N 8 int mg[M+2][N+2]= { {1,1,1,1,1,1,1,1,1,1}, {1,0,0,1,0,0,0,1,0,1}, {1,0,0,1,0,0,0,1,0,1}, {1,0,0,0,0,1,1,0,0,1}, {1,0,1,1,1,0,0,0,0,1}, {1,0,0,0,1,0,0,0,0,1}, {1,0,1,0,0,0,1,0,0,1}, {1,0,1,1,1,0,1,1,0,1}, {1,1,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1} }; typedef struct { int i; int j; int di; }box; typedef struct { box date[maxsize]; int top; }sqstack;//采用顺序栈 void initstack(sqstack *&s) { s=(sqstack *)malloc(sizeof(sqstack)); s->top=-1; } void destorystack(sqstack *&s) { free(s); } bool stackempty(sqstack *s) { return s->top==-1; } bool push(sqstack *&s,box &e) { if(s->top==maxsize-1) return false; s->top++; s->date[s->top]=e; return true; } bool pop(sqstack *&s,box &e) { if(s->top==-1) return false; e=s->date[s->top]; s->top--; return true; } bool gettop(sqstack *&s,box &e) { if(s->top==-1) return false; e=s->date[s->top]; return true; } bool mgpath(int xi,int yi,int xe,int ye) { box path[maxsize],e; int i,j,di,il,jl,k; bool find; sqstack *s; initstack(s); e.i=xi;e.j=yi;e.di=-1; push(s,e); mg[xi][yi]=-1; while(!stackempty(s)) { gettop(s,e); i=e.i;j=e.j;di=e.di; if(xe==i&&ye==j) { cout<<"一条迷宫路径如下"<<endl; k=0; while(!stackempty(s)) { pop(s,e); path[k++]=e; } while(k>=1) { k--; cout<<" ("<<path[k].i<<'.'<<path[k].j<<')'; if((k+2)%5==0) cout<<endl; } cout<<endl; destorystack(s); return true; } find=false; while(di<4&&!find) { di++; switch(di) { case 0:il=i-1;jl=j;break; case 1:il=i;jl=j+1;break; case 2:il=i+1;jl=j;break; case 3:il=i;jl=j-1;break; } if(mg[il][jl]==0) find=true; } if(find) { s->date[s->top].di=di; e.i=il;e.j=jl;e.di=-1; push(s,e); mg[il][jl]=-1; } else { pop(s,e); mg[e.i][e.j]=0; } } destorystack(s); return false; } int main() { if(!mgpath(1,1,M,N)) cout<<"改迷宫没有解"<<endl; return 0; }
队列实现bug:
#include <iostream> #include <malloc.h> #define maxsize 100 using namespace std; int mg[10][10]= { {1,1,1,1,1,1,1,1,1,1}, {1,0,0,1,0,0,0,1,0,1}, {1,0,0,1,0,0,0,1,0,1}, {1,0,0,0,0,1,1,0,0,1}, {1,0,1,1,1,0,0,0,0,1}, {1,0,0,0,1,0,0,0,0,1}, {1,0,1,0 4000 ,0,0,1,0,0,1}, {1,0,1,1,1,0,1,1,0,1}, {1,1,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1} }; typedef struct { int i,j; int pre; }box; typedef struct { box date[maxsize]; int front,rear; }qutype; void initqueue(qutype *&q) { q=(qutype *)malloc(sizeof(qutype));//q=new box[maxsize] q->front=q->rear=-1; } bool enqueue(qutype *&q,box &e) { if(q->rear==maxsize-1) return false; q->rear=q->rear+1; q->date[q->rear]=e; return true; } bool dequeue(qutype *&q,box &e) { if(q->rear==q->front) return false; q->front=q->front+1; e=q->date[q->front]; return true; } bool destoryqueue(qutype *&q) { free(q);//delete[] date; } bool emptyqueue(qutype *&q) { return q->rear==q->front; } void print(qutype *&q,int front) { /*int k,ans=0;//自己的 for(int tag=front;tag!=-1;) { k=tag; tag=q->date[tag].pre; q->date[k].pre=-1; }*/ int k=front,j,ans=0; cout<<endl; do { j=k; k=q->date[k].pre; q->date[j].pre=-1; }while(k!=0); cout<<"一条路径如下:"<<endl; k=0; while(k<maxsize) { if(q->date[k].pre==-1) { ans++; cout<<" ("<<q->date[k].i<<','<<q->date[k].j<<')'; if(ans%5==0) cout<<endl; } ++k; } cout<<endl; } bool mgpath1(int xi,int yi,int xe,int ye) { box e; int i,j,di,il,jl; qutype *q; initqueue(q); e.i==xi; e.j=yi; e.pre=-1; enqueue(q,e); mg[xi][yi]=-1; while(!emptyqueue(q)) { dequeue(q,e); i=e.i;j=e.j; if(xe==i&&ye==j) { print(q,q->front); destoryqueue(q); return true; } for(di=0;di<4;di++) { switch(di) { case 0:il=i-1;jl=j;break; case 1:il=i;jl=j+1;break; case 2:il=i+1;jl=j;break; case 3:il=i;jl=j-1;break; } if(mg[il][jl]==0) { e.i=il; e.j=jl; e.pre=q->front;//指向路径中上一个方块的下标 enqueue(q,e);//(il,jl)方块进队 mg[il][jl]=-1;//将其赋值避免回过来反复搜索 } } } destoryqueue(q);//销毁队列 return false;//未找到任何路径时返回假 } int main() { if(!mgpath1(1,1,8,8)) cout<<"该迷宫无解"<<endl; return 0; }
转载一篇用类实现的 http://blog.csdn.net/seu_nuaa_zc/article/details/73188376?locationNum=8&fps=1
相关文章推荐
- 队列实现迷宫问题
- 数据结构和算法设计(迷宫求解问题的栈和队列的实现)
- 队列实现迷宫问题(精装版,可返回路径)
- 第12周上机实践项目5 - 迷宫问题之图深度优先遍历解法
- 第5周实践项目4 用栈实现运算表达式求值
- 迷宫问题队列实现
- 第十二周上机实践—项目5—迷宫问题之图深度优先遍历解法
- 第7周实践项目1.1 环形队列中用队尾和队的元素个数来实现队列的算法库
- 栈和队列的底层实现及迷宫问题
- 第十二周实践项目5—迷宫问题
- 队列实现迷宫问题(垃圾版)
- c++链表实现队列,深搜加宽搜,加模版类实现迷宫问题;
- 第十二周实践项目~迷宫问题—图深度优先遍历
- 第7周项目实践2.1 用只有尾节点指针rear的循环单链表实现队列算法库
- 【orange】OrangeS一个操作系统的实现:第四章实践方面遇到的一些问题
- C++链栈实现迷宫问题
- 迷宫问题--非递归回溯 C语言实现
- Asp.Net大型项目实践(6)-用Unity实现层与层之间的接口调用(附源码)
- Asp.Net大型项目实践(9)-ExtJs实现系统框架页(非iframe,附源码,在线demo)
- Flex项目中如何实现跨域访问问题