Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. package hw3.puzzle;
  2. import edu.princeton.cs.algs4.Queue;
  3.  
  4. public class Board implements WorldState {
  5.  
  6. private int[][] board, goal;
  7. private int sideLength;
  8.  
  9. //Constructs a board from an N-by-N array of tiles where
  10. //tiles[i][j] = tile at row i, column j
  11. public Board(int[][] tiles) {
  12. //board = tiles;
  13. //iterate thru and make copy
  14. sideLength = tiles.length;
  15. board = new int[sideLength][sideLength];
  16. for (int i = 0; i < sideLength; i++) {
  17. for (int j = 0; j < sideLength; j++) {
  18. board[i][j] = tiles[i][j];
  19. }
  20. }
  21. //goal = new int[sideLength][sideLength];
  22. //for (int i = 0; i < sideLength; i++) {
  23. // for (int j = 0; j < sideLength; j++) {
  24. // goal[i][j] = (i * sideLength) + j + 1;
  25. // }
  26. //}
  27. }
  28.  
  29. //Returns value of tile at row i, column j (or 0 if blank)
  30. public int tileAt(int i, int j) {
  31. if (i < 0 || i > (board.length - 1) || j < 0 || j > (board.length - 1)) {
  32. throw new IndexOutOfBoundsException("Can't do this");
  33. }
  34. return board[i][j];
  35. }
  36.  
  37. //Returns the board size N
  38. public int size() {
  39. return sideLength;
  40. }
  41.  
  42. /**
  43. * Returns neighbors of this board.
  44. * SPOILERZ: This is the answer.
  45. */
  46. @Override
  47. public Iterable<WorldState> neighbors() {
  48. Queue<WorldState> neighbors = new Queue<>();
  49. int hug = size();
  50. int bug = -1;
  51. int zug = -1;
  52. for (int rug = 0; rug < hug; rug++) {
  53. for (int tug = 0; tug < hug; tug++) {
  54. if (tileAt(rug, tug) == 0) {
  55. bug = rug;
  56. zug = tug;
  57. }
  58. }
  59. }
  60. int[][] ili1li1 = new int[hug][hug];
  61. for (int pug = 0; pug < hug; pug++) {
  62. for (int yug = 0; yug < hug; yug++) {
  63. ili1li1[pug][yug] = tileAt(pug, yug);
  64. }
  65. }
  66. for (int l11il = 0; l11il < hug; l11il++) {
  67. for (int lil1il1 = 0; lil1il1 < hug; lil1il1++) {
  68. if (Math.abs(-bug + l11il) + Math.abs(lil1il1 - zug) - 1 == 0) {
  69. ili1li1[bug][zug] = ili1li1[l11il][lil1il1];
  70. ili1li1[l11il][lil1il1] = 0;
  71. Board neighbor = new Board(ili1li1);
  72. neighbors.enqueue(neighbor);
  73. ili1li1[l11il][lil1il1] = ili1li1[bug][zug];
  74. ili1li1[bug][zug] = 0;
  75. }
  76. }
  77. }
  78. return neighbors;
  79. }
  80.  
  81. //Hamming estimate described below
  82. public int hamming() {
  83. int numWrong = 0;
  84. for (int i = 0; i < sideLength; i++) {
  85. for (int j = 0; j < sideLength; j++) {
  86. if (board[i][j] != (i * sideLength) + j + 1) {
  87. numWrong++;
  88. }
  89. }
  90. }
  91. return numWrong;
  92. }
  93.  
  94. //Manhattan estimate described below
  95. public int manhattan() {
  96. int distanceWrong = 0;
  97. for (int i = 0; i < sideLength; i++) {
  98. for (int j = 0; j < sideLength; j++) {
  99. int whereItShouldBeI = (board[i][j] - 1) / sideLength;
  100. int whereItShouldBeJ = (board[i][j] - 1) % sideLength;
  101. if (whereItShouldBeI != i || whereItShouldBeJ != j) {
  102. distanceWrong += Math.abs(whereItShouldBeI - i) + Math.abs(whereItShouldBeJ - j);
  103. }
  104. }
  105. }
  106. return distanceWrong;
  107. }
  108.  
  109. //Estimated distance to goal. This method should
  110. //simply return the results of manhattan() when submitted to
  111. //Gradescope.
  112. public int estimatedDistanceToGoal() {
  113. return manhattan();
  114. }
  115.  
  116. //Returns true if is this board the goal board
  117. /**public boolean isGoal() {
  118. for (int i = 0; i < sideLength * sideLength - 1; i++) {
  119. for (int j = 0; i < sideLength * sideLength - 1; j++) {
  120. if (board[i][j] != (i * sideLength) + j + 1) {
  121. return false;
  122. }
  123. }
  124. }
  125. return true;
  126. }*/
  127.  
  128. //Returns true if this board's tile values are the same
  129. //position as y's
  130. public boolean equals(Object y) {
  131.  
  132. Board copy = (Board) y;
  133. for (int i = 0; i < sideLength; i++) {
  134. for (int j = 0; j < sideLength; j++) {
  135. if (board[i][j] != copy.board[i][j]) {
  136. return false;
  137. }
  138. }
  139. }
  140. return true;
  141. }
  142.  
  143. /** Returns the string representation of the board.
  144. * Uncomment this method. */
  145. public String toString() {
  146. StringBuilder s = new StringBuilder();
  147. int N = size();
  148. s.append(N + "\n");
  149. for (int i = 0; i < N; i++) {
  150. for (int j = 0; j < N; j++) {
  151. s.append(String.format("%2d ", tileAt(i,j)));
  152. }
  153. s.append("\n");
  154. }
  155. s.append("\n");
  156. return s.toString();
  157. }
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement