迷宫
来源:互联网 发布:易语言json使用方法 编辑:程序博客网 时间:2024/04/28 05:29
#include<iostream>
#include<stack>
#include<malloc.h>
using namespace std;
struct SHAGUO
...{
int x;
int y;
};
int **array;
int outx=0,outy=0;//出口坐标
stack<SHAGUO>shaguo;
void Creat(int sum)
...{
int i,j;
array=(int **)malloc((sum+2)*sizeof(int *));
for( i=0;i<=sum+1;i++)
array[i]=(int *)malloc((sum+2)*sizeof(int));
i=0;/**//*下面把迷宫四周设成墙 ,0 代表墙,1代表路*/
for(j=0;j<=sum+1;j++)
...{
array[i][j]=0;
array[j][i]=0;
}
i=sum+1;
for(j=0;j<=sum+1;j++)
...{
array[i][j]=0;
array[j][i]=0;
}
cout<<"请输入迷宫,0代表墙,1代表路,3代表出口,入口默认为(1,1)"<<endl;
for(i=1;i<=sum;i++)
for(j=1;j<=sum;j++)
cin>>array[i][j];
for(i=1;i<=sum;i++)
for(j=1;j<=sum;j++)
...{
if(array[i][j]==3)
...{
outx=i;
outy=j;
}
}
if(!(outx||outy))
...{
cout<<"没有出口!程序将采用默认出口( "<<sum<<" , "<<sum<<" )"<<endl;
outx=sum;
outy=sum;
array[sum][sum]=1;
}
}
void ok(int num)
...{
cout<<endl<<"走出来啦!"<<endl;
SHAGUO shaguo1;
int x,y;
while(!shaguo.empty())
...{
shaguo1=shaguo.top();
x=shaguo1.x;
y=shaguo1.y;
array[x][y]=4;
shaguo.pop();
}
array[outx][outy]=4;
for(x=1;x<=num;x++)
...{
for(y=1;y<=num;y++)
...{
if(array[x][y]==4)
cout<<''<<' ';
else cout<<'*'<<' ';
}
cout<<endl;
}
system("pause");
exit(0);
}
void bad()
...{
cout<<endl<<"没有路!"<<endl;
system("pause");
exit(0);
}
void go(int num)
...{
SHAGUO shaguo2,shaguo1;
shaguo2.x=1;
shaguo2.y=1;
shaguo.push(shaguo2);
int X=1,Y=1;
int come=1;//探索方向
while(1&&!shaguo.empty())
...{
shaguo1=shaguo.top();
X=shaguo1.x;
Y=shaguo1.y;
switch(come)
...{
case 1://向下
...{
X=X+1;
if(array[X][Y]&&array[X][Y]!=2)//不是走下面上来的
...{
if(X==outx&&Y==outy)
ok(num);
shaguo2.x=X;
shaguo2.y=Y;
array[X][Y]=2;
shaguo.push(shaguo2);
come=1;
}
else
...{
come++;
}
}break;
case 2://向右
...{
Y=Y+1;
if(array[X][Y]&&array[X][Y]!=2)
...{
if(X==outx&&Y==outy)
ok(num);
shaguo2.x=X;
shaguo2.y=Y;
shaguo.push(shaguo2);
array[X][Y]=2;
come=1;
}
else
...{
come++;
}
}break;
case 3:
...{
X=X-1;
if(array[X][Y]&&array[X][Y]!=2)
...{
if(X==outx&&Y==outy)
ok(num);
shaguo2.x=X;
shaguo2.y=Y;
shaguo.push(shaguo2);
array[X][Y]=2;
come=1;
}
else
...{
come++;
}
}break;
case 4://向右
...{
Y=Y-1;
if(array[X][Y]&&array[X][Y]!=2)
...{
if(X==outx&&Y==outy)
ok(num);
shaguo2.x=X;
shaguo2.y=Y;
shaguo.push(shaguo2);
array[X][Y]=2;
come=1;
}
else
...{
come=1;
array[X][Y]=0;
shaguo.pop();
if(!shaguo.empty())
...{
shaguo1=shaguo.top();
if(shaguo1.x==1&&shaguo1.y==1)
bad();
}
else bad();
}
}break;
}
}
bad();
}
void main()
...{
cout<<"请输入迷宫大小,只需输入迷宫的维数,8即是8*8的迷宫"<<endl;
int sum;
cin>>sum;
Creat(sum);
go(sum);
}
#include<stack>
#include<malloc.h>
using namespace std;
struct SHAGUO
...{
int x;
int y;
};
int **array;
int outx=0,outy=0;//出口坐标
stack<SHAGUO>shaguo;
void Creat(int sum)
...{
int i,j;
array=(int **)malloc((sum+2)*sizeof(int *));
for( i=0;i<=sum+1;i++)
array[i]=(int *)malloc((sum+2)*sizeof(int));
i=0;/**//*下面把迷宫四周设成墙 ,0 代表墙,1代表路*/
for(j=0;j<=sum+1;j++)
...{
array[i][j]=0;
array[j][i]=0;
}
i=sum+1;
for(j=0;j<=sum+1;j++)
...{
array[i][j]=0;
array[j][i]=0;
}
cout<<"请输入迷宫,0代表墙,1代表路,3代表出口,入口默认为(1,1)"<<endl;
for(i=1;i<=sum;i++)
for(j=1;j<=sum;j++)
cin>>array[i][j];
for(i=1;i<=sum;i++)
for(j=1;j<=sum;j++)
...{
if(array[i][j]==3)
...{
outx=i;
outy=j;
}
}
if(!(outx||outy))
...{
cout<<"没有出口!程序将采用默认出口( "<<sum<<" , "<<sum<<" )"<<endl;
outx=sum;
outy=sum;
array[sum][sum]=1;
}
}
void ok(int num)
...{
cout<<endl<<"走出来啦!"<<endl;
SHAGUO shaguo1;
int x,y;
while(!shaguo.empty())
...{
shaguo1=shaguo.top();
x=shaguo1.x;
y=shaguo1.y;
array[x][y]=4;
shaguo.pop();
}
array[outx][outy]=4;
for(x=1;x<=num;x++)
...{
for(y=1;y<=num;y++)
...{
if(array[x][y]==4)
cout<<''<<' ';
else cout<<'*'<<' ';
}
cout<<endl;
}
system("pause");
exit(0);
}
void bad()
...{
cout<<endl<<"没有路!"<<endl;
system("pause");
exit(0);
}
void go(int num)
...{
SHAGUO shaguo2,shaguo1;
shaguo2.x=1;
shaguo2.y=1;
shaguo.push(shaguo2);
int X=1,Y=1;
int come=1;//探索方向
while(1&&!shaguo.empty())
...{
shaguo1=shaguo.top();
X=shaguo1.x;
Y=shaguo1.y;
switch(come)
...{
case 1://向下
...{
X=X+1;
if(array[X][Y]&&array[X][Y]!=2)//不是走下面上来的
...{
if(X==outx&&Y==outy)
ok(num);
shaguo2.x=X;
shaguo2.y=Y;
array[X][Y]=2;
shaguo.push(shaguo2);
come=1;
}
else
...{
come++;
}
}break;
case 2://向右
...{
Y=Y+1;
if(array[X][Y]&&array[X][Y]!=2)
...{
if(X==outx&&Y==outy)
ok(num);
shaguo2.x=X;
shaguo2.y=Y;
shaguo.push(shaguo2);
array[X][Y]=2;
come=1;
}
else
...{
come++;
}
}break;
case 3:
...{
X=X-1;
if(array[X][Y]&&array[X][Y]!=2)
...{
if(X==outx&&Y==outy)
ok(num);
shaguo2.x=X;
shaguo2.y=Y;
shaguo.push(shaguo2);
array[X][Y]=2;
come=1;
}
else
...{
come++;
}
}break;
case 4://向右
...{
Y=Y-1;
if(array[X][Y]&&array[X][Y]!=2)
...{
if(X==outx&&Y==outy)
ok(num);
shaguo2.x=X;
shaguo2.y=Y;
shaguo.push(shaguo2);
array[X][Y]=2;
come=1;
}
else
...{
come=1;
array[X][Y]=0;
shaguo.pop();
if(!shaguo.empty())
...{
shaguo1=shaguo.top();
if(shaguo1.x==1&&shaguo1.y==1)
bad();
}
else bad();
}
}break;
}
}
bad();
}
void main()
...{
cout<<"请输入迷宫大小,只需输入迷宫的维数,8即是8*8的迷宫"<<endl;
int sum;
cin>>sum;
Creat(sum);
go(sum);
}
- 迷宫
- 迷宫
- 迷宫
- 迷宫
- 迷宫?
- 迷宫
- 迷宫
- 迷宫
- 迷宫
- 迷宫
- 迷宫
- 迷宫
- 迷宫
- 迷宫。。。
- 迷宫
- 迷宫
- 迷宫
- 迷宫
- Hebrew word list(1)
- 图形文件的格式(转载)
- JVM的原理
- OpenGL lighting hints for new programmers
- OpenGL ES API简要介绍
- 迷宫
- 原始输入[第一次翻译msdn,保留原文,欢迎指正]
- UCOSII252成功移植到44B0的板子上的经验+自己总结的小细节
- 关于java里的堆(heap)栈(stack)信息!
- Advanced Visual Effects with OpenGL - Inside the GDC 2006(From GameDev.net)
- 用CSS调整文字间距
- 一个o(n)的排序算法,类位图排序
- 你真的会用Google搜索引擎吗
- PC & 软件 的未来