新手求指教 迷宫问题 求找bug
c++吧
全部回复
仅看楼主
level 2
吴名112 楼主

#include<iostream>
#include<fstream>
using namespace std;
//struct MIGONG(){
//};
int i=1,j=1;
void zoumigong(int a[][10]){ //走迷宫
while(i!=8&&j!=8){
/*cout<<endl;
for(int s=0;s<10;s++)
{for(int k=0;k<10;k++)
cout<<a[s][k]<<" ";
cout<<endl;}
cout<<endl;*/
if(a[i+1][j]==0) //判断前后左右是否有通路,若有则标记该数为2,访问通路
{
a[i][j]=2;
i=i+1;
zoumigong(a);
}
else if(a[i-1][j]==0){
a[i][j]=2;
i=i-1;
zoumigong(a);
}
else if(a[i][j-1]==0){
a[i][j]=2;
j=j-1;
zoumigong(a);
}
else if(a[i][j+1]==0){
a[i][j]=2;
j=j+1;
zoumigong(a);
}
else if(a[i+1][j]==2) //周围没有通路,则原路返回,并标记该数为3,不再访问该元素
{
a[i][j]=3;
i=i+1;
zoumigong(a);
}
else if(a[i-1][j]==2){
a[i][j]=3;
i=i-1;
zoumigong(a);
}
else if(a[i][j-1]==2){
a[i][j]=3;
j=j-1;
zoumigong(a);
}
else if(a[i][j+1]==2){
a[i][j]=3;
j=j+1;
zoumigong(a);
}
else //如果都行不通的话 则输出error
{cout<<"error"<<endl;
return ;
}
}
int n[64],m[64]; //定义N,M数组 用以储存坐标
int x=0;
for(int s=0;s<10;s++) //找出路径,并标记坐标
for(int k=0;k<10;k++)
{
while(a[s][k]==2)
{s=n[x];
k=m[x];
x=x+1;}
}
ofstream outfile("path.txt",ios::out); //定义并创建以个path.txt
if(!outfile){
cerr<<"open error!"<<endl;
exit(1);
}
for(int y=0;y<=x;y++) //输出到path.txt
outfile<<"("<<n[y]<<","<<m[y]<<")"<<" ";
outfile.close();
cout<<endl;
}
int main(){
int a[10][10];
for(int k=0;k<10;k++) //手动输入迷宫数据,0为通路,1为墙
for(int s=0;s<10;s++)
cin>>a[k][s];
zoumigong(a); //开始走迷宫
return 0;
}
/*
1 1 1 1 1 1 1 1 1 1
1 0 1 1 0 1 0 1 0 1
1 0 1 0 1 0 0 0 0 1
1 0 0 0 0 1 0 1 0 1
1 0 1 0 1 0 0 1 0 1
1 0 1 0 0 0 1 0 0 1
1 1 1 0 1 0 1 0 1 1
1 1 0 0 1 1 1 0 0 1
1 0 1 0 0 0 1 1 0 1
1 1 1 1 1 1 1 1 1 1
*/
2013年03月19日 07点03分 1
1