Skip to content

Instantly share code, notes, and snippets.

@jojoldu
Created May 29, 2016 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jojoldu/687b11bdac3c6f9c9de71ccf08e60a95 to your computer and use it in GitHub Desktop.
Save jojoldu/687b11bdac3c6f9c9de71ccf08e60a95 to your computer and use it in GitHub Desktop.
ty.java
public class Main {
static int N = 4;
static int[] solution = new int[N];
static int[] use = new int[N];
public static void main(String[] args){
backtrack(0);
}
public static void backtrack(int n){
System.out.println(n);
if(n==N){
System.out.println("=====================================");
StringBuffer sb = new StringBuffer();
for(int i=0; i<N; i++){
sb.append(solution[i]).append(", ");
}
System.out.println(sb.toString());
System.out.println("=====================================");
return;
}
for(int i=0; i<N; i++){
System.out.println("n : "+n+", i : "+i);
if(use[i] == 0){
solution[n] = i+1;
use[i] = 1;
backtrack(n+1);
use[i] = 0;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment