Files
python/TangDou/AcWing/SearchOrder/1116.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

55 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 10;
int n, m;
bool st[N][N]; // 是否走过
int ans;
// 八个方向
int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};
// cnt:已经走了多少个格子
void dfs(int x, int y, int cnt) {
// 收集答案
if (cnt == n * m) {
ans++;
return;
}
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) continue;
if (st[tx][ty]) continue;
// 标识当前格子已使用
st[tx][ty] = 1;
dfs(tx, ty, cnt + 1);
// 标识当前格子未使用
st[tx][ty] = 0;
}
}
int main() {
int T;
cin >> T;
while (T--) {
int x, y;
cin >> n >> m >> x >> y;
// 清空状态数组
memset(st, 0, sizeof st);
// 清空方案数
ans = 0;
// 标识起点已访问
st[x][y] = 1;
// 从(x,y)出发,目前的访问个数为1
dfs(x, y, 1);
// 最终有多少条路径
cout << ans << endl;
}
return 0;
}