Created
May 29, 2016 01:50
-
-
Save jojoldu/687b11bdac3c6f9c9de71ccf08e60a95 to your computer and use it in GitHub Desktop.
ty.java
This file contains 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 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