栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++迷宫的实现代码

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++迷宫的实现代码

本文实例为大家分享了C++实现迷宫游戏的具体代码,供大家参考,具体内容如下

//文件的输入,有墙
#include
#include
#include
#include
using namespace std;
const int max1=100*100;   //加入墙
const int max2=102;
bool value[max2][max2];   //记录是否被访问过
int maze[max2][max2];    //迷宫的大小
int n,m;   //输入迷宫的长和宽
ofstream outfile("path.txt");   //文件保存迷宫及输出的路径
struct Point   //栈中的数据
{
 int x;
 int y;
};
struct Stack
{
 int top;
 Point path[max1];  //存坐标点的数组栈
 stack()
 {
 top=-1;    //栈中从0开始存数据
 }
 bool Empty()  //检验是否为空
 {
 if(top==-1)return true;
 else
  return false;
 }
 void Clear()   //清空栈
 {
 top=-1;
 }
 void Push(Point p)   //进栈
 {
 top++;
 path[top]=p;
 }
 Point Pop()    //返回栈顶元素
 {
 return path[top]; 
 }
 void Delete_Pop()   //删除顶栈元素
 {
 top--;
 }
 int Y_N_Push()
 {
 int x=path[top].x;
 int y=path[top].y;
 if(x<1||y<1||x>n||y>m||!value[x][y]||maze[x][y])    //不符合要求
 {
  value[x][y]=false;   //标记这个点被访问过(不能任意做标记)
  return 1;  
 }
 else
  if((x==n)&&(y==m))   //已经找到出口,不要标记,后面直接跳出
  return 2;
  else
  {
  value[x][y]=false;   //标记这个点被访问过
  return 3;    //可以进栈
  }
 }
 void Output()   //输出栈中的路径
 {
 int i;
 for(i=0;i";
 }
 cout<<"("<";
 }
 outfile<<"("<>n>>m;
 for(i=0;i<=(m+1);i++)
 maze[0][i]=maze[n+1][i]=1;
 for(i=1;i<=(n+1);i++)
 maze[i][0]=maze[i][m+1]=1;
 cout<<"输入迷宫,1为墙,0为可通路(规定左上角为入口,右下角为出口):"<

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/61851.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号