• 迷宫求解


    1.找出一条迷宫通路

    思路:用0表示当前路径可通,-1表示当前路径不可通,1表示当前路径已经走过。

    do{

    从起点开始判断,如果当前位置可通,就把当前位置加入到栈中,并且记录路径。然后判断是否为出口,如果不是出口,则将该位置的东邻作为下一个位置。

    如果当前位置不可通,则判断栈顶元素是否已经搜查过所有的方向了,如果是,则弹出当前栈顶元素,并将该位置设置为不可通。

    如果当前位置不可通,并且栈顶元素的方向没有全部搜查完,则把下一个方向作为待探索的位置。

    }while(栈不为空);

    //迷宫求解。
    #include"stdlib.h"    
    #include"stdio.h"
    #include<stack>
    #include<iostream>
    using namespace std;
    
    typedef struct Pseat{
        int x;
        int y;
        bool operator==(Pseat& rhs)
        {
            return x==rhs.x&&y==rhs.y;
        }   //操作符重载
    };
    
    typedef struct{
        Pseat seat;   //通道块在迷宫中的位置
        int di;     //下一通道块的方向
    }ElemType;
    
    Pseat findNextSeat(Pseat cur,int di)  //结构体是可以直接返回的?
    {
        Pseat nextSeat;
        if(di==1)
        {
            nextSeat.x=cur.x+1;
            nextSeat.y=cur.y;
        }
        else if(di==2)
        {
            nextSeat.x=cur.x;
            nextSeat.y=cur.y+1;
        }
        else if(di==3)
        {
            nextSeat.x=cur.x-1;
            nextSeat.y=cur.y;
        }
        else if(di==4)
        {
            nextSeat.x=cur.x;
            nextSeat.y=cur.y-1;
        }
        return nextSeat;
    }
    
    stack<ElemType> findPath(int migong[][10],Pseat start,Pseat end)   //栈可以返回
    {  
        stack<ElemType> path;
        Pseat curpos=start;    //当前路径。注意结构体初始化的方法
        do 
        {
            if(migong[curpos.x][curpos.y]==0)   //当前位置为迷宫墙壁则为-1,当前位置可通则为0,当前位置已经走过则为1,当前位置不通则为2
            {
                migong[curpos.x][curpos.y]=1;   //留下足迹,走过的位置置1
                ElemType current={curpos,1};
                path.push(current);    //加入路径
                if(curpos==end)   //为出口,则结束.注意,在这里curpos==end是错的
                    return path;
                else                                //否则切换到该点的东邻为下一个点
                {
                    curpos=findNextSeat(curpos,1);
                }
            }
            else
            {
                ElemType *top=&path.top();
                if(top->di==4)  //如果当前位置不通,并且栈顶块的4个方向都探索完,则弹出栈顶元素
                {
                    migong[top->seat.x][top->seat.y]=2;   //留下不能通过的足迹
                    path.pop();
                }
                else                //否则就换一个方向探索
                {
                    (top->di)++;   
                    curpos=findNextSeat(top->seat,top->di);
                }
            }
        } while (!path.empty());
        return path;
    }
    
    void main()
    {
        int migong[][10]={{-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}};
        Pseat start={1,1};
        Pseat end={8,8};
        stack<ElemType> path=findPath(migong,start,end);
        while(!path.empty())
        {
            cout<<path.top().seat.x<<","<<path.top().seat.y<<endl;
            path.pop();
        }
    }

    2.找出所有的迷宫通路

  • 相关阅读:
    图像切割之(五)活动轮廓模型之Snake模型简单介绍
    拓扑排序的原理及事实上现
    理解class.forName()
    Java中StringBuilder的清空方法比較
    Java实现BASE64编解码
    KMP算法具体解释(贴链接)
    基于PCM2912a的USB声卡设计
    51. 腾讯面试题:一个二叉树,中序遍历,找一个节点的后一个节点
    Handler和HandlerThread
    Nuget-QRCode:jquery-qrcode
  • 原文地址:https://www.cnblogs.com/wy1290939507/p/4801436.html
Copyright © 2020-2023  润新知