翻了翻题解,都是用全局变量统计答案,那么以下是使用局部变量统计答案的做法,其实差不多
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 10;
int n, m, t, x, y;
bool vis[N][N];
int dx[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};
int dfs(int x, int y, int cnt)
{
if (cnt == n * m) {
return 1;
}
int ans = 0;
for (int i = 0; i < 8; i ++ )
{
int tx = x + dx[i], ty = y + dy[i];
if (tx >= 0 && tx < n && ty >= 0 && ty < m) {
if (vis[tx][ty]) continue;
vis[tx][ty] = 1;
ans += dfs(tx, ty, cnt + 1);
vis[tx][ty] = 0;
}
}
return ans;
}
int main(void) {
scanf("%d", &t);
while (t -- ) {
scanf("%d%d%d%d", &n, &m, &x, &y);
memset(vis, 0, sizeof vis);
vis[x][y] = 1;
cout << dfs(x, y, 1) << endl;
}
return 0;
}