Skip to content

Instantly share code, notes, and snippets.

@gavinHuang
Created November 14, 2014 05:12
Show Gist options
  • Save gavinHuang/2d113b850c526a839187 to your computer and use it in GitHub Desktop.
Save gavinHuang/2d113b850c526a839187 to your computer and use it in GitHub Desktop.
DFS for allsorts
public class DFS2 {
int[] numbers = {2,3,4,5,6};
boolean[] book = new boolean[numbers.length];
int[] answers = new int[numbers.length];
public static void main(String[] args) {
DFS2 dfs = new DFS2();
dfs.dfs(0);
}
public void dfs(int step){
//condition check
if (step == numbers.length){
//print
for(int v : answers){
System.out.print(v+" ");
}
System.out.println();
return;
}
for(int i=0;i<numbers.length;i++){
if (!book[i]){
answers[step] = numbers[i];
book[i] = true;
//do next step
dfs(step+1);
//go back one
book[i] = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment