解题思路:看注释就行
注意事项:
参考代码:
import java.util.Scanner;
public class Main {
static boolean[][] vis; // 标记数组
static int[] orient = {-1, 2, 1, -2, -1, -2, 1, 2, -1}; // 8个方向
static int sum, n, m;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
// 矩阵图大小
n = sc.nextInt();
m = sc.nextInt();
vis = new boolean[n][m];
// 获起点
int x = sc.nextInt();
int y = sc.nextInt();
//方案总数
sum = 0;
// 标记起点位置已经访问
vis[x][y] = true;
dfs(x, y, 1);
System.out.println(sum);
}
}
public static void dfs(int x, int y, int curSum) {
if (curSum == (n * m)) {
sum++;
return;
}
// 递归8个方向
for (int i = 0; i < 8; i++) {
// 获取可以到达的新方向
int u = x + orient[i];
int v = y + orient[i+1];
// 剪枝条件,避免无效递归
if (u < 0 || u >= n || v < 0 || v >= m || vis[u][v]){
continue;
}
// 标记当前位置已访问
vis[u][v] = true;
// 深处递归
dfs(u, v, curSum+1);
// 回溯
vis[u][v] = false;
}
}
}
0.0分
3 人评分
逆反的01串 (C++代码)(依旧推荐switch)浏览:936 |
点我有惊喜!你懂得!浏览:1435 |
汽水瓶 (C语言代码)浏览:608 |
C语言程序设计教程(第三版)课后习题9.4 (Java代码)浏览:1419 |
C语言程序设计教程(第三版)课后习题5.5 (C语言代码)浏览:668 |
Pascal三角 (C语言代码)浏览:1188 |
数组输出 (C语言代码)--此题的题目描述有问题浏览:1817 |
C语言程序设计教程(第三版)课后习题8.6 (C语言代码)浏览:599 |
C语言训练-求1+2!+3!+...+N!的和 (C语言代码)万恶的long long浏览:880 |
C语言训练-求PI* (C语言代码)浏览:614 |