三种算法实现迷宫自动寻路(C++实现 鼠标点击 含源码)

迷宫实现:随机prime算法  生成随机迷宫

 三种寻路算法:
                                深度遍历

                                广度遍历

                                A*算法

 源代码:

#include <iostream>
#include <vector>
#include <conio.h>
#include<graphics.h>
#include<stdio.h>
#include <windows.h>
#include <time.h>
#include <string>
using namespace std;
#pragma warning(disable:4996) //这一行是为了能在 Visual Studio 2022内使用getch()函数
#define BLANKSTR  "·"//就是个全角的点
#define DEADSTR   "X"//全角的大写的英文字母x
int x_num = 1, y_num = 1;//矿工位置
int G[35][35];
#define m 33//row
#define n 33
#define down 3
#define right 2
#define left 4
#define up 8
#define WALL 1
#define NOTHING 0
#define DED 5
#define UPSTR     "∧"
#define DOWNSTR   "∨"
#define LEFTSTR   "<"
#define RIGHTSTR  ">"
//竖着 Y 行数
#define rows 35
//横着 X 列数
#define cols 35
//直线代价10
#define ZXDJ 10
//斜线代价14
#define XXDJ 14
int sj = 0;
//点的类型
struct button
{
    int x;
    int y;
    int width;
    int height;
    COLORREF color;
    char* pText;
};//初始按钮属性
struct button* createButton(int x, int y, int width, int height, COLORREF color, const char* pText)
{
    struct button* pB = (struct button*)malloc(sizeof(struct button));
    pB->x = x;
    pB->y = y;
    pB->width = width;
    pB->height = height;
    pB->color = color;
    pB->pText = (char*)malloc(strlen(pText) + 1);
    strcpy(pB->pText, pText);
    return pB;
}//画按钮

void drawButton(struct button* pB)
{
    setfillcolor(pB->color);
    settextstyle(35, 0, "楷体");
    setlinecolor(BLACK);
    setbkmode(TRANSPARENT);
    fillrectangle(pB->x, pB->y, pB->x + pB->width, pB->y + pB->height);
    outtextxy(pB->x + 5, pB->y + 10, pB->pText);
}
int mouseInButton(struct button* pB, MOUSEMSG mn)
{
    if (pB->x <= mn.x && mn.x <= pB->x + pB->width && pB->y <= mn.y && mn.y <= pB->height + pB->y)
    {
        pB->color = BLUE;
        return 1;
    }
    pB->color = YELLOW;
    return 0;

}

struct MyPoint {
    int row;
    int col;
    int f, g, h;
    void setF() {
        f = g + h;
    }
};
struct point {
    int x;
    int y;
}start, end;
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
void hide() {  //隐藏光标
    CONSOLE_CURSOR_INFO cursor;
    cursor.bVisible = 0;
    cursor.dwSize = 1;
    //1-100
    SetConsoleCursorInfo(hout, &cursor);
}
void SetPosition(int line, int col)//更改光标的位置
{
    static COORD coord;
    coord.X = col * 2;
    coord.Y = line;
    SetConsoleCursorPosition(hout, coord);
}
struct block {
    int row, column, direction;
    block(int _row, int _column, int _direction) {
        row = _row;
        column = _column;
        direction = _direction;
    }
};
struct QUEUE
{
    int x, y;
    int PreSub;
};
point Stack[2500] = { {0,0} };
vector<block> myblock;
QUEUE Queue[2500] = { {0,0,0} };
int head = -1, tail = -1, top = -1;


void jianmian();
void init() {
    //将地图全部置为墙
    //memset(G, WALL, sizeof(G));
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++)
            G[i][j] = 1;
    }
    //定义起始点
    G[1][1] = NOTHING;
    start.x = start.y = 1;
}
void FindBlock() {
    //找出与当前位置相邻的墙
    if (x_num + 1 <= m && G[x_num + 1][y_num] == WALL) {//down
        myblock.push_back(block(x_num + 1, y_num, down));
    }
    if (y_num + 1 <= n && G[x_num][y_num + 1] == WALL) {//right
        myblock.push_back(block(x_num, y_num + 1, right));
    }
    if (x_num - 1 >= 1 && G[x_num - 1][y_num] == WALL) {//up
        myblock.push_back(block(x_num - 1,

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值