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') 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.结果

01迷宫问题

 

上一篇:20.unittest使用


下一篇:UVa 1663 - Purifying Machine(二分匹配)