1 条题解
-
0
一道经典的dfs题。dfs有三个参数,x, y 表示当前点的坐标,u表示步数。从起点x,y开始dfs,步数为1,当步数等于n * m 即棋盘的格子数,表示已经遍历完全部棋盘,然后路径加一。最后输出答案即可。
#include <iostream> using namespace std; const int dx[8] = {1, 2, 2, 1, -1, -2, -2, -1}; const int dy[8] = {2, 1, -1, -2, -2, -1, 1, 2}; int n, m, paths = 0; bool visited[10][10]; void dfs(int x, int y, int step) { if (step == n * m) { paths ++; return; } for (int i =0; i < 8; i++) { int nx = x + dx[i], ny = y + dy[i]; if (nx >= 0 && nx < n && ny >= 0 && ny < m && !visited[nx][ny]) { visited[nx][ny] = true; dfs(nx, ny, step + 1); visited[nx][ny] = false; } } } int main() { int x, y; cin >> n >> m >> x >> y; visited[x][y] = true; dfs(x, y, 1); cout << paths; return 0; }
- 1
信息
- ID
- 167
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 7
- 标签
- 递交数
- 1146
- 已通过
- 237
- 上传者