Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. ```java
  2. class Solution {
  3. public int minimumMoves(int[][] a) {
  4. int n = a.length;
  5. State start = new State(new Coordinate(0, 1),
  6. new Coordinate(0, 0), Dir.RIGHT);
  7. State end = new State(new Coordinate(n - 1, n - 1),
  8. new Coordinate(n - 1, n - 2), Dir.RIGHT);
  9.  
  10. Deque<QNode> q = new ArrayDeque<>();
  11. Map<State, Boolean> visited = new HashMap<>();
  12.  
  13. q.offer(new QNode(start, 0));
  14. visited.put(start, true);
  15. int ans = -1;
  16.  
  17. while (!q.isEmpty()) {
  18. QNode u = q.poll();
  19. if (u.state.equals(end)) {
  20. ans = u.dist;
  21. break;
  22. }
  23.  
  24. List<State> neighbors = new ArrayList<>();
  25. switch (u.state.dir) {
  26. case RIGHT:
  27. State right = new State(new Coordinate(u.state.head.x, u.state.head.y + 1),
  28. new Coordinate(u.state.tail.x, u.state.tail.y + 1), Dir.RIGHT);
  29. if (isWithinBoundaries(right, a) && isPossible(right, a)) {
  30. neighbors.add(right);
  31. }
  32.  
  33. State down = new State(new Coordinate(u.state.head.x + 1, u.state.head.y),
  34. new Coordinate(u.state.tail.x + 1, u.state.tail.y), Dir.RIGHT);
  35. // rotate only if down move is possible
  36. if (isWithinBoundaries(down, a) && isPossible(down, a)) {
  37. neighbors.add(down);
  38. neighbors.add(
  39. new State(new Coordinate(u.state.tail.x + 1, u.state.tail.y), u.state.tail, Dir.DOWN));
  40. }
  41. break;
  42. case DOWN:
  43. right = new State(new Coordinate(u.state.head.x, u.state.head.y + 1),
  44. new Coordinate(u.state.tail.x, u.state.tail.y + 1), Dir.DOWN);
  45. // rotate only if right move is possible
  46. if (isWithinBoundaries(right, a) && isPossible(right, a)) {
  47. neighbors.add(right);
  48. neighbors.add(
  49. new State(new Coordinate(u.state.tail.x, u.state.tail.y + 1), u.state.tail, Dir.RIGHT));
  50. }
  51.  
  52. down = new State(new Coordinate(u.state.head.x + 1, u.state.head.y),
  53. new Coordinate(u.state.tail.x + 1, u.state.tail.y), Dir.DOWN);
  54. if (isWithinBoundaries(down, a) && isPossible(down, a)) {
  55. neighbors.add(down);
  56. }
  57. }
  58.  
  59. for (State v : neighbors) {
  60. if (visited.containsKey(v)) {
  61. continue;
  62. }
  63. q.offer(new QNode(v, u.dist + 1));
  64. visited.put(v, true);
  65. }
  66. }
  67. return ans;
  68. }
  69.  
  70. boolean isWithinBoundaries(State st, int[][] a) {
  71. int n = a.length;
  72. if (st.head.x >= n || st.head.y >= n || st.tail.x >= n || st.tail.y >= n) {
  73. return false;
  74. }
  75. return true;
  76. }
  77.  
  78. boolean isPossible(State st, int[][] a) {
  79. if (a[st.head.x][st.head.y] == 1 || a[st.tail.x][st.tail.y] == 1) {
  80. return false;
  81. }
  82. return true;
  83. }
  84. }
  85.  
  86. class QNode {
  87. State state;
  88. int dist;
  89. public QNode(State _state, int _dist) {
  90. state = _state;
  91. dist = _dist;
  92. }
  93. }
  94. enum Dir {
  95. RIGHT, DOWN
  96. }
  97.  
  98. class Coordinate {
  99. int x, y;
  100. public Coordinate(int _x, int _y) {
  101. x = _x;
  102. y = _y;
  103. }
  104.  
  105. @Override
  106. public boolean equals(Object thatObj) {
  107. if (this == thatObj) {
  108. return true;
  109. }
  110. if (thatObj == null) {
  111. return false;
  112. }
  113. if (this.getClass() != thatObj.getClass()) {
  114. return false;
  115. }
  116. Coordinate that = (Coordinate) thatObj;
  117. return (x == that.x) && (y == that.y);
  118. }
  119. }
  120.  
  121. class State {
  122. Coordinate head, tail;
  123. Dir dir;
  124.  
  125. public State(Coordinate _head, Coordinate _tail, Dir _dir) {
  126. head = _head;
  127. tail = _tail;
  128. dir = _dir;
  129. }
  130.  
  131. @Override
  132. public boolean equals(Object that) {
  133. if (this == that) {
  134. return true;
  135. }
  136. if (that == null) {
  137. return false;
  138. }
  139. if (this.getClass() != that.getClass()) {
  140. return false;
  141. }
  142. State thatState = (State) that;
  143. if (!head.equals(thatState.head)) {
  144. return false;
  145. }
  146. if (!tail.equals(thatState.tail)) {
  147. return false;
  148. }
  149. return dir == thatState.dir;
  150. }
  151.  
  152. @Override
  153. public int hashCode() {
  154. int dirVal = 1;
  155. if (dir == Dir.DOWN) {
  156. dirVal = 2;
  157. }
  158. return dirVal + head.x + head.y + tail.x + tail.y;
  159. }
  160. }
  161. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement