let label = 4;
let arr = new Array(4).fill(0);
let x = 0;
let arr_result = new Array(4).fill(0);
DFS(0);
function DFS(n){
if(n == label){
x++;
console.log(x,arr_result)
return;
}
for(let i=0;i<label;i++){
if(!arr[i]){
arr_result[n] = i+1;
arr[i] = 1;
DFS(n+1);
arr[i] = 0;
}
}
}
1 [ 1, 2, 3, 4 ]
2 [ 1, 2, 4, 3 ]
3 [ 1, 3, 2, 4 ]
4 [ 1, 3, 4, 2 ]
5 [ 1, 4, 2, 3 ]
6 [ 1, 4, 3, 2 ]
7 [ 2, 1, 3, 4 ]
8 [ 2, 1, 4, 3 ]
9 [ 2, 3, 1, 4 ]
10 [ 2, 3, 4, 1 ]
11 [ 2, 4, 1, 3 ]
12 [ 2, 4, 3, 1 ]
13 [ 3, 1, 2, 4 ]
14 [ 3, 1, 4, 2 ]
15 [ 3, 2, 1, 4 ]
16 [ 3, 2, 4, 1 ]
17 [ 3, 4, 1, 2 ]
18 [ 3, 4, 2, 1 ]
19 [ 4, 1, 2, 3 ]
20 [ 4, 1, 3, 2 ]
21 [ 4, 2, 1, 3 ]
22 [ 4, 2, 3, 1 ]
23 [ 4, 3, 1, 2 ]
24 [ 4, 3, 2, 1 ]
做深度递归,考虑数据回溯就好