经典迷宫问题

经典的迷宫问题,分别用了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找出来的是最短路径。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值