Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. public class Solver {
  2. private final Stack<Board> boards;
  3. private int moves;
  4. private boolean isSolvable;
  5.  
  6. private class SearchNode implements Comparable<SearchNode> {
  7. private Board board;
  8. private int moves;
  9. private SearchNode previous;
  10. private int cachedPriority = -1;
  11.  
  12. SearchNode(Board board, int moves, SearchNode previous) {
  13. this.board = board;
  14. this.moves = moves;
  15. this.previous = previous;
  16. }
  17.  
  18. private int priority() {
  19. if (cachedPriority == -1) {
  20. cachedPriority = moves + board.manhattan();
  21. }
  22. return cachedPriority;
  23. }
  24.  
  25. @Override
  26. public int compareTo(SearchNode that) {
  27. if (this.priority() < that.priority()) {
  28. return -1;
  29. }
  30. if (this.priority() > that.priority()) {
  31. return +1;
  32. }
  33. return 0;
  34. }
  35. }
  36.  
  37. /*
  38. * find a solution to the initial board (using the A* algorithm)
  39. */
  40. public Solver(Board initial) {
  41. boards = new Stack<Board>();
  42. if (initial.isGoal()) {
  43. isSolvable = true;
  44. this.boards.push(initial);
  45. return;
  46. }
  47. if (initial.twin().isGoal()) {
  48. isSolvable = false;
  49. return;
  50. }
  51.  
  52. MinPQ<SearchNode> minPQ = new MinPQ<SearchNode>();
  53. MinPQ<SearchNode> minPQTwin = new MinPQ<SearchNode>();
  54. moves = 0;
  55. Board board = initial;
  56. Board boardTwin = initial.twin();
  57. SearchNode node = new SearchNode(board, 0, null);
  58. SearchNode nodeTwin = new SearchNode(boardTwin, 0, null);
  59. minPQ.insert(node);
  60. minPQTwin.insert(nodeTwin);
  61. while (moves < 100) {
  62. node = minPQ.delMin();
  63. nodeTwin = minPQTwin.delMin();
  64. board = node.board;
  65. boardTwin = nodeTwin.board;
  66. if (boardTwin.isGoal()) {
  67. isSolvable = false;
  68. return;
  69. }
  70. if (board.isGoal()) {
  71. isSolvable = true;
  72. this.boards.push(board);
  73. while (node.previous != null) {
  74. node = node.previous;
  75. this.boards.push(node.board);
  76. }
  77. return;
  78. }
  79. node.moves++;
  80. nodeTwin.moves++;
  81. Iterable<Board> neighbors = board.neighbors();
  82. for (Board neighbor : neighbors) {
  83. if (node.previous != null
  84. && neighbor.equals(node.previous.board)) {
  85. continue;
  86. }
  87. SearchNode newNode = new SearchNode(neighbor, node.moves, node);
  88. minPQ.insert(newNode);
  89. }
  90. Iterable<Board> neighborsTwin = boardTwin.neighbors();
  91. for (Board neighbor : neighborsTwin) {
  92. if (nodeTwin.previous != null
  93. && neighbor.equals(nodeTwin.previous.board)) {
  94. continue;
  95. }
  96. SearchNode newNode = new SearchNode(neighbor, nodeTwin.moves,
  97. nodeTwin);
  98. minPQTwin.insert(newNode);
  99. }
  100. }
  101. }
  102.  
  103. /*
  104. * is the initial board solvable?
  105. */
  106. public boolean isSolvable() {
  107. return isSolvable;
  108. }
  109.  
  110. /*
  111. * min number of moves to solve initial board; -1 if no solution
  112. */
  113. public int moves() {
  114. if (isSolvable) {
  115. return boards.size() - 1;
  116. } else {
  117. return -1;
  118. }
  119. }
  120.  
  121. /*
  122. * sequence of boards in a shortest solution; null if no solution
  123. */
  124. public Iterable<Board> solution() {
  125. if (isSolvable) {
  126. return boards;
  127. } else {
  128. return null;
  129. }
  130. }
  131.  
  132. /*
  133. * solve a slider puzzle (given below)
  134. */
  135. public static void main(String[] args) {
  136. // create initial board from file
  137. In in = new In(args[0]);
  138. int N = in.readInt();
  139. int[][] blocks = new int[N][N];
  140. for (int i = 0; i < N; i++)
  141. for (int j = 0; j < N; j++)
  142. blocks[i][j] = in.readInt();
  143. Board initial = new Board(blocks);
  144.  
  145. // solve the puzzle
  146. Solver solver = new Solver(initial);
  147.  
  148. // print solution to standard output
  149. if (!solver.isSolvable())
  150. StdOut.println("No solution possible");
  151. else {
  152. StdOut.println("Minimum number of moves = " + solver.moves());
  153. for (Board board : solver.solution())
  154. StdOut.println(board);
  155. }
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement