c++ 珊格迷宫问题

#demo1
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<queue>
#include<cstdio>
using namespace std;
//生成迷宫
const int HEIGHT = 10;
const int WIDTH = 10;
bool isFound = false;
int maze[HEIGHT][WIDTH];
void initialMaze()
{

    maze[0][0] = 0;//入口
    maze[HEIGHT - 1][WIDTH - 1] = 0;//出口
    for (int i = 0; i < HEIGHT; i++)//用随机数0,1填充迷宫
    {
        for (int j = 0; j < WIDTH; j++)
        {
            if (i == 0 && j == 0)
                continue;
            if (i == HEIGHT - 1 && j == WIDTH - 1)
                continue;
            maze[i][j] = rand() % 2;
        }
    }

    //展示生成的迷宫
    for (int i = 0; i < HEIGHT; i++)
    {
        for (int j = 0; j < WIDTH; j++)
        {
            cout << maze[i][j];
            if (j != WIDTH - 1)
            {
                cout << " ";
            }
            else
            {
                cout << endl;
            }
        }
    }
}
//生成方向
int directory[8][2] = { {0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1} };
//判断是否越界
bool isLeap(int x, int y)
{
    return x >= 0 && x < WIDTH&&y >= 0 && y < HEIGHT;

}
//任意位置的结构体
struct point {
    int x;
    int y;
};
//声明用于存储路径的结构体
struct dir
{
    int x;
    int y;
    int d;
};
//声明用于存储路径的队列
queue<dir> directoryQueue;
//迷宫循迹
dir path[HEIGHT][WIDTH];//记录迷宫的路径
int output[HEIGHT*WIDTH][3];
void mazeTravel(point start, point end, int maze[HEIGHT][WIDTH], int directory[8][2])
{
    dir element;
    //dir tmp;
    int i;
    int j;
    int d;
    int a;
    int b;
    element.x = start.x;
    element.y = start.y;
    element.d = -1;
    maze[start.x][start.y] = 2;
    directoryQueue.push(element);
    while (!directoryQueue.empty())
    {
        element = directoryQueue.front();
        dir m = element;
        directoryQueue.pop();
        i = element.x;
        j = element.y;
        d = element.d + 1;

        while (d < 8)
        {
            a = i + directory[d][0];
            b = j + directory[d][1];
            if (a == end.x&&b == end.y&&maze[a][b] == 0)
            {
                //储存前一个点的信息至path
                dir temp = m;
                temp.d = d;
                path[a][b] = temp;

                isFound = true;
                return;
            }
            if (isLeap(a, b)&&maze[a][b]==0)
            {
                //储存前一个点的信息至path
                dir temp = m;
                temp.d = d;
                path[a][b] = temp;

                maze[a][b] = 2;
                element.x = a;
                element.y = b;
                element.d = -1;
                directoryQueue.push(element);
            }
            d++;
        }
    }
}
void printPath(point start, point end)
{
    if (!isFound)
        printf("The path is not found");
    else
    {
        int step = 0;
        dir q;
        q.x = end.x;
        q.y = end.y;
        q.d = 886;
        while (q.x != start.x || q.y != start.y)
        {
            output[step][0] = q.x;
            output[step][1] = q.y;
            output[step][2] = q.d;
            int x = q.x;
            int y = q.y;
            q.x = path[q.x][q.y].x;
            q.y = path[x][q.y].y;
            q.d = path[x][y].d;
            step++;
        }
        output[step][0] = q.x;
        output[step][1] = q.y;
        output[step][2] = q.d;
        printf("The path is as follows: \n");
        for (int i = step; i >= 0; i--)
        {
            printf("(%d,%d)", output[i][0], output[i][1]);
            if (i != 0)
                printf("->");
        }
        printf("\n");
    }
}
int main()
{
    srand(time(0));
    initialMaze();
    point a, b;
    a.x = 0;
    a.y = 0;
    b.x = HEIGHT - 1;
    b.y = WIDTH - 1;
    mazeTravel(a, b, maze, directory);
    printPath(a, b);
    return 0;
}

输出

0 1 1 0 1 0 0 1 1 0
0 0 0 1 0 1 1 1 0 1
0 1 1 0 1 0 1 0 0 0
0 1 1 0 0 0 0 1 0 1
1 0 1 1 1 0 0 1 0 0
0 1 1 0 0 0 0 1 1 0
0 1 0 1 0 0 0 1 0 1
0 1 0 0 1 0 0 0 0 1
1 0 1 0 1 0 1 0 0 0
0 0 1 0 1 1 1 1 0 0
The path is as follows:
(0,0)->(1,1)->(1,2)->(2,3)->(3,4)->(4,5)->(5,6)->(6,6)->(7,7)->(8,8)->(9,9)
Program ended with exit code: 0

demo2

#demo2
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
using std::vector;
struct point
{
    int x;
    int y;
    int step;
    point(int _x, int _y, int _step) :x(_x), y(_y), step(_step) {}
    point(int _x, int _y) :x(_x), y(_y), step(0){}
    point(){}
    bool operator==(const point& other)const
    {
        return x == other.x&&y == other.y;
    }
};
int minSteps_BFS(const vector<vector<int>>& path, vector<vector<point>>& mp, point src, point des, int step);
int main()
{
    vector<vector<int>> path = { { 1, 1, 1, 1, 1 }, { 1, 0, 1, 0, 1 }, { 1, 0, 0, 1, 1 }, { 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1 } };
    vector<vector<point>> mp(5, vector<point>(5));
    point src(0,0);
    point des(3,3);
    int step = 0;
    cout << minSteps_BFS(path, mp, src, des, step) << endl;
    cout << "具体路径如下:" << endl;
    //vector<point> res;
    while (!(mp[des.x][des.y] == src))
    {
        cout << des.x << " " << des.y << endl;
        des = mp[des.x][des.y];
    }
    cout << des.x << " " << des.y << endl;
    return 0;
}

int minSteps_BFS(const vector<vector<int>>& path, vector<vector<point>>& mp, point src, point des, int step)
{
    const unsigned long n = path.size();
    const unsigned long m = path[0].size();
    const int dx[4] = { 0, 0, -1, 1 };
    const int dy[4] = { 1, -1, 0, 0 };
    vector<vector<bool>> flag(n, vector<bool>(m, false));
    flag[src.x][src.y] = true;
    queue<point> que;
    que.push(src);
    while (!que.empty())
    {
        point p = que.front();
        for (int i = 0; i < 4; ++i)
        {
            if (p.x + dx[i] < 0 || p.x + dx[i] >= n || p.y + dy[i] < 0 || p.y + dy[i] >= m)
                continue;
            if (path[p.x + dx[i]][p.y + dy[i]] == 1 && !flag[p.x + dx[i]][p.y + dy[i]])
            {
                flag[p.x + dx[i]][p.y + dy[i]] = true;
                que.push(point(p.x + dx[i], p.y + dy[i], p.step + 1));
                mp[p.x + dx[i]][p.y + dy[i]]= p;
                if (point(p.x + dx[i], p.y + dy[i], p.step + 1) == des)
                {
                    return p.step + 1;
                }
            }
        }
        que.pop();
    }
    return -1;
}

输出

6
具体路径如下:
3 3
3 2
3 1
3 0
2 0
1 0
Program ended with exit code: 0

参考:
https://www.cnblogs.com/xiugeng/p/9687354.html
https://blog.csdn.net/weixin_41106545/article/details/83211418

原文地址:https://www.cnblogs.com/sea-stream/p/11100374.html

时间: 2024-08-29 09:13:30

c++ 珊格迷宫问题的相关文章

珊格地图笔记

写在前面:这篇文章是Coursera上的课程(Robotics: Estimation and Learning),权当笔记,激光传感器的数据可以在课程内下载.这一周的内容比较简单,但十分实用. 在这片文章中,我们将会介绍: 机器人世界的几种地图: 占据栅格地图的表示方法与更新方法: 利用激光传感器数据构建占据栅格地图. 1. 机器人地图的分类 地图有很多种表示方式,例如,用经纬度标识地方的世界地图,城市的地铁图,校园指引图. 第一种我们称为尺度地图(Metric Map),每一个地点都可以用坐

c++ 珊格画椭圆

#ifndef _TEST_H #define _TEST_H #include <iostream> #include <math.h> using namespace std; int main() { const double pi=3.14159,p=16.0; //圆周率和圆的半径 double angle; //角度 int x,y; //计算存储数组的坐标 char rose[25][80]; //模拟屏幕为25*80个像素构成 for (x=0;x<80;x+

洛谷——P1141 01迷宫

https://www.luogu.org/problem/show?pid=1141 题目描述 有一个仅由数字0与1组成的n×n格迷宫.若你位于一格0上,那么你可以移动到相邻4格中的某一格1上,同样若你位于一格1上,那么你可以移动到相邻4格中的某一格0上. 你的任务是:对于给定的迷宫,询问从某一格开始能移动到多少个格子(包含自身). 输入输出格式 输入格式: 输入的第1行为两个正整数n,m. 下面n行,每行n个字符,字符只可能是0或者1,字符之间没有空格. 接下来m行,每行2个用空格分隔的正整

noip 01迷宫(BFS+记忆化)

题目链接:https://www.luogu.org/problem/show?pid=1141 题意:给出一个仅由数字0与1组成的n×n格迷宫.放0的那一格可以4个方向走到放1那一格,1也是四个方向走到放0的那一格.算上本身的那格.求最多能移动多少格子. 数据比较大,如果直接用bfs搜的话会暴时.所以需要每次搜索完都记录一下. 1 #include <iostream> 2 #include <algorithm> 3 #include <queue> 4 using

P1141 01迷宫

题目描述 有一个仅由数字0与1组成的n×n格迷宫.若你位于一格0上,那么你可以移动到相邻4格中的某一格1上,同样若你位于一格1上,那么你可以移动到相邻4格中的某一格0上. 你的任务是:对于给定的迷宫,询问从某一格开始能移动到多少个格子(包含自身). 输入输出格式 输入格式: 输入的第1行为两个正整数n,m. 下面n行,每行n个字符,字符只可能是0或者1,字符之间没有空格. 接下来m行,每行2个用空格分隔的正整数i,j,对应了迷宫中第i行第j列的一个格子,询问从这一格开始能移动到多少格. 输出格式

luogu cogs P1141 01迷宫

题目描述 有一个仅由数字0与1组成的n×n格迷宫.若你位于一格0上,那么你可以移动到相邻4格中的某一格1上,同样若你位于一格1上,那么你可以移动到相邻4格中的某一格0上. 你的任务是:对于给定的迷宫,询问从某一格开始能移动到多少个格子(包含自身). 输入输出格式 输入格式: 输入的第1行为两个正整数n,m. 下面n行,每行n个字符,字符只可能是0或者1,字符之间没有空格. 接下来m行,每行2个用空格分隔的正整数i,j,对应了迷宫中第i行第j列的一个格子,询问从这一格开始能移动到多少格. 输出格式

luogu P1141 01迷宫 x

P1141 01迷宫 题目描述 有一个仅由数字0与1组成的n×n格迷宫.若你位于一格0上,那么你可以移动到相邻4格中的某一格1上,同样若你位于一格1上,那么你可以移动到相邻4格中的某一格0上. 你的任务是:对于给定的迷宫,询问从某一格开始能移动到多少个格子(包含自身). 输入输出格式 输入格式: 输入的第1行为两个正整数n,m. 下面n行,每行n个字符,字符只可能是0或者1,字符之间没有空格. 接下来m行,每行2个用空格分隔的正整数i,j,对应了迷宫中第i行第j列的一个格子,询问从这一格开始能移

洛谷OJ P1141 01迷宫 解题报告

洛谷OJ P1141 01迷宫 解题报告 by MedalPluS [题目描述]    有一个仅由数字0与1组成的n×n格迷宫.若你位于一格0上,那么你可以移动到相邻4格中的某一格1上,同样若你位于一格1上,那么你可以移动到相邻4格中的某一格0上.你的任务是:对于给定的迷宫,询问从某一格开始能移动到多少个格子(包含自身).   [输入描述]   输入的第1行为两个正整数n,m.  下面n行,每行n个字符,字符只可能是0或者1,字符之间没有空格.  接下来m行,每行2个用空格分隔的正整数i,j,对

1629 01迷宫

1629 01迷宫 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 有一个由01组成的n*n格迷宫,若你位于一格0上,那么你可以移动到相邻4格中的某一格1上,同样若你位于一格1上,那么你可以移动到相邻4格中的某一格0上.那么对于给定的迷宫,询问从某一格开始能移动到多少格. 输入描述 Input Description 输入的第1行为两个正整数n,m. 下面n行,每行n个字符,字符只可能是0或者1,字符之