想象一下坐在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