Advertisement
Guest User

Untitled

a guest
May 28th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. public class Main {
  2. static int N = 4;
  3. static int[] solution = new int[N];
  4. static int[] use = new int[N];
  5.  
  6. public static void main(String[] args){
  7. backtrack(0);
  8. }
  9.  
  10.  
  11. public static void backtrack(int n){
  12. System.out.println(n);
  13.  
  14. if(n==N){
  15. System.out.println("=====================================");
  16. StringBuffer sb = new StringBuffer();
  17. for(int i=0; i<N; i++){
  18. sb.append(solution[i]).append(", ");
  19. }
  20. System.out.println(sb.toString());
  21. System.out.println("=====================================");
  22. return;
  23. }
  24.  
  25. for(int i=0; i<N; i++){
  26. System.out.println("n : "+n+", i : "+i);
  27.  
  28. if(use[i] == 0){
  29. solution[n] = i+1;
  30. use[i] = 1;
  31. backtrack(n+1);
  32. use[i] = 0;
  33. }
  34. }
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement