Created
November 14, 2014 05:12
-
-
Save gavinHuang/2d113b850c526a839187 to your computer and use it in GitHub Desktop.
DFS for allsorts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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){ | |
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