穷举vs暴搜vs深搜vs回溯vs剪枝系列一>优美的排列
题目:
解析:
部分决策树:
代码设计:
代码:
private int count; private boolean[] check; public int countArrangement(int n) { check = new boolean[n+1]; dfs(n,1); return count; } private void dfs(int n, int pos){ if(pos == n+1){ count++; return; } for(int i = 1; i <= n; i++){ if(check[i] == false && (i % pos == 0 || pos % i == 0)){ check[i] = true; dfs(n,pos+1); check[i] = false;//恢复现场 } } }