Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```java
- class Solution {
- public int minimumMoves(int[][] a) {
- int n = a.length;
- State start = new State(new Coordinate(0, 1),
- new Coordinate(0, 0), Dir.RIGHT);
- State end = new State(new Coordinate(n - 1, n - 1),
- new Coordinate(n - 1, n - 2), Dir.RIGHT);
- Deque<QNode> q = new ArrayDeque<>();
- Map<State, Boolean> visited = new HashMap<>();
- q.offer(new QNode(start, 0));
- visited.put(start, true);
- int ans = -1;
- while (!q.isEmpty()) {
- QNode u = q.poll();
- if (u.state.equals(end)) {
- ans = u.dist;
- break;
- }
- List<State> neighbors = new ArrayList<>();
- switch (u.state.dir) {
- case RIGHT:
- State right = new State(new Coordinate(u.state.head.x, u.state.head.y + 1),
- new Coordinate(u.state.tail.x, u.state.tail.y + 1), Dir.RIGHT);
- if (isWithinBoundaries(right, a) && isPossible(right, a)) {
- neighbors.add(right);
- }
- State down = new State(new Coordinate(u.state.head.x + 1, u.state.head.y),
- new Coordinate(u.state.tail.x + 1, u.state.tail.y), Dir.RIGHT);
- // rotate only if down move is possible
- if (isWithinBoundaries(down, a) && isPossible(down, a)) {
- neighbors.add(down);
- neighbors.add(
- new State(new Coordinate(u.state.tail.x + 1, u.state.tail.y), u.state.tail, Dir.DOWN));
- }
- break;
- case DOWN:
- right = new State(new Coordinate(u.state.head.x, u.state.head.y + 1),
- new Coordinate(u.state.tail.x, u.state.tail.y + 1), Dir.DOWN);
- // rotate only if right move is possible
- if (isWithinBoundaries(right, a) && isPossible(right, a)) {
- neighbors.add(right);
- neighbors.add(
- new State(new Coordinate(u.state.tail.x, u.state.tail.y + 1), u.state.tail, Dir.RIGHT));
- }
- down = new State(new Coordinate(u.state.head.x + 1, u.state.head.y),
- new Coordinate(u.state.tail.x + 1, u.state.tail.y), Dir.DOWN);
- if (isWithinBoundaries(down, a) && isPossible(down, a)) {
- neighbors.add(down);
- }
- }
- for (State v : neighbors) {
- if (visited.containsKey(v)) {
- continue;
- }
- q.offer(new QNode(v, u.dist + 1));
- visited.put(v, true);
- }
- }
- return ans;
- }
- boolean isWithinBoundaries(State st, int[][] a) {
- int n = a.length;
- if (st.head.x >= n || st.head.y >= n || st.tail.x >= n || st.tail.y >= n) {
- return false;
- }
- return true;
- }
- boolean isPossible(State st, int[][] a) {
- if (a[st.head.x][st.head.y] == 1 || a[st.tail.x][st.tail.y] == 1) {
- return false;
- }
- return true;
- }
- }
- class QNode {
- State state;
- int dist;
- public QNode(State _state, int _dist) {
- state = _state;
- dist = _dist;
- }
- }
- enum Dir {
- RIGHT, DOWN
- }
- class Coordinate {
- int x, y;
- public Coordinate(int _x, int _y) {
- x = _x;
- y = _y;
- }
- @Override
- public boolean equals(Object thatObj) {
- if (this == thatObj) {
- return true;
- }
- if (thatObj == null) {
- return false;
- }
- if (this.getClass() != thatObj.getClass()) {
- return false;
- }
- Coordinate that = (Coordinate) thatObj;
- return (x == that.x) && (y == that.y);
- }
- }
- class State {
- Coordinate head, tail;
- Dir dir;
- public State(Coordinate _head, Coordinate _tail, Dir _dir) {
- head = _head;
- tail = _tail;
- dir = _dir;
- }
- @Override
- public boolean equals(Object that) {
- if (this == that) {
- return true;
- }
- if (that == null) {
- return false;
- }
- if (this.getClass() != that.getClass()) {
- return false;
- }
- State thatState = (State) that;
- if (!head.equals(thatState.head)) {
- return false;
- }
- if (!tail.equals(thatState.tail)) {
- return false;
- }
- return dir == thatState.dir;
- }
- @Override
- public int hashCode() {
- int dirVal = 1;
- if (dir == Dir.DOWN) {
- dirVal = 2;
- }
- return dirVal + head.x + head.y + tail.x + tail.y;
- }
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement