想象一下坐在NxN网格左上角的机器人。机器人只能在三个方向上移动:向右,向下和向下倾斜。机器人必须到达NxN网格的右下角。想象一下,某些方块是“非限制”或“偏移”的,这样机器人就无法踩到它们。编写一个程序来确定机器人可能路径的数量。查找NxN网格中的所有路径
这里是我的代码:
#include<stdio.h>
#include<string.h>
int abc[50]={}, count=0;
int ak[5][5];
void called(int a,int b,int c){
if(a==c-1 && b==c-1){
int i=0;
printf("(0 , 0) - ");
for(i=0;i<count;i+=2){
if(i==count-2)
printf("(%d , %d)",abc[i],abc[i+1]);
else
printf("(%d , %d) - ",abc[i],abc[i+1]);
}
printf("\n");
abc[count--]=-1;
abc[count--]=-1;
return;
}
else{
if(a!=c-1 && ak[a][b]!=1){
abc[count++]=a+1;
abc[count++]=b;
called(a+1,b,c);
}
if(b!=c-1 && ak[a][b]!=1){
abc[count++]=a;
abc[count++]=b+1;
called(a,b+1,c);
}
if(a!=c-1 && a!=c-1 && ak[a][b]!=1){
abc[count++]=a+1;
abc[count++]=b+1;
called(a+1,b+1,c);
}
abc[count--]=-1;
abc[count--]=-1;
}
}
void main(){
int a,b,i,j,n;
printf("Enter the size of the grid\n");
scanf("%d",&n);
if(n>=0){
for(i=0;i<n;i++)
for(j=0;j<n;j++)
ak[i][j]=0;
printf("Enter the grid points that are offsets\n");
scanf("%d",&a);
scanf("%d",&b);
while(a!=-1 && b!=-1){
ak[a][b]=1;
scanf("%d",&a);
scanf("%d",&b);
}
printf("The paths for the robot are\n");
called(0,0,n);
}
else
printf("Invalid Input");
getchar();
}
在运行时,一个错误弹出saying--
Program terminated due to "Segmentation fault" (11)
你在汇编上遇到seg错误? –
我建议你阅读Eric Lippert的[如何调试小程序](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/),并学习如何使用* debugger *在程序中捕捉这样的崩溃。 –
请参阅[this](https://stackoverflow.com/q/45522468/971127) – BLUEPIXY