标签:import OLE i++ eve cte 深度优先遍历 sys print public
就是dfs,有个好听的名字叫回溯,其实就是穷举法,这种算法的时机复杂度为n^level ,效率还是很低的
import java.util.Stack;
public class DFSTest {
public static void main(String[] args) {
char[] p = {‘A‘,‘B‘,‘C‘};
boolean[] pb = new boolean[p.length];
dfs(p,pb,new Stack<>());
}
private static void dfs(char[] p,boolean[] pb,Stack<Character> res){
if(res.size() == p.length ){
System.out.println(res);
return;
}
for (int i = 0; i < p.length; i++) {
char c = p[i];
if(!pb[i]){
res.push(c);
pb[i] = true;
dfs(p,pb,res);
res.pop();
pb[i] = false;
}
}
}
}
标签:import OLE i++ eve cte 深度优先遍历 sys print public
原文地址:https://www.cnblogs.com/dongma/p/13171390.html