ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

简陋迷宫生成和寻路

2021-10-30 19:30:00  阅读:136  来源: 互联网

标签:map sy sx int 简陋 迷宫 && push 寻路


#include <iostream>
#include<graphics.h>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define W 801
#define S 801
#define X 1
struct point {
    int x, y;
};
void initMap(vector<vector<int>> &map) {
    if (map.size() > 0) {
        for (int i = 0; i < W; i++) {
            for (int j = 0; j < W; j++) {
                if (i % 2 == 0 || j % 2 == 0) map[i][j]=1;
                else map[i][j]=0;
            }
        }
    }
    for (int i = 0; i < W; i++) {
        map.push_back(vector<int>());
        for (int j = 0; j < W; j++) {
            if (i % 2 == 0 || j % 2 == 0) map[i].push_back(1);
            else map[i].push_back(0);
        }
    }
}
void displayMap(vector<vector<int>>& map) {
    for (int i = 0; i < W; i++) {
        map.push_back(vector<int>());
        for (int j = 0; j < W; j++) {
            if (map[i][j] == -1) setfillcolor(WHITE);
            else setfillcolor(BLACK);
            if(map[i][j]==-2)setfillcolor(RGB(0, 255, 0));
            solidrectangle(j * X, i * X, j * X + X, i * X + X);
        }
    }
}

void maze(vector<vector<int>>& map, int x, int y) {
    map[x][y] = -1;
    int dir[4][2] = { {0,1},{0,-1},{1,0},{-1,0} };
    point p = { x,y };
    stack<point> s;
    s.push(p);
    while (!s.empty()) {
        p = s.top();
        s.pop();
        x = p.x;
        y = p.y;
        while (1) {
            bool end = true;
            for (int i = 0; i < 3; i++) {
                end = true;
                int seed;
                vector<int>temp;
                if (y + 2 < W && map[x][y + 2] == 0) temp.push_back(0);
                if (y - 2 > 0 && map[x][y - 2] == 0)temp.push_back(1);
                if (x + 2 < W && map[x + 2][y] == 0)temp.push_back(2);
                if (x - 2 > 0 && map[x - 2][y] == 0)temp.push_back(3);
                if (temp.size() > 0) {
                    seed = temp[rand() % temp.size()];
                }
                else break;
                int dx = dir[seed][0];
                int dy = dir[seed][1];
                int nx = x + 2 * dx;
                int ny = y + 2 * dy;
                if (nx > 0 && ny > 0 && nx < W && ny < W) {
                    if (map[nx][ny] != -1)
                    {
                        end = false;
                        x = nx;
                        y = ny;
                        map[x][y] = -1;
                        map[x - dx][y - dy] = -1;
                        p = { nx,ny };
                        s.push(p);
                        break;
                    }
                }
            }
            //  displayMap(map);
            if (end) break;
        }
    }
}

void findRoad(vector<vector<int>>& map, int sx, int sy, int ox, int oy) {
    map[sx][sy] = -2;
    int dir[4][2] = { {0,1},{0,-1},{1,0},{-1,0} };
    point p = { sx,sy };
    stack<point> s;
    s.push(p);
    s.push(p);
    while (!s.empty()) {
        p = s.top();
        s.pop();
        sx = p.x;
        sy = p.y;
        while (true) {
            bool end = false;
            for (int i = 0; i < 3; i++) {
                vector<int>temp;
                int seed;
                if (sy + 1 < W && map[sx][sy + 1] == -1) temp.push_back(0);
                if (sy - 1 > 0 && map[sx][sy - 1] ==-1)temp.push_back(1);
                if (sx + 1 < W && map[sx + 1][sy] == -1)temp.push_back(2);
                if (sx - 1 > 0 && map[sx - 1][sy] ==-1)temp.push_back(3);
                if (temp.size() > 0) {
                    seed = temp[rand() % temp.size()];
                }else break;
                int dx = dir[seed][0];
                int dy = dir[seed][1];
                int nx = sx + dx;
                int ny = sy + dy;
                if (nx > 0 && ny > 0 && nx < W && ny < W) {
                    if (map[nx][ny] == -1)
                    {
                        end = true;
                        sx = nx;
                        sy = ny;
                        map[sx][sy] = -2;
                        setfillcolor(RGB(0,255,0));
                        solidrectangle(sy * X, sx * X, sy * X + X, sx * X + X);
                        //Sleep(100) 减缓寻路过程,其他对应地方也可以通过此方法延长运行时间。
                        if (sx == ox && sy == oy) return;
                        p = { nx,ny };
                        s.push(p);
                        break;
                    }
                }
            }
            if (!end) break;
        }
    }
}

int main()
{
    vector<vector<int>> map;
    srand(unsigned int(time(0)));
    //#include<graphics.h> 需要安装Easyx的图形库,方便查看地图的生成过程
    initgraph(S, S);
    setbkcolor(WHITE);
    srand(unsigned int(time(0)));
    cleardevice();
    while (1) {
        initMap(map);
        int a = rand() % (W - 2);
        int b = rand() % (W - 2);
        if (a % 2 == 0) a += 1;
        if (b % 2 == 0) b += 1;
        maze(map, a, b);
        displayMap(map);
        findRoad(map, 1, 1, W - 2, W - 2);
        displayMap(map);
        Sleep(1000);
    }
    system("pause");
    closegraph();
}

        这是第二次写这个函数,显然比上一次熟练多了。但是由于学习原因,没有进行优化,问题还是很多很多。假期再继续使用其他方法来实现,优化生成的速度。之前看过一个博主的视频,感觉迷宫的运用还是有很多的潜力,探索过程蛮有趣的。

以下是实际效果

标签:map,sy,sx,int,简陋,迷宫,&&,push,寻路
来源: https://blog.csdn.net/qq_53331986/article/details/121054982

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有