Untitled
unknown
java
3 years ago
2.3 kB
11
Indexable
package pl.amen;
import java.awt.*;
import java.util.Stack;
public class Solution {
public static void solution(int[][] maze) {
int[][] visited = new int[maze.length][maze.length];
Stack<JumpState> jumpStateStack = new Stack<>();
jumpStateStack.push(new JumpState(new Point(0, 0), 1));
while (!jumpStateStack.isEmpty()) {
JumpState currentState = jumpStateStack.pop();
if (currentState.point.x == maze.length - 1 && currentState.point.y == maze.length - 1) {
System.out.printf("found it, length = " + currentState.length);
break;
}
if (((currentState.point.x + 1) < maze.length) && maze[currentState.point.x + 1][currentState.point.y] == 0) {
jumpStateStack.push(new JumpState(new Point(currentState.point.x + 1, currentState.point.y), currentState.length + 1));
}
if (((currentState.point.x - 1) >= 0) && maze[currentState.point.x - 1][currentState.point.y] == 0) {
jumpStateStack.push(new JumpState(new Point(currentState.point.x - 1, currentState.point.y), currentState.length + 1));
}
if (((currentState.point.y + 1)) < maze.length && maze[currentState.point.x][currentState.point.y + 1] == 0) {
jumpStateStack.push(new JumpState(new Point(currentState.point.x + 1, currentState.point.y + 1), currentState.length + 1));
}
if (((currentState.point.y - 1) >= 0) && maze[currentState.point.x][currentState.point.y - 1] == 0) {
jumpStateStack.push(new JumpState(new Point(currentState.point.x + 1, currentState.point.y - 1), currentState.length + 1));
}
}
System.out.println("No exit");
}
public static void main(String[] args) {
Solution.solution(new int[][]{{0, 0, 0, 0, 0, 0}, {1, 1, 1, 1, 1, 0}, {0, 0, 0, 0, 0, 0}, {0, 1, 1, 1, 1, 1}, {0, 1, 1, 1, 1, 1}, {0, 0, 0, 0, 0, 0}});
}
public static class JumpState {
Point point;
int length;
public JumpState(Point point, int length) {
this.point = point;
this.length = length;
}
}
}
Editor is loading...