经典迷宫问题

经典的迷宫问题,分别用了BFS与DFS解决。

#include<iostream>
#include<queue> 
using namespace std;
/* 6 8 0 0 1 1 0 1 1 1 1 0 1 0 1 0 1 0 0 0 0 0 0 1 1 1 0 1 1 1 0 0 1 1 1 0 0 1 1 0 0 0 0 1 1 0 0 1 1 0 */
struct point{
    int x;
    int y;
};
int **maze, width, height;
point **pre;
int next2[4][2] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };
//输入迷宫
void input(){
    cout << "请输入迷宫的高和宽" << endl;
    cin >> height >> width;     //10,10

    maze = new int *[height + 2];
    pre = new point *[height + 2];
    for (int i = 0; i <= height + 1; i++)
    {
        maze[i] = new int[width + 2];
        pre[i] = new point[width + 2];
    }

    for (int i = 0; i <= height + 1; i++)
        maze[i][0] = maze[i][width + 1] = 1;
    for (int j = 0; j <= width + 1; j++)
        maze[0][j] = maze[height + 1][j] = 1;
    for (int i = 1; i < height + 1; i++){
        for (int j = 1; j < width + 1; j++)
            cin >> maze[i][j];
    }
    for (int i = 0; i <= height + 1; i++){
        for (int j = 0; j <= width + 1; j++){
            cout << maze[i][j];
            pre[i][j].x = pre[i][j].y = 0;
        }
        cout << endl;
    }
}

bool BFS(point start, point end){
    if (start.x == end.x && start.y == end.y){
        return true;
    }

    queue<point> queue;
    point now;

    queue.push(start);
    maze[start.x][start.y] = -1;

    while (!queue.empty())
    {
        now = queue.front();
        queue.pop();
        for (int i = 0; i < 4; i++){
            point move;
            move.x = now.x + next2[i][0];
            move.y = now.y + next2[i][1];

            if (move.x == end.x&&move.y == end.y) {
                pre[end.x][end.y].x = now.x;
                pre[end.x][end.y].y = now.y;
                return true;
            }
            if (maze[move.x][move.y] == 0){
                queue.push(move);

                maze[move.x][move.y] = 1;
                pre[move.x][move.y].x = now.x;
                pre[move.x][move.y].y = now.y;
            }
        }
    }
    return false;
}

bool DFS(point start, point end){
    if (start.x == end.x && start.y == end.y){
        return true;
    }

    point now;
    point *queue = new point[100];
    int count = 0;

    queue[count++] = start;
    maze[start.x][start.y] = -1;

    while (count)
    {
        now = queue[--count];
        for (int i = 0; i < 4; i++){
            point move;
            move.x = now.x + next2[i][0];
            move.y = now.y + next2[i][1];

            if (move.x == end.x&&move.y == end.y) {
                pre[end.x][end.y].x = now.x;
                pre[end.x][end.y].y = now.y;
                return true;
            }
            if (maze[move.x][move.y] == 0){

                queue[count++] = move;
                maze[move.x][move.y] = 1;
                pre[move.x][move.y].x = now.x;
                pre[move.x][move.y].y = now.y;
            }
        }
    }
    return false;
}
//显示迷宫的路径
void showMaze(){
    point now = { height, width };
    point endPoint = { 0, 0 };
    while (now.x != endPoint.x && now.y != endPoint.y)
    {
        cout << "(" << now.x << "," << now.y << ") ";
        now = pre[now.x][now.y];
    }
}

int main(){
    //输入迷宫
    input();

    point start;
    start.x = 1;
    start.y = 1;
    point end;
    end.x = height;
    end.y = width;

    //if (BDF(start, end))
    if (DFS(start, end))
    {
        for (int i = 0; i <= height + 1; i++){
            for (int j = 0; j <= width + 1; j++){
                cout << pre[i][j].x << "," << pre[i][j].y << " ";
            }
            cout << endl;
        }
    }
    showMaze();
}

不过这里的DFS只能找到一条通的路径,并不能确保最短,而BFS找出来的是最短路径。

    原文作者:迷宫问题
    原文地址: https://blog.csdn.net/qq_25673113/article/details/47070589
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞