Untitled
unknown
plain_text
3 years ago
2.9 kB
3
Indexable
package pl.amen; import java.awt.*; import java.util.Stack; public class Solution { public static void solution(int[][] maze) { boolean[][] visited = new boolean[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 (visited[currentState.point.x][currentState.point.y]) { continue; } visited[currentState.point.x][currentState.point.y] = true; 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) { if (!visited[currentState.point.x + 1][currentState.point.y]) { 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) { if (!visited[currentState.point.x - 1][currentState.point.y]) { 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) { if (!visited[currentState.point.x][currentState.point.y + 1]) { jumpStateStack.push(new JumpState(new Point(currentState.point.x, currentState.point.y + 1), currentState.length + 1)); } } if (((currentState.point.y - 1) >= 0) && maze[currentState.point.x][currentState.point.y - 1] == 0) { if (!visited[currentState.point.x][currentState.point.y - 1]) { jumpStateStack.push(new JumpState(new Point(currentState.point.x, 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...