主题:自己写的迷宫探路程序,求高手赐教
最近几天我在编个迷宫算法,用链栈存储的。首先构造了一个有外围的迷宫maze【10】【10】,起点在maze【1】【1】,终点在maze【8】【8】,然后让起点入栈,并把它所在位置置为-1,避免重复。然后对栈顶元素搜索(向其余3个方向,搜索),只要找到路的话,就让它入栈,然后又对新进栈的元素搜索,如果3个方向都搜索完,没有路可走就弹出栈,并置该元素为-1,就这样依次搜索,知道找到出口,最后展示整个堆栈,就算找到路径了。
我现在把程序编好了,也可以运行了,而且当按照正确的路走时能够显示正确走过的路,但是有个问题,当走入死胡同的时候就走不出来了,麻烦帮我看下,不胜感激
#include<iostream>
#define Max 10
using namespace std;
int maze[Max][Max]={
{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 megong
{
int X,Y;
struct megong *next;
}*Link;
void Creat(Link &S)
{
S=(Link)malloc(sizeof(megong));
S->next=NULL;
}
void Push(Link &S,int x,int y)
{
Link p;
p=(Link)malloc(sizeof(megong));
p->X=x;
p->Y=y;
p->next=S;
S=p;
}
Link Pop(Link &S)
{
Link p;
p=(Link)malloc(sizeof(megong));
if(p=NULL)
{
cout<<"The stack is empty"<<endl;
return 0;
}
else
{
p=S;
S=S->next;
free(p);
return S;
}
}
void display(Link &S)
{
while(S)
{
cout<<"("<<S->X<<","<<S->Y<<")"<<endl;
S=S->next;
}
}
void Findpath(Link &S)//探索栈顶元素
{
Link p;
int count=1;
int ch;
int x,y;
if((S->X==Max-2)&&(S->Y==Max-2))
{
cout<<"You find the path"<<endl;
display(S);
}
else
{
while(count<=3)
{
cout<<"Please choose where to go(2,4,6,8)"<<endl;
cin>>ch;
switch(ch)
{
case 2:
x=S->X+1;y=S->Y;
if(maze[x][y]==0)
{
Push(S,x,y);
maze[x-1][y]=-1;
Findpath(S);
}
else
{
cout<<"This is a dead way,please try again"<<endl;
count++;
}
break;
case 4:
x=S->X;y=S->Y-1;
if(maze[x][y]==0)
{
Push(S,x,y);
maze[x][y+1]=-1;
Findpath(S);
}
else
{
cout<<"This is a dead way,please try again"<<endl;
count++;
}
break;
case 6:
x=S->X;y=S->Y+1;
if(maze[x][y]==0)
{
Push(S,x,y);
maze[x][y-1]=-1;
Findpath(S);
}
else
{
cout<<"This is a dead way,please try again"<<endl;
count++;
}
break;
case 8:
x=S->X-1;y=S->Y;
if(maze[x][y]==0)
{
Push(S,x,y);
maze[x+1][y]=-1;
Findpath(S);
}
else
{
cout<<"This is a dead way,please try again"<<endl;
count++;
}
break;
default:
cout<<"Error Direcation"<<endl;
break;
}
}
if(count==4)
{
p=Pop(S);
x=p->X;y=p->Y;
maze[x][y]=-1;
p=S->next;
Findpath(p);
}
}
}
void main()
{
Link S;
Creat(S);
int x,y;
x=1;y=1;
Push(S,x,y);
Findpath(S);
}
我现在把程序编好了,也可以运行了,而且当按照正确的路走时能够显示正确走过的路,但是有个问题,当走入死胡同的时候就走不出来了,麻烦帮我看下,不胜感激
#include<iostream>
#define Max 10
using namespace std;
int maze[Max][Max]={
{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 megong
{
int X,Y;
struct megong *next;
}*Link;
void Creat(Link &S)
{
S=(Link)malloc(sizeof(megong));
S->next=NULL;
}
void Push(Link &S,int x,int y)
{
Link p;
p=(Link)malloc(sizeof(megong));
p->X=x;
p->Y=y;
p->next=S;
S=p;
}
Link Pop(Link &S)
{
Link p;
p=(Link)malloc(sizeof(megong));
if(p=NULL)
{
cout<<"The stack is empty"<<endl;
return 0;
}
else
{
p=S;
S=S->next;
free(p);
return S;
}
}
void display(Link &S)
{
while(S)
{
cout<<"("<<S->X<<","<<S->Y<<")"<<endl;
S=S->next;
}
}
void Findpath(Link &S)//探索栈顶元素
{
Link p;
int count=1;
int ch;
int x,y;
if((S->X==Max-2)&&(S->Y==Max-2))
{
cout<<"You find the path"<<endl;
display(S);
}
else
{
while(count<=3)
{
cout<<"Please choose where to go(2,4,6,8)"<<endl;
cin>>ch;
switch(ch)
{
case 2:
x=S->X+1;y=S->Y;
if(maze[x][y]==0)
{
Push(S,x,y);
maze[x-1][y]=-1;
Findpath(S);
}
else
{
cout<<"This is a dead way,please try again"<<endl;
count++;
}
break;
case 4:
x=S->X;y=S->Y-1;
if(maze[x][y]==0)
{
Push(S,x,y);
maze[x][y+1]=-1;
Findpath(S);
}
else
{
cout<<"This is a dead way,please try again"<<endl;
count++;
}
break;
case 6:
x=S->X;y=S->Y+1;
if(maze[x][y]==0)
{
Push(S,x,y);
maze[x][y-1]=-1;
Findpath(S);
}
else
{
cout<<"This is a dead way,please try again"<<endl;
count++;
}
break;
case 8:
x=S->X-1;y=S->Y;
if(maze[x][y]==0)
{
Push(S,x,y);
maze[x+1][y]=-1;
Findpath(S);
}
else
{
cout<<"This is a dead way,please try again"<<endl;
count++;
}
break;
default:
cout<<"Error Direcation"<<endl;
break;
}
}
if(count==4)
{
p=Pop(S);
x=p->X;y=p->Y;
maze[x][y]=-1;
p=S->next;
Findpath(p);
}
}
}
void main()
{
Link S;
Creat(S);
int x,y;
x=1;y=1;
Push(S,x,y);
Findpath(S);
}