public class Arrange {
private static final int n = 5;
private static int x[] = new int[n + 1];
private static int count = 0;
public static void main(String args[]) {
backTrack(1);
System.out.println(count);
}
public static boolean place(int k) {
for (int i = 1; i < k; i++) {
if (x[i] == x[k]) {
return false;
}
}
return true;
}
public static void backTrack(int t) {
if (t > n) {
count ++;
} else {
for (int i = 1; i <= n; i++) {
x[t] = i;
if (place(t)) {
backTrack(t + 1);
}
}
}
}
}
Java回溯法求全排列
最新推荐文章于 2024-09-21 22:15:06 发布