#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int flag = 0;
int mazeGenerator(char **maze,int n,int m);
void mazeTraverse(char **maze,int n,int m,int emy, int y,int x=0);
void printMaze(char **maze,int n,int m);
int* isAccessible(char **maze,int x,int y ,int n,int m);//判断下一步是否有通路若有返回下一个位置
bool isEntry(char **maze, int emy,int x,int y);//判断当前坐标是否是入口坐标
int main(){
int n, m, emy;
cout << "请输入迷宫的行数N(N>2):";
cin >> n;
cout << "请输入迷宫的列数M(M>0):";
cin >> m;
char **maze = new char *[n];
for (int i = 0; i < n; i++){
maze[i] = new char[m];
}
emy = mazeGenerator(maze, n, m);
mazeTraverse(maze,n,m,emy, emy);
flag = 0;
system("pause");
}
int mazeGenerator(char **maze, int n, int m){
char mazeEle[2];
mazeEle[0] = '#';
mazeEle[1] = '.';
srand(int (time(0)));
//随机生成部分的迷宫
for (int i = 1; i < n-1; i++){
for (int j = 0; j < m-1; j++){
if (j == 0)
maze[i][j] = '#';
else{
maze[i][j] = mazeEle[rand() % 2];
}
}
}
//生成第一行和最后一行的迷宫
for (int j = 0; j < m; j++){
maze[0][j] = '#';
maze[n - 1][j] = '#';
}
//生成最后一列的迷宫
for (int i = 1; i < n - 1; i++){
maze[i][m - 1] = mazeEle[rand() % 2];
}
//确保至少有一个不同于入口的出口
int exy;//出口的y轴坐标
do{
maze[0][m - 1] = '#';
maze[n - 1][m - 1] = '#';
exy = rand() % n;
maze[exy][m - 1] = '.';
} while (exy==0||exy==n-1);
//初始化入口位置
int emy;//入口的y轴坐标
do{
maze[0][0] = '#';
maze[n - 1][0] = '#';
emy = rand() % n;
maze[emy][0] = 'X';
} while (emy == 0 || emy == n - 1);
printMaze(maze,n,m);
return emy;
}
void printMaze(char **maze,int n,int m){
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
cout << maze[i][j];
}
cout << endl;
}
system("pause");
}
void mazeTraverse(char **maze, int n, int m, int emy, int y, int x){
if (((x == 0) && (y != emy)) || (x == m - 1)){
cout << "成功走出迷宫" << endl;
flag = 1;
return;
}
if (flag != 1 && isAccessible(maze, x, y, n, m)){
int x1, y1;
int *t = isAccessible(maze, x, y, n, m);
x1 = *t; y1 = *(t + 1);
maze[y1][x1] = 'X';
printMaze(maze, n, m);
mazeTraverse(maze, n, m, emy, y1, x1);
if (!isEntry(maze, emy, x, y))
mazeTraverse(maze, n, m, emy, y, x);
}
if (flag != 1 && isEntry(maze, emy, x, y)){
printMaze(maze, n, m);
cout << "回退到入口" << endl;
return;
}
}
int* isAccessible(char **maze, int x, int y, int n, int m){
int nextPos[2];
if ((y<(n - 1)) && (maze[y+1][x] == '.')){//下
nextPos[0] = x;
nextPos[1] = y + 1;
return nextPos;
}
if ((x<(m - 1)) && (maze[y][x + 1] == '.')){//右
nextPos[0] = x + 1;
nextPos[1] = y;
return nextPos;
}
if ((y>0)&&(maze[y - 1][x] == '.')){//上
nextPos[0] = x;
nextPos[1] = y - 1;
return nextPos;
}
if ((x>0) && (maze[y][x - 1] == '.')){//左
nextPos[0] = x - 1;
nextPos[1] = y;
return nextPos;
}
return NULL;//无通路
}
bool isEntry(char **maze, int emy,int x,int y){
if (x == 0 && y == emy)
return true;
else return false;
}
迷宫游戏C++代码
1星 需积分: 26 87 浏览量
2018-08-06
16:16:06
上传
评论 7
收藏 18KB ZIP 举报
Innse
- 粉丝: 3
- 资源: 2
最新资源
- MyBatis 动态 SQL:灵活而强大的查询构建器.pdf
- com.accordion.prettyo.apk
- 毕业设计:基于SSM的mysql-ssm软件bug管理系统(源码 + 数据库 + 说明文档)
- MTSQL8.0.35windows(64bit)-mysql-installer-community-8.0.35.0
- 人工智能引领音乐创作新时代之Suno AI
- Public-bicycle-usage-forecast-master.zip
- 通道处理过程模拟:从理论到实践.pdf
- 数据库第七次作业E-R图第一题
- 大厂面试真题Java语法基础面试专题及答案
- IMG20240428211124.jpg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈