Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement