#include<stdio.h>
int main() {
int x = 0, y = 0;
int maze1[4][4] = { { 1, 0, 0, 0 }, { 1, 1, 1, 1 }, { 0, 1, 0, 0 }, { 1, 1, 1, 1}}; // The maze which is should be solved.
int maze[4][4];
for (int k = 0; k < 4; k++) { //Creating 4*4 matrix
for (int l = 0; l < 4; l++) {
maze[k][l] = 0;
}
}
printf("\n\n\n");
sol(maze, x, y, maze1);
for (int k = 0; k < 4; k++) {
for (int l = 0; l < 4; l++) {
printf("%3d", maze[k][l]);
}
printf("\n");
}
return 0;
}
int sol(int maze[4][4], int x, int y, int maze1[4][4]) {
if (x < 4 && x >= 0 && y >= 0 && y < 4 && maze[x][y] == 0 && maze1[x][y] == 1) {
maze[x][y] = 1;
if (x == 3 && y == 3) {
return 0;
}
if (sol(maze, x + 1, y, maze1) == 1) //How does it works ?
return 0;
if (sol(maze, x, y + 1, maze1) == 0) // How does it works ?
return 0;
else {
maze[x][y] = 0;
return 0;
}
}
}
答案 0 :(得分:0)
标记的行是简单的函数调用。所调用的函数是当前正在执行的函数这一事实不是问题。这称为递归函数调用。您可以在Wikipedia和其他地方阅读有关recursion的信息。递归调用具有自己的参数和局部变量。
例如,假设您要计算所有非负整数之和,直到给定整数。好吧,您可以将其定义如下:
int sum_up_to(int i) {
if (i <= 0)
return 0;
else
return i + sum_up_to(i-1);
}
所以
sum_up_to(0)
返回0
。sum_up_to(1)
返回1 + sum_up_to(0)
,即1 + 0
,即1
。sum_up_to(2)
返回2 + sum_up_to(1)
,即2 + 1
,即3
。sum_up_to(3)
返回3 + sum_up_to(2)
,即3 + 3
,即6
。sum_up_to(4)
返回4 + sum_up_to(3)
,即4 + 6
,即10
。