迷宫求解非递归 DFS BFS(应用栈和队列)

简介:

栈和队列的应用对迷宫问题求解 没有递归 自己手动建的栈和队 并且输出路径 DFS的路径就是

栈中的坐标 BFS的路径在队又开了一个域存上一层的base值 语言还是用的C++ 感觉比C的封装性好很多

充分体会了一下DFS一边比BFS快 但是BFS是最优解而DFS可能不是最优解

 

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define Maze_size 100 //定义迷宫最大值
class Maze
{
public:
    char Maze_map[Maze_size][Maze_size];
    bool Maze_map_bj[Maze_size][Maze_size];
    int stack[Maze_size][2],top;//栈 栈顶指针
    int queue[Maze_size][3],base,qtop;//队列 0 1存坐标 2记录路径 对首指针 队尾指针
    int length,wide;//迷宫长,宽(竖,横)
    int startx,starty;//起点坐标
    int step[4][2];
    Maze();//初始化迷宫
    void input();//从键盘输入
    bool DFS();//利用栈深度优先遍历
    bool BFS();//利用队列广度优先遍历
    void outputDFSmap();//输出深度优先遍历路径
    void outputBFSmap();//输出广度优先遍历路径
};

Maze::Maze()//初始化
{
    length=wide=0;
    startx=starty=0;
    step= {{0,1},{0,-1},{1,0},{-1,0}};
    top=0;
}

void Maze::input()//输入
{
    do
    {
        cout<<"input length and wide of maze(length>0,wide>0)"<<endl;
        cin>>length>>wide;
    }
    while(length<=0||wide<=0);
    cout<<"input maze"<<endl;
    for(int i=0; i<length; i++)
        for(int j=0; j<wide; j++)
        {
            cin>>Maze_map[i][j];
            if(Maze_map[i][j]=='S')
                startx=i,starty=j;
        }
    cout<<"input end"<<endl;
}

bool Maze::DFS()
{
    top=0;
    memset(Maze_map_bj,0,sizeof(Maze_map_bj));
    stack[++top][0]=startx,stack[top][1]=starty;
    Maze_map_bj[startx][starty]=1;
    while(top!=0)
    {
        int x=stack[top][0],y=stack[top][1];
        for(int i=0; i<4; i++)
            if(Maze_map[x+step[i][0]][y+step[i][1]]=='E')
                return 1;
        bool flag=0;
        for(int i=0; i<4; i++)
            if(Maze_map[x+step[i][0]][y+step[i][1]]=='.'&&!Maze_map_bj[x+step[i][0]][y+step[i][1]])
            {
                stack[++top][0]=x+step[i][0],stack[top][1]=y+step[i][1];
                Maze_map_bj[x+step[i][0]][y+step[i][1]]=1;
                flag=1;
                break;
            }
        if(flag)
            continue;
        top--;
    }
    return 0;
}

void Maze::outputDFSmap()
{
    if(!DFS())
    {
        cout<<"maze bu neng zou = = "<<endl;
        return ;
    }
    char newmap[Maze_size][Maze_size];
    for(int i=0; i<length; i++)
        for(int j=0; j<wide; j++)
            newmap[i][j]=Maze_map[i][j];
    for(int i=2; i<=top; i++)
        newmap[stack[i][0]][stack[i][1]]='*';
    cout<<"DFS:"<<endl;
    for(int i=0; i<length; i++)
        for(int j=0; j<wide; j++)
        {
            cout<<newmap[i][j];
            if(j==wide-1)
                cout<<endl;
        }
    cout<<endl;
}

bool Maze::BFS()
{
    base=0,qtop=1;
    memset(Maze_map_bj,0,sizeof(Maze_map_bj));
    queue[base][0]=startx,queue[base][1]=starty,queue[base][2]=0;
    Maze_map_bj[startx][starty]=1;
    while(base!=qtop)
    {
        int x=queue[base][0],y=queue[base][1];
        for(int i=0; i<4; i++)
        {
            if(Maze_map[x+step[i][0]][y+step[i][1]]=='E')
                return 1;
            if(Maze_map[x+step[i][0]][y+step[i][1]]=='.'&&!Maze_map_bj[x+step[i][0]][y+step[i][1]])
            {
                queue[qtop][0]=x+step[i][0],queue[qtop][1]=y+step[i][1];
                queue[qtop][2]=base;
                qtop++;
                Maze_map_bj[x+step[i][0]][y+step[i][1]]=1;
            }
        }
        base++;
    }
    return 0;
}

void Maze::outputBFSmap()
{
    if(!BFS())
    {
        cout<<"maze bu neng zou = = "<<endl;
        return ;
    }
    char newmap[Maze_size][Maze_size];
    for(int i=0; i<length; i++)
        for(int j=0; j<wide; j++)
            newmap[i][j]=Maze_map[i][j];
    while(base!=0)
    {
        newmap[queue[base][0]][queue[base][1]]='*';
        base=queue[base][2];
    }
    cout<<"BFS:"<<endl;
    for(int i=0; i<length; i++)
        for(int j=0; j<wide; j++)
        {
            cout<<newmap[i][j];
            if(j==wide-1)
                cout<<endl;
        }
    cout<<endl;
}
int main()
{
    Maze a;
    a.input();
    a.outputBFSmap();
    a.outputDFSmap();
    return 0;
}


 

目录
相关文章
|
11天前
|
消息中间件 存储 搜索推荐
深入理解栈和队列(二):队列
深入理解栈和队列(二):队列
29 0
|
16天前
|
存储 消息中间件 NoSQL
Redis数据类型详解:选择合适的数据结构优化你的应用
Redis数据类型详解:选择合适的数据结构优化你的应用
|
12天前
|
存储 算法 索引
【算法与数据结构】队列的实现详解
【算法与数据结构】队列的实现详解
|
16天前
|
算法 C语言
【算法与数据结构】 C语言实现单链表队列详解2
【算法与数据结构】 C语言实现单链表队列详解
|
16天前
|
存储 算法 C语言
【算法与数据结构】 C语言实现单链表队列详解1
【算法与数据结构】 C语言实现单链表队列详解
|
16天前
|
存储 算法 编译器
【数据结构】栈算法(算法原理+源码)
【数据结构】栈算法(算法原理+源码)
【数据结构】栈算法(算法原理+源码)
|
20天前
|
存储
【数据结构】什么是栈?
【数据结构】什么是栈?
24 0
【数据结构】什么是栈?
|
20天前
|
存储 编译器 C语言
【数据结构】深入浅出理解链表中二级指针的应用
【数据结构】深入浅出理解链表中二级指针的应用
27 0
|
24天前
|
存储 设计模式 算法
【C/C++ 数据结构 线性表】深入理解与实现栈:从基础到应用的全面探索
【C/C++ 数据结构 线性表】深入理解与实现栈:从基础到应用的全面探索
52 0
|
24天前
|
算法 搜索推荐
数据结构第十二弹---堆的应用
数据结构第十二弹---堆的应用

热门文章

最新文章