• 01迷宫问题


    1.问题描述

    文件中有一矩形迷宫。0代表该点可行,1代表该点不可行,2代表终点。从一个为0的点开始出发,找出所有到达终点的路径。

    2.算法思路

    (1)采用深度优先遍历,递归解集树

    (2)设置记忆性

      <1>空间换时间:刷新传入新地图

      <2>时间换空间:记录走过的路径

    3.代码

    #include <iostream>
    #include<vector>
    using namespace std;
    
    
    class Maze{ //迷宫构造函数,由于只有一个迷宫,故将规模直接放入public以不需写get/set
        public:
            int m;//迷宫的行数
            int n;//迷宫的列数
            char** content;//迷宫内容的数组
            Maze(){
                const int MAX_SIZE = 100;//最大问题规模
                int m=0,n=0;
                /*首先计算出迷宫的规模*/
                FILE* file = fopen("map.txt","r");
                char * buffer = new char;
                bool firstRead = true;
                while(!feof(file)){
                    fgets(buffer,MAX_SIZE,file);
                    if(firstRead){
                        while(buffer[n]&&buffer[n]!='
    ') n++;
                        firstRead = false;
                    }
                    m++;
                }
                rewind(file);
                this->m=m;
                this->n=n;
                this->content = new char *[m];
                /*将迷宫内容赋值*/
                m=0;
                while(!feof(file)){
                    this->content[m] = new char[n];
                    n=0;
                    fgets(buffer,MAX_SIZE,file);
                    while(buffer[n]&&buffer[n]!='
    '){
                        this->content[m][n]=buffer[n];
                        n++;
                    }
                    m++;
                }
                fclose(file);
            };
            void mazeShow(){//迷宫展示函数,0为可走位置。1为不可走位置,2为终点
                for(int i=0;i<this->m;i++){
                    for(int j=0;j<this->n;j++){
                        cout<<this->content[i][j]<<" ";
                    }
                    cout<<endl;
                }
            }
    
    };
    class PathNode{//路径节点类
        private:
            int x;
            int y;
        public:
            PathNode(int x,int y){
                this->x=x;
                this->y=y;
            };
            int getX(){
                return this->x;
            };
            int getY(){
                return this->y;
            };
    
    };
    
    vector<vector<PathNode>> totalPath;//所有路线
    
    bool notRepeat(PathNode* currentNode,vector<PathNode> path){//判断是否走重复
        bool notRepeatMark = true;
        for(int i=0;i<path.size();i++){
            if(path.at(i).getX()==currentNode->getX()&&path.at(i).getY()==currentNode->getY()){
                notRepeatMark = false;
            }
        }
        return notRepeatMark;
    }
    
    void searchMaze(int current_x,int current_y,Maze* maze,vector<PathNode> path){//寻找函数
        path.push_back(*(new PathNode(current_x,current_y)));//将当前节点加入路径
        if(maze->content[current_x][current_y]=='2'){//出口,将当前路径加入总路径
            totalPath.push_back(path);
        }
        else{
            if(current_y-1>=0&&maze->content[current_x][current_y-1]!='1'&&notRepeat(new PathNode(current_x,current_y-1),path)){
                searchMaze(current_x,current_y-1,maze,path);//向上
            }
            if(current_y+1<maze->n&&maze->content[current_x][current_y+1]!='1'&&notRepeat(new PathNode(current_x,current_y+1),path)){
                searchMaze(current_x,current_y+1,maze,path);//向下
            }
            if(current_x-1>=0&&maze->content[current_x-1][current_y]!='1'&&notRepeat(new PathNode(current_x-1,current_y),path)){
                searchMaze(current_x-1,current_y,maze,path);//向左
            }
            if(current_x+1<maze->m&&maze->content[current_x+1][current_y]!='1'&&notRepeat(new PathNode(current_x+1,current_y),path)){
                searchMaze(current_x+1,current_y,maze,path);//向右
            }
        }
    }
    
    
    
    int main()
    {
        Maze* maze = new Maze();
        maze->mazeShow();
        int start_x,start_y;
        cout<<"
    put in the start position X:";
        cin>>start_x;
        cout<<"put in the end   position Y:";
        cin>>start_y;
        vector<PathNode> path;
        searchMaze(start_x,start_y,maze,path);
    
        cout<<"
    -------------------The Path------------------
    ";
        for(int i=0;i<totalPath.size();i++){
            cout<<"Path"<<i+1<<":	";
            for(int j=0;j<totalPath.at(i).size();j++){
                cout<<totalPath.at(i).at(j).getX()<<","<<totalPath.at(i).at(j).getY();
                if(j!=totalPath.at(i).size()-1) cout<<" -> ";
            }
            cout<<endl;
        }
        cout<<"
    请按任意键退出(非回车)";
        cin>>maze->m;
        return 0;
    }

    4.结果

  • 相关阅读:
    mysqldump 导出数据库为DBname的表名为Tname的表结构 导出数据库的所有表的表结构
    mysqldump 备份某张表 Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions,
    nfs missing codepage or helper program, or other error
    date 增加一个小时 减少一个小时
    mysqldump 备份单个数据库
    mysql删除账户
    怎么删除某个用户的所有帖子?
    mongodb删除重复数据
    ReSharper2018破解详细方法
    激活windows和office
  • 原文地址:https://www.cnblogs.com/fangexuxiehuihuang/p/14365842.html
Copyright © 2020-2023  润新知