回 帖 发 新 帖 刷新版面

主题:自己写的迷宫探路程序,求高手赐教

最近几天我在编个迷宫算法,用链栈存储的。首先构造了一个有外围的迷宫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);
    
}

回复列表 (共3个回复)

沙发

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;//这个-1,就是说,一旦走错了,就无法回去了,因为你的if里没有这个判断
                Findpath(S);
            }
            
现在的问题是,如果走错了,可不可以往回走,可以的话,那个-1要改改,还有后面的那个
if(count==4)
        {
           p=Pop(S);
           x=p->X;y=p->Y;
           maze[x][y]=-1;
           p=S->next;
           Findpath(p);
        }
自然也要改了

  

 

板凳

感觉楼主缺少了对每个case后else部分的考虑。走正确路线肯定没问题,但一旦走错路再返回时因为后退的格子可能都被置-1了,以致程序假死了

3 楼

你好.我是全职网赚工作者.
如果你有时间有电脑.
想在网络上创业.请联系我..
项目绝对真实.详情QQ空间资料
加盟请联系 QQ908889846

我来回复

您尚未登录,请登录后再回复。点此登录或注册