ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

01迷宫问题

2021-02-03 11:02:57  阅读:203  来源: 互联网

标签:current 01 int 迷宫 问题 && path maze content


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') 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]!='\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<<"\nput 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<<"\n-------------------The Path------------------\n";
    for(int i=0;i<totalPath.size();i++){
        cout<<"Path"<<i+1<<":\t";
        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<<"\n请按任意键退出(非回车)";
    cin>>maze->m;
    return 0;
}

4.结果

 

标签:current,01,int,迷宫,问题,&&,path,maze,content
来源: https://www.cnblogs.com/fangexuxiehuihuang/p/14365842.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有