回溯算法解决迷宫问题

迷宫问题,首先解决的是两个问题:1、什么时候可以继续走也就是什么时候可以走?–a、不出界限、没有到限制条件,它可以继续运行。—b、当没有障碍物时候就继续运行:这里也会有问题,我走到这里时候那么我又该怎样来走了啊?这是不是你得给它怎样去走啊,对于数组选择路径就是把它的坐标给它加上或减去一个数不是就可以在四周走了。2、它在什么时候走,它停下来是什么条件,遇到障碍物又该怎样走啊?a、出界、运障碍物时候,就会停下来,它会“反悔”到上一步,那就是我们此时要怎样可以让它悔步;b、要悔步你得要知道前一步怎样走?你首先做的事要纪录你前一步走的,这就要借助一个container来做辅助保存,才可以知道继续下去。

迷宫问题就是以上我的思维步骤,下面是具体的代码实现,我是以5*5为例子写的:

using namespacestd;

/*

  5 * 5 labyrinth ,0—barrier,1—-pass,2—gone

 */

#define  N 5

#define M 5

struct node{

    int x;

    int y;

    int value;

};

#define InISize 20

#define AppSize 50

typedef struct Stack{

    int Capacity;

    node*base;

    node*top;

}Stack;

void IniStack(Stack *s)

{

    s->base=(node*)malloc(InISize*sizeof(node));

    if(!s->base)

        return ;

    s->top=s->base;

    s->Capacity=InISize;

}

int EmptyStack(Stack *s)

{

    if(s->base==s->top)

    {

        return 1;

    }

    else

        return 2;

}

void ClearStack(Stack *s)

{

    s->top=s->base;

}

void Push(Stack *s,node d)

{

    

    if(s->top-s->base>=s->Capacity)

    {

        s->base=(node*)realloc(s->base,AppSize*sizeof(node));

        if(!s->base)

            return ;

        s->top=s->base+s->Capacity;

        s->Capacity=AppSize+InISize;

    }

    *(s->top)=d;

    s->top++;

}

void pop(Stack *s,node *D)

{

    if(s->top==s->base)

        return ;

    s->top–;

    *D=*(s->top);

}

void destroyStack(Stack *s)

{

    free(s->base);

    s->top=s->base;

    s->Capacity=0;

}

//navigaten :east north south west

int x[4]={-1,0,1,0};

int y[4]={0,-1,0,1};

/*1- go through this node,so return true others return false */

int check(int i,int j)

{

    int a=1;

    if(i<0||i>=N)

    {

       a=1;

    }

    if(j<0||j>=M)

        a=1;

     if(i>=0&&i<N)

         if(j>=0&&j<M)

             a=0;

    return a;

}

//navigaten :east north south west

int x[4]={-1,1,0,0};

int y[4]={0,0,1,-1};

/*1- go through this node,so return true others return false */

int check(int i,int j)

{

    int a=1;

    if(i<0||i>=N)

    {

       a=1;

    }

    if(j<0||j>=M)

        a=1;

     if(i>=0&&i<N)

         if(j>=0&&j<M)

             a=0;

    return a;

}

void labyrinth(int **map,constint & start_x,constint& start_y)

{

    

    int u=start_x;

    int v=start_y;

    int n,m;

    Stack S;

    IniStack(&S);

    map[u][v]=2;

    node *p=(node*)malloc(sizeof(node));

    p->x=u;

    p->y=v;

    p->value=map[u][v];

    Push(&S, *p);

    m=u;

    n=v;

    while(1)

    {

        /*

           direction ,choose the way where is the true;

         */

        for(int k=0;k<4;k++)

        {

            m=u+x[k];

            n=v+y[k];

            //true,—-go

            if(check(m, n)==0&&map[m][n]==1)

            {

                break;

            }

            if(check(m, n)==1&&k==3)

                //here ,you can get the privoius steps

                if(map[m][n]==0&&EmptyStack(&S)!=1)

            {

                node *s=(node*)malloc(sizeof(node));

                pop(&S, s);

                u=s->x;

                v=s->y;

                k=0;

                

            }

        }

        cout<<“(“<<m<<“,”<<n<<“)”;

        map[m][n]=2;

        node * l=(node*)malloc(sizeof(node));

        l->x=m;

        l->y=n;

        l->value=map[m][n];

        //recording

        u=m;

        v=n;

        Push(&S, *p);

        if(m==N-1&&n==M-1)

        {

            break;

        }

        

    }

    cout<<endl;

    cout<<“the @ is gone:”<<endl;

    for(int i=0;i<N;i++)

    {

        for(int j=0;j<M;j++)

            if(map[i][j]==2)

            cout<<“@”<<“\t”;

        elsecout<<map[i][j]<<“\t”;

        cout<<endl;

    }

    }

int main(int argc,constchar * argv[]) {

    // insert code here…

    std::cout <<“Hello, World!\n”;

    int **map=newint *[N];

    for(int i=0;i<N;i++)

        map[i]=newint [M];

    for(int i=0;i<N;i++)

        for(int j=0;j<M;j++)

            cin>>map[i][j];

    int start_x;

    int start_y;

    cout<<“the starting localtion:”;

    cin>>start_x>>start_y;

    labyrinth(map, start_x, start_y);   

    return 0;

}

example:《回溯算法解决迷宫问题》



    原文作者:迷宫问题
    原文地址: https://blog.csdn.net/qq_33405276/article/details/77543267
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞