资源描述:
《迷宫问题实验报告.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、数据结构作业之迷宫问题蔡有夫计080420082939原代码:Stack.cpp:implementationoftheCStackclass.#include"Stack.h"#includeCStack::CStack(){head=NULL;depth=0;}CStack::~CStack(){}voidCStack::Pop(SElemType&e){classpoint*p=newpoint;//e=head->data{e.di=head->data.di;e.ord=head->data.ord;e.seat.x=head->data.se
2、at.x;e.seat.y=head->data.seat.y;}p=head;head=head->next;depth--;deletep;}voidCStack::Push(SElemType&e){classpoint*p=newpoint;//p->data=e;{p->data.di=e.di;p->data.ord=e.ord;p->data.seat.x=e.seat.x;p->data.seat.y=e.seat.y;}p->next=head;head=p;depth++;}boolCStack::Empty(){if(depth>=1)returnfals
3、e;elsereturntrue;}voidCStack::print(){SElemTypee;while(!Empty()){Pop(e);cout<<"("<#include"Stack.h"usingnamespacestd;intmaze[10][10]={{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
4、,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}};intmazepass[10][10]={0};classPosTypestart(1,1),end(8,8);voidNextPos(PosType&curpos,PosType&cur1,intk);intmain(){classCStackS;classPosTypecurpos(start.x,start.y);intcurstep
5、=1;do{if(0==maze[curpos.x][curpos.y]&&0==mazepass[curpos.x][curpos.y]){mazepass[curpos.x][curpos.y]=1;SElemTypee(curstep,curpos,1);S.Push(e);//加入路径if(curpos.x==end.x&&curpos.y==end.y){cout<<"到达终点";for(inti=0;i<10;i++){cout<6、);return1;}NextPos(curpos,curpos,1);curstep++;}else{SElemTypee;if(!S.Empty()){S.Pop(e);}while(4==e.di&&!S.Empty()){mazepass[e.seat.x][e.seat.y]=1;S.Pop(e);}if(e.di<4){e.di++;S.Push(e);NextPos(curpos,e.seat,e.di);}}}while(!S.Empty());return0;}voidNextPos(PosType&curpos,PosType&cur1,intk){swit
7、ch(k){case1:curpos.x=cur1.x+1;curpos.y=cur1.y;break;case2:curpos.y=cur1.y+1;curpos.x=cur1.x;break;case3:curpos.x=cur1.x-1;curpos.y=cur1.y;break;case4:curpos.x=cur1.x;curpos.y=cur1.y-1;break;}}