2021年08月16日 周一 天气雨 【不悲叹过去,不荒废现在,不惧怕未来】
1. 题目简介
原题链接:http://noi.openjudge.cn/ch0205/8465/
2. DFS
思路比较清晰,直接用dfs在8个方向上进行搜索即可。
#include <iostream>
#include <vector>
using namespace std;
int t, m, n, x, y, mn, ans;
vector<vector<int>> board;
vector<vector<int>> dir = { {-1,-2},{-2,-1},{-1,2},{-2,1},{1,-2},{2,-1},{1,2},{2,1} };
void dfs(int x, int y, int cnt) {
if (++cnt == mn) {
++ans;
return;
}
board[x][y] = 1;
for (int i = 0; i < 8; ++i) {
int newx = x + dir[i][0], newy = y + dir[i][1];
if (newx >= 0 && newx < m && newy >= 0 && newy < n && !board[newx][newy]) {
dfs(newx, newy, cnt);
}
}
// 搜索完记得恢复
board[x][y] = 0;
}
int main() {
// 1
// 5 4 0 0
scanf("%d", &t);
for (int i = 0; i < t; ++i) {
scanf("%d %d %d %d", &m, &n, &x, &y);
board.resize(m, vector<int>(n));
ans = 0;
mn = m * n;
dfs(x, y, 0);
cout << ans << endl;
}
}
参考文献
https://blog.csdn.net/yuanxu716/article/details/78086978