本文实例为大家分享了C++实现迷宫游戏的具体代码,供大家参考,具体内容如下
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
//文件的输入,有墙
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<fstream>
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<top;i++)
{
cout<<"("<<path[i].x<<","<<path[i].y<<")"<<"--->";
}
cout<<"("<<path[i].x<<","<<path[i].y<<")"<<endl;
outfile<<"该迷宫的路径为:"<<endl; //文件的输出
for(i=0;i<top;i++)
{
outfile<<"("<<path[i].x<<","<<path[i].y<<")"<<"--->";
}
outfile<<"("<<path[i].x<<","<<path[i].y<<")"<<endl;
}
};
Stack stack; //不可以放在栈的定义前面
bool Test_value() //检验每个节点是否是否被访问过,全访问过了则为true,false为没有全部访问过
{
int i,j;
bool t=true;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(value[i][j]!=false)
{
t=false;
break;
}
return t;
}
int Judge()
{
Point t;
int frage=0; //先规定先向右
while(frage==0)
{
t=stack.Pop(); //返回栈顶元素
t.y=t.y+1; //向右
stack.Push(t);
if(stack.Y_N_Push()==1) //不符合要求的节点
{
stack.Delete_Pop();
t=stack.Pop();
t.x=t.x+1; //向下
stack.Push(t);
if(stack.Y_N_Push()==1)
{
stack.Delete_Pop();
t=stack.Pop();
t.y=t.y-1; //向左
stack.Push(t);
if(stack.Y_N_Push()==1)
{
stack.Delete_Pop();
t=stack.Pop();
t.x=t.x-1; //向上
stack.Push(t);
if(stack.Y_N_Push()==1)
{
frage=4; //这个点4个方向都不成立
stack.Delete_Pop();
}
}
}
}
if(stack.Y_N_Push()==2)
{
frage=5; //找到了出口并跳出
break;
}
}
return frage;
}
void Search()
{
stack.Clear();
Point u;
u.x=1;
u.y=1;
stack.Push(u);
value[1][1]=false;
while(true)
{
int t=Judge();
if(t==5) //找到了路
{
cout<<"找到了能通的路,路径为(用那个点的坐标表示):"<<endl;
stack.Output();
break;
}
if(t==4)stack.Delete_Pop(); //退回一个点继续找
if((Test_value()==true)||(stack.top==-1)) //很重要的条件,stack.top==-1,否则的话当有个点四周都不能通过的话,而且前面的点也不能通的话,就没有通路
{
cout<<"没有通路!"<<endl;
outfile<<"没有通路!"<<endl;
break;
}
}
}
int main()
{
int i,j;
cout<<"请输入迷宫的长和宽:"; //二维数组从maze[0][0]开始
cin>>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为可通路(规定左上角为入口,右下角为出口):"<<endl;
srand(unsigned(time(NULL)));
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
maze[i][j]=(rand()%2);
maze[1][1]=maze[n][m]=0;
cout<<"输入的迷宫(带有墙)是:"<<endl;
for(i=0;i<=(n+1);i++)
{
for(j=0;j<=(m+1);j++)
cout<<maze[i][j]<<" ";
cout<<endl;
}
if(!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
outfile<<"生成"<<n<<"行"<<m<<"列"<<"的迷宫为:"<<endl;
for(i=0;i<=(n+1);i++)
{
for(j=0;j<=(m+1);j++)
outfile<<maze[i][j]<<" ";
outfile<<endl;
}
for(i=1;i<=n;i++) //记录从value[0][0]开始
for(j=1;j<=m;j++)
value[i][j]=true;
Search();
return 0;
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/jpday/article/details/8647915








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。