查找二元矩阵上的任意路径
我正在尝试编写一个C程序,该C程序遵循左右的1秒打印1s的水平移动2s的1s。它不必是最短的路径或计算每个路径。我试图递归地写下它非常新。
char check_path(char *table,int current_row,int current_col);
int current_row=0,current_col=0;
int main()
{
int i,j,row,col,current_row,current_col;
char table[30][30];
printf("Matrisin satir sayisini giriniz : ");
scanf("%d",&row);
printf("Matrisin sutun sayisini giriniz : ");
scanf("%d",&col);
for(i=0;i<row;i++){
for(j=0;j<col;j++){
printf("1 ya da 0 olacak sekilde %d. satir ve %d. sutundaki elemani giriniz : ",i+1,j+1);
scanf("%d",&table[i][j]);
}
}
check_path(table,0,0);
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf("%d\t", table[i][j]);
}
printf("\n");
}
return 0;
}
char check_path(char *table,int current_row,int current_col)
{
if (*(*(table+current_row)+current_col+1) != 0)
{
*(*(table+current_row)+current_col+1) = '-';
printf("1\t");
return check_path(table,current_row,current_col+1);
}
else
{
current_col=0;
printf("2\t");
return check_path(table,current_row+1,current_col);
}
}
I am trying to write a C program that follow 1s to the left or down printing out 1s for horizontal moves 2s for vertical.It doesn`t have to be the shortest path or calculate every path.I tried to write it recursively but I am very new to it.
char check_path(char *table,int current_row,int current_col);
int current_row=0,current_col=0;
int main()
{
int i,j,row,col,current_row,current_col;
char table[30][30];
printf("Matrisin satir sayisini giriniz : ");
scanf("%d",&row);
printf("Matrisin sutun sayisini giriniz : ");
scanf("%d",&col);
for(i=0;i<row;i++){
for(j=0;j<col;j++){
printf("1 ya da 0 olacak sekilde %d. satir ve %d. sutundaki elemani giriniz : ",i+1,j+1);
scanf("%d",&table[i][j]);
}
}
check_path(table,0,0);
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf("%d\t", table[i][j]);
}
printf("\n");
}
return 0;
}
char check_path(char *table,int current_row,int current_col)
{
if (*(*(table+current_row)+current_col+1) != 0)
{
*(*(table+current_row)+current_col+1) = '-';
printf("1\t");
return check_path(table,current_row,current_col+1);
}
else
{
current_col=0;
printf("2\t");
return check_path(table,current_row+1,current_col);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论