【发布时间】:2017-10-12 07:57:34
【问题描述】:
想象一个机器人坐在 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();
}
在运行时,会弹出一个错误说--
Program terminated due to "Segmentation fault" (11)
【问题讨论】:
-
编译时出现段错误???
-
我建议您阅读 Eric Lippert 的 How to debug small programs,并了解如何使用 调试器 在您的程序中捕获此类崩溃。
-
见this
标签: c