迷宫通路的图形界面演示程序(栈)

编制一个求解迷宫通路的图形界面演示程序。

*问题描述:

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
  • 14
    点赞
  • 95
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
好的,以下是一个简单的 Java 程序,可以通过读取 txt 文件中的迷宫地图,然后寻找迷宫路径: ```java import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class MazeSolver { private static final int WALL = 1; private static final int PATH = 0; private static final int VISITED = 2; private int[][] maze; private int startRow, startCol; private int endRow, endCol; public MazeSolver(String filename) throws IOException { // 从文件中读取迷宫地图 BufferedReader br = new BufferedReader(new FileReader(filename)); String line; int row = 0; while ((line = br.readLine()) != null) { if (maze == null) { maze = new int[line.length()][line.length()]; } for (int col = 0; col < line.length(); col++) { char c = line.charAt(col); if (c == '#') { maze[row][col] = WALL; } else if (c == 'S') { startRow = row; startCol = col; maze[row][col] = PATH; } else if (c == 'E') { endRow = row; endCol = col; maze[row][col] = PATH; } else { maze[row][col] = PATH; } } row++; } br.close(); } public void solve() { if (findPath(startRow, startCol)) { System.out.println("Found path!"); printMaze(); } else { System.out.println("No path found!"); } } private boolean findPath(int row, int col) { if (row < 0 || row >= maze.length || col < 0 || col >= maze[0].length) { return false; } if (maze[row][col] == WALL || maze[row][col] == VISITED) { return false; } if (row == endRow && col == endCol) { return true; } maze[row][col] = VISITED; if (findPath(row - 1, col) || findPath(row + 1, col) || findPath(row, col - 1) || findPath(row, col + 1)) { return true; } maze[row][col] = PATH; return false; } private void printMaze() { for (int row = 0; row < maze.length; row++) { for (int col = 0; col < maze[0].length; col++) { if (maze[row][col] == WALL) { System.out.print("#"); } else if (maze[row][col] == PATH) { System.out.print("."); } else if (maze[row][col] == VISITED) { System.out.print("X"); } } System.out.println(); } } public static void main(String[] args) { try { MazeSolver solver = new MazeSolver("maze.txt"); solver.solve(); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的程序中,我们通过 `BufferedReader` 读取了一个 txt 文件(文件名为 `maze.txt`),然后将其转换成一个二维数组 `maze` 表示迷宫地图。在地图中,`#` 表示墙壁,`.` 表示通路,`S` 表示起点,`E` 表示终点。然后我们使用递归函数 `findPath` 来寻找迷宫路径。如果递归到了终点,返回 `true`,表示找到了路径;否则返回 `false`。在递归过程中,我们用 `maze` 数组来记录哪些格子是墙壁,哪些格子已经走过,哪些格子是通路。 最后,我们在 `main` 函数中创建了一个 `MazeSolver` 对象,并调用其 `solve` 方法来解决迷宫问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值