Facebook
From Speedy Pintail, 3 Years ago, written in Plain Text.
This paste is a reply to Untitled from Crippled Wigeon - go back
Embed
Viewing differences between Untitled and Re: Untitled
class Solution {
    public boolean hasPath(int[][] maze, int[] start, int[] destination) {
        int nr = maze.length;
        int nc = maze[0].length;
        boolean[][] visited = new boolean[nr][nc];
        int[][] dirs = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
        Queue queue = new LinkedList<>();
        queue.add(start);
        visited[start[0]][start[1]] = true;
        while (!queue.isEmpty()) {
            int[] idx = queue.poll();
            if (idx[0] == destination[0] && idx[1] == destination[1]) {
                return true;
            }
            for (int[] dir : dirs) {
                int x = idx[0] + dir[0];
                int y = idx[1] + dir[1];
                while (x >= 0 && y >= 0 && x < nr && y < nc &&
                      maze[x][y] == 0) {
                    x += dir[0];
                    y += dir[1];
                }
                if (!visited[x - dir[0]][y - dir[1]]) {
                    queue.add(new int[] {x - dir[0], y - dir[1]});
                    visited[x - dir[0]][y - dir[1]] = true;
                }
            }
        }
        return false;
    }
}

Replies to Re: Untitled rss

Title Name Language When
Re: Re: Untitled Thundering Mousedeer text 3 Years ago.