#include <iostream>#include <cstdlib>#include <ctime>#include <windows.h>
using namespace std;int M;int N;int **maze;
class stack_for_maze//记录路径{ private: struct node { int x; int y; char direction; //上一步方向 node* next; }; node* head; public: stack_for_maze() { head=NULL; } ~stack_for_maze() { node* p=head; while(head!=NULL) { head=head->next; delete p; p=head; } } void push(int xx,int yy,char ddirection)//压栈 { node* new_node; new_node=new node; if(new_node!=NULL) { new_node->x=xx; new_node->y=yy; new_node->direction=ddirection; new_node->next=NULL; if(head==NULL) head=new_node; else { new_node->next=head; head=new_node; } } else cout<<"\n压栈失败!"; } node* pop(int& xx,int& yy) { if(head!=NULL) { node* p=head; head=head->next; xx=p->x; yy=p->y; delete p; } else { cout<<"\n出栈失败!"; } return head; } void print() { if(head!=NULL) { node* p=head; while(p!=NULL) { cout<<" "<<p->x<<" "<<p->y<<" "<<p->direction<<endl; p=p->next; } } else cout<<"\n打印失败!"; }};void mazeGenerator(){ cout<<"请输入迷宫的宽度:"; cin>>M; cout<<"请输入迷宫的高度:"; cin>>N; maze = (int **)malloc(sizeof(int *)*M); int i; for (i=0;i<M;i++) maze[i]=(int *)malloc(sizeof(int)*N); int max_way=M*N; int x,y; for(x=0;x<M;x++) for(y=0;y<N;y++) maze[x][y]=1;
srand((unsigned)time(NULL)); for(int i=0;i<=max_way;i++) { x=rand()%(M-2)+1; y=rand()%(N-2)+1; maze[x][y]=0; } maze[1][1]=0; maze[M-2][N-2]=0;
maze[0][1]=2; maze[M-1][N-2]=0;}void PrintMaze(){ int x,y; system("cls"); cout<<endl; for(x=0;x<M;x++) { for(y=0;y<N;y++) { if(maze[x][y]==0||maze[x][y]==8){cout<<"..";continue;} if(maze[x][y]==1){cout<<"##";continue;} if(maze[x][y]==2||maze[x][y]==3||maze[x][y]==4||maze[x][y]==5||maze[x][y]==6){cout<<"×";continue;} if(maze[x][y]==7){cout<<"※";continue;} } cout<<endl; } Sleep(200);}void mazeTraverse(stack_for_maze &s){ int x=1,y=1; while(1) { maze[x][y]=2; if(maze[x+1][y]==0) { s.push(x,y,'D'); maze[x][y]=3;//标记D x=x+1; maze[x][y]=7;//标记当前位置 PrintMaze(); if((x==M-1)&&(y==N-2)) { s.push(x,y,'*'); cout<<"\n成功走出迷宫!!\n"; return; } else continue; } if(maze[x][y+1]==0) { s.push(x,y,'R'); maze[x][y]=4; y=y+1; maze[x][y]=7; PrintMaze(); if((x==M-1)&&(y==N-2)) { s.push(x,y,'*'); cout<<"\n成功走出迷宫!!\n"; return; } else continue; } if(maze[x-1][y]==0) { s.push(x,y,'U'); maze[x][y]=6; x=x-1; maze[x][y]=7; PrintMaze(); if((x==M-1)&&(y==N-2)) { s.push(x,y,'*'); cout<<"\n成功走出迷宫!!\n"; return; } else continue; } if(maze[x][y-1]==0) { s.push(x,y,'L'); maze[x][y]=5; y=y-1; maze[x][y]=7; PrintMaze(); if((x==M-1)&&(y==N-2)) { s.push(x,y,'*'); cout<<"\n成功走出迷宫!!\n"; return; } else continue; } if(s.pop(x,y)==NULL&&maze[x-1][y]!=0&&maze[x][y-1]!=0&&maze[x][y+1]!=0&&maze[x+1][y]!=0) { cout<<"\n回退到入口!!!\n"; maze[0][1]=7; if(maze[1][1]!=1)maze[1][1]=2; return; } }}void main(){ mazeGenerator(); cout<<"▔▔▔▔▔▔▔▔▔▔▔▔▔..表示迷宫的通路!▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n"; cout<<"▔▔▔▔▔▔▔▔▔▔▔▔▔##表示迷宫的墙壁!▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n"; cout<<"▔▔▔▔▔▔▔▔▔▔▔▔▔※表示站立的位置!▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n"; cout<<"▔▔▔▔▔▔▔▔▔▔▔▔▔×表示走过的路径!▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n"; Sleep(4000); PrintMaze(); stack_for_maze stack; mazeTraverse(stack); system("pause");}
using namespace std;int M;int N;int **maze;
class stack_for_maze//记录路径{ private: struct node { int x; int y; char direction; //上一步方向 node* next; }; node* head; public: stack_for_maze() { head=NULL; } ~stack_for_maze() { node* p=head; while(head!=NULL) { head=head->next; delete p; p=head; } } void push(int xx,int yy,char ddirection)//压栈 { node* new_node; new_node=new node; if(new_node!=NULL) { new_node->x=xx; new_node->y=yy; new_node->direction=ddirection; new_node->next=NULL; if(head==NULL) head=new_node; else { new_node->next=head; head=new_node; } } else cout<<"\n压栈失败!"; } node* pop(int& xx,int& yy) { if(head!=NULL) { node* p=head; head=head->next; xx=p->x; yy=p->y; delete p; } else { cout<<"\n出栈失败!"; } return head; } void print() { if(head!=NULL) { node* p=head; while(p!=NULL) { cout<<" "<<p->x<<" "<<p->y<<" "<<p->direction<<endl; p=p->next; } } else cout<<"\n打印失败!"; }};void mazeGenerator(){ cout<<"请输入迷宫的宽度:"; cin>>M; cout<<"请输入迷宫的高度:"; cin>>N; maze = (int **)malloc(sizeof(int *)*M); int i; for (i=0;i<M;i++) maze[i]=(int *)malloc(sizeof(int)*N); int max_way=M*N; int x,y; for(x=0;x<M;x++) for(y=0;y<N;y++) maze[x][y]=1;
srand((unsigned)time(NULL)); for(int i=0;i<=max_way;i++) { x=rand()%(M-2)+1; y=rand()%(N-2)+1; maze[x][y]=0; } maze[1][1]=0; maze[M-2][N-2]=0;
maze[0][1]=2; maze[M-1][N-2]=0;}void PrintMaze(){ int x,y; system("cls"); cout<<endl; for(x=0;x<M;x++) { for(y=0;y<N;y++) { if(maze[x][y]==0||maze[x][y]==8){cout<<"..";continue;} if(maze[x][y]==1){cout<<"##";continue;} if(maze[x][y]==2||maze[x][y]==3||maze[x][y]==4||maze[x][y]==5||maze[x][y]==6){cout<<"×";continue;} if(maze[x][y]==7){cout<<"※";continue;} } cout<<endl; } Sleep(200);}void mazeTraverse(stack_for_maze &s){ int x=1,y=1; while(1) { maze[x][y]=2; if(maze[x+1][y]==0) { s.push(x,y,'D'); maze[x][y]=3;//标记D x=x+1; maze[x][y]=7;//标记当前位置 PrintMaze(); if((x==M-1)&&(y==N-2)) { s.push(x,y,'*'); cout<<"\n成功走出迷宫!!\n"; return; } else continue; } if(maze[x][y+1]==0) { s.push(x,y,'R'); maze[x][y]=4; y=y+1; maze[x][y]=7; PrintMaze(); if((x==M-1)&&(y==N-2)) { s.push(x,y,'*'); cout<<"\n成功走出迷宫!!\n"; return; } else continue; } if(maze[x-1][y]==0) { s.push(x,y,'U'); maze[x][y]=6; x=x-1; maze[x][y]=7; PrintMaze(); if((x==M-1)&&(y==N-2)) { s.push(x,y,'*'); cout<<"\n成功走出迷宫!!\n"; return; } else continue; } if(maze[x][y-1]==0) { s.push(x,y,'L'); maze[x][y]=5; y=y-1; maze[x][y]=7; PrintMaze(); if((x==M-1)&&(y==N-2)) { s.push(x,y,'*'); cout<<"\n成功走出迷宫!!\n"; return; } else continue; } if(s.pop(x,y)==NULL&&maze[x-1][y]!=0&&maze[x][y-1]!=0&&maze[x][y+1]!=0&&maze[x+1][y]!=0) { cout<<"\n回退到入口!!!\n"; maze[0][1]=7; if(maze[1][1]!=1)maze[1][1]=2; return; } }}void main(){ mazeGenerator(); cout<<"▔▔▔▔▔▔▔▔▔▔▔▔▔..表示迷宫的通路!▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n"; cout<<"▔▔▔▔▔▔▔▔▔▔▔▔▔##表示迷宫的墙壁!▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n"; cout<<"▔▔▔▔▔▔▔▔▔▔▔▔▔※表示站立的位置!▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n"; cout<<"▔▔▔▔▔▔▔▔▔▔▔▔▔×表示走过的路径!▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n"; Sleep(4000); PrintMaze(); stack_for_maze stack; mazeTraverse(stack); system("pause");}

