求助!简单的递归问题
1 2 3 4
5 6 7 8
9 10 11 12
从1出发走五步有几种走法
#include<cstdio>
#include<cmath>
bool charge[3][5] = {false};
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int sum = 0;
void dfs(int index, int x, int y) {
if (index == 6) {
sum++;
return;
}
for (int i = 0; i < 4; i++) {
int newx = x + dir[i][0];
int newy = y + dir[i][1];
if ((!charge[newx][newy]) && newx < 4 && newx > 0 && newy < 5 && newy > 0) {
charge[newx][newy] = true;
dfs(index + 1, newx, newy);
charge[newx][newy] = false;
}
}
}
int main() {
charge[1][1] = true;
dfs(1, 1, 1);
printf("%d", sum);
return 0;
}
输出是0
错在哪里??