编制一个求解迷宫通路的图形界面演示程序。
*问题描述:
1) 输入一个任意大小的迷宫,任设起点、终点、障碍,用栈求出一条走 出迷宫的路径,并显示在屏幕上。
2) 根据用户界面提示,用键盘输入。Home 键设置迷宫起点,End 键设终 点,上下左右箭头键移动,Enter 键添加墙,Del 键删除墙,完成后按 F9 键演示,Esc 键退出。
3)橙色的实心小圆圈表示起点,绿色实心圆圈表示终点,空心圆圈表示 足迹,红色方块表示墙。
4)本程序只求出一条成功的通路,但若对求解函数 MazePath 稍加更改即 可求得全部路径。此外,因受图形界面限制,不能保存或载入测试文件(此 功能可在 Maze_text 中实现)。
5)当未输入起点时,消息显示“Error: You must set Startplace.”; 未输入终点时,显示“Error: You must set Endplace.” 找到路径时, 屏幕显示足迹,并在消息框出现 Path found,否则消去足迹,显示 Path not found.
演示视频:https://www.bilibili.com/video/BV16W411L7wG/
#include <iostream>
#include <graphics.h> //EasyX图形库
#include <conio.h>
#include <windows.h>
#include <stack>
#include <time.h>
#define Place 10
#define S 30
using namespace std;
struct Node
{
int row;
int col;
};
int W = 10;
int map[2+15][2+15] = {
{ 1,1,1,1,1,1,1,1,1,1,1,1 },
{ 1,0,0,0,0,0,1,0,1,0,0,1 },
{ 1,1,1,1,1,0,1,0,1,0,1,1 },
{ 1,0,0,0,0,0,1,0,0,0,0,1 },
{ 1,0,1,1,0,0,1,1,1,1,0,1 },
{ 1,0,0,1,0,1,1,0,0,1,0,1 },
{ 1,0,1,1,0,0,0,1,0,0,0,1 },
{ 1,0,1,0,0,1,0,1,0,0,0,1 },
{ 1,0,1,0,1,0,0,1,0,1,0,1 },
{ 1,0,0,1,1,1,0,0,0,1,0,1 },
{ 1,0,0,1,0,0,0,1,0,1,0,1 },
{ 1,1,1,1,1,1,1,1,1,1,1,1 }
};
int startX = 16, startY = 16;
int endX = 16, endY = 16;
int wight = (W + 2) * S + Place, height = (W + 2) * S + Place;
int wi;
bool MapSet(int x); // 地图大小设置函数
void DrawMap(int w); // 绘制地图
void MouseSet(MOUSEMSG msg); // 获取鼠标事件
void MoveFouse(int x, int y); //移动光标
void SetStart(); // 设置起点
void SetEnd(); // 设置终点
bool CheckMap(); // 检查是否有起点 终点
bool CheckPath(int *a, Node next, int n); // 寻路过程中检查是否可行
bool MazePath(int *a, int n, Node start, stack<Node>& path); // 用栈 迷宫寻路
void Path(); // 开始寻路
void DrawPath(stack<Node> path); // 寻找到通路后利用延时动态画出路径
void SetMouseClick(MOUSEMSG msg); // 处理点击事件
void KeyBoardClick(); // 按键事件
void SetMapCousor(); //回车设置当前障碍
void Tips(); //设置提示信息
int main()
{
do
{
TCHAR w[10];
bool in_judge = InputBox(w, 10, "请输入迷宫宽度 (5-15) ,障碍需要自己设置\n\n\n取消(Esc)则取默认值 (10) ,并已有演示迷宫", "迷宫大小预设置", "", 340, 0, false);
sscanf(w, "%d", &wi);//转int
if (!in_judge)
{
wi = -1;
startX = 1, startY = 2;
endX = 2, endY = 9;
map[startX][startY] = 2;
map[endX][en