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
錯在哪里??
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/282193.html
標籤:C++ 語言
