迷宫实现:随机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, y_num, up));
}
if (y_num - 1 >= 1 && G[x_num][y_num - 1] == WALL) {//left
myblock.push_back(block(x_num, y_num - 1, left