关于走出迷宫计算步数的问题
程序代码:#include<stdio.h>
#define N 15
int walk(char a[][N],int x,int y)
{ int n1,n2,n3,n4;
if(x>N-1&&x<0||y<0&&y>N-1) return 0;//不在迷宫内
if(a[x][y]!=' ') return 0;//撞墙
if(a[x][y]=='E') return 1;//找到出口
else{
a[x][y]='o';//足记
n1=walk(a,x-1,y);
n2=walk(a,x,y+1);
n3=walk(a,x+1,y);
n4=walk(a,x,y-1);
return n1+n2+n3+n4;//总步数
}
}
int main()
{ int total_step ,i=0,j=0;
char a[][N]={{"***************"},
{"****S**********"},
{"**** **********"},
{"**** **********"},
{"**** **********"},
{"**** **********"},
{"**** **********"},
{"**** **********"},
{"**** **********"},
{"**** **********"},
{"**** **********"},
{"**** E***"},
{"***************"},
{"***************"},
{"***************"}};
for(;i<N;++i)
for(;j<N;++j)
if(a[i][j]=='S') break;//找到迷宫入口
a[i][j]=' ';
total_step=walk(a,i,j);
for(i=0;i<N;++i)
{for(j=0;j<N;++j)
printf("%c",a[i][j]);
printf("\n");
}
if(total_step==0) printf("error\n");
else printf("%d",total_step);
return 0;
}
参考了本论坛大神写的,自己改了下但实现不了,不知问题出现在哪,请指教,,,在线等
[此贴子已经被作者于2017-4-15 14:10编辑过]






