Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
5.1 kB
10
Indexable
Never
package pl.amen;

import java.awt.*;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class Solution {
    public static void solution(int[][] maze) {
        boolean[][] visited = new boolean[maze.length][maze.length];
        Queue<JumpState> jumpStateStack = new LinkedList<>();
        jumpStateStack.add(new JumpState(new Point(0, 0), 1, false));


        while (!jumpStateStack.isEmpty()) {
//            printMaze(maze, visited);
            JumpState currentState = jumpStateStack.poll();
            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)) {
//                if (!visited[currentState.point.x + 1][currentState.point.y]) {
                if (maze[currentState.point.x + 1][currentState.point.y] == 1 && !currentState.wallRemoved) {
                    jumpStateStack.add(new JumpState(new Point(currentState.point.x + 1, currentState.point.y), currentState.length + 1, true));
                }
                if (maze[currentState.point.x + 1][currentState.point.y] == 0) {
                    jumpStateStack.add(new JumpState(new Point(currentState.point.x + 1, currentState.point.y), currentState.length + 1, currentState.wallRemoved));
                }
//                }
            }
            if (((currentState.point.y + 1)) < maze.length) {
//                if (!visited[currentState.point.x][currentState.point.y + 1]) {
                if (maze[currentState.point.x][currentState.point.y + 1] == 1 && !currentState.wallRemoved) {
                    jumpStateStack.add(new JumpState(new Point(currentState.point.x, currentState.point.y + 1), currentState.length + 1, true));
                }
                if (maze[currentState.point.x][currentState.point.y + 1] == 0) {
                    jumpStateStack.add(new JumpState(new Point(currentState.point.x, currentState.point.y + 1), currentState.length + 1, currentState.wallRemoved));
                }
//                }
            }
            if (((currentState.point.x - 1) >= 0)) {
//                if (!visited[currentState.point.x - 1][currentState.point.y]) {
                if (maze[currentState.point.x - 1][currentState.point.y] == 1 && !currentState.wallRemoved) {
                    jumpStateStack.add(new JumpState(new Point(currentState.point.x - 1, currentState.point.y), currentState.length + 1, true));
                }
                if (maze[currentState.point.x - 1][currentState.point.y] == 0) {
                    jumpStateStack.add(new JumpState(new Point(currentState.point.x - 1, currentState.point.y), currentState.length + 1, currentState.wallRemoved));
                }
//                }
            }
            if (((currentState.point.y - 1) >= 0)) {
//                if (!visited[currentState.point.x][currentState.point.y - 1]) {
                if (maze[currentState.point.x][currentState.point.y - 1] == 1 && !currentState.wallRemoved) {
                    jumpStateStack.add(new JumpState(new Point(currentState.point.x, currentState.point.y - 1), currentState.length + 1, true));
                }
                if (maze[currentState.point.x][currentState.point.y - 1] == 0) {
                    jumpStateStack.add(new JumpState(new Point(currentState.point.x, currentState.point.y - 1), currentState.length + 1, currentState.wallRemoved));
                }
//                }
            }
        }
        System.out.println("No exit");
    }

    public static void printMaze(int[][] maze, boolean[][] visited) {
        for (int i = 0; i < maze.length; i++) {
            for (int j = 0; j < maze[i].length; j++) {
                System.out.print("" + maze[i][j]);
            }
            System.out.println();
        }
        System.out.println();
        for (int i = 0; i < visited.length; i++) {
            for (int j = 0; j < visited[i].length; j++) {
                System.out.print("" + (visited[i][j] ? '1' : '0'));
            }
            System.out.println();
        }
        System.out.println();
        System.out.println();
    }

    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;
        boolean wallRemoved;

        public JumpState(Point point, int length, boolean wallRemoved) {
            this.point = point;
            this.length = length;
            this.wallRemoved = wallRemoved;
        }
    }
}