Advertisement
Guest User

tsp

a guest
Mar 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class tsp {
  4. /**
  5. * Reads Sudoku Puzzle from STDIN in the following format. First number, should
  6. * be N, which is the value for this board configuration N^2 * N^2 = N^4 size
  7. * board Prints answer to STDOUT
  8. * <p/>
  9. * 4 0 0 0 0 0 0 0 5 0 0 9 4 0 2 8 0 0 0 6 0 0 5 0 0 9 0 0 3 0 0 8 0 0 2 0 0 0 2
  10. * 5 0 1 3 0 0 0 9 0 0 4 0 0 7 0 0 1 0 0 6 0 0 5 0 0 0 8 1 0 5 9 0 0 5 0 0 0 0 0
  11. * 0 0 7
  12. * <p/>
  13. * ZEROS represent values that need to be found
  14. *
  15. * @param args
  16. *
  17. *
  18. */
  19. public static void main(String[] args) {
  20. Scanner input = new Scanner(System.in);
  21. int N = 1;
  22. int SIZE = 7;
  23. int CELLS = 7;
  24.  
  25. int[] puzzle = { 1, 0, 0, 0, 0, 0, 3 };
  26. int[] puzzle2 = { 1, 0, 0, 0, 0, 0, 2 };
  27.  
  28. if (solve(puzzle) && solve(puzzle2)) {
  29. System.out.println("Puzzle Solved, here is the solution: ");
  30. for (int i = 0; i < CELLS; i++) {
  31. System.out.print(" " + puzzle[i]);
  32. if ((i + 1) % N == 0)
  33. System.out.print(" |");
  34. if (i != 0 && (i + 1) % SIZE == 0)
  35. System.out.println();
  36. if ((i + 1) % (N * SIZE) == 0)
  37. System.out.println("----------------------------");
  38. }
  39. for (int i = 0; i < CELLS; i++) {
  40. System.out.print(" " + puzzle2[i]);
  41. if ((i + 1) % N == 0)
  42. System.out.print(" |");
  43. if (i != 0 && (i + 1) % SIZE == 0)
  44. System.out.println();
  45. if ((i + 1) % (N * SIZE) == 0)
  46. System.out.println("----------------------------");
  47. }
  48. } else {
  49. System.out.println("Could not solve this puzzle");
  50. }
  51. System.out.println();
  52. }
  53.  
  54. /**
  55. * Simple recursive, deterministic, depth first search, backtracking algorithm
  56. * for Sudoku Row major format puzzle as input
  57. *
  58. * @param puzzle
  59. * Row major format puzzle as input
  60. * @return
  61. */
  62. public static boolean solve(int[] puzzle) {
  63. int N = 1;
  64. int SIZE = 7;
  65. int CELLS = 7;
  66. int[][] usablePath = new int[][] {
  67. { 0, 0, 0, 0, 0, 0, 0, 0 },
  68. { 0, 0, 1, 1, 0, 0, 0, 0 },
  69. { 0, 1, 0, 1, 1, 1, 1, 1 },
  70. { 0, 1, 1, 0, 1, 1, 1, 1 },
  71. { 0, 0, 1, 1, 0, 1, 0, 1 },
  72. { 0, 0, 1, 1, 1, 0, 1, 0 },
  73. { 0, 0, 1, 1, 0, 1, 0, 0 },
  74. { 0, 0, 1, 1, 1, 0, 0, 0 }
  75. };
  76.  
  77. boolean noEmptyCells = true;
  78. int myRow = 0, myCol = 0;
  79. for (int i = 0; i < CELLS; i++) {
  80. if (puzzle[i] == 0) {
  81. myRow = i / SIZE;
  82. myCol = i % SIZE;
  83. noEmptyCells = false;
  84. break;
  85. }
  86. }
  87. if (noEmptyCells)
  88. return true;
  89.  
  90. for (int choice = 1; choice <= SIZE; choice++) {
  91. boolean isValid = true;
  92. int gridCol = 6;
  93.  
  94. int j = 0;
  95. for (; j < SIZE; j++) {
  96. if (puzzle[j] == choice) {
  97. isValid = false;
  98. break;
  99. }
  100. }
  101. boolean theRealValid = false;
  102.  
  103. if (isValid) {
  104. if (usablePath[puzzle[(myRow * SIZE + myCol) - 1]][choice] == 0) {
  105. isValid = false;
  106. }
  107. }
  108. if (isValid) {
  109. puzzle[myRow * SIZE + myCol] = choice;
  110. boolean solved = solve(puzzle);
  111. if (solved)
  112. return true;
  113. else
  114. puzzle[myRow * SIZE + myCol] = 0;
  115. }
  116.  
  117. }
  118. return false;
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement