亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

[LeetCode] 490. The Maze (BFS/DFS)

smartlion / 939人閱讀

Problem

There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left or right, but it won"t stop rolling until hitting a wall. When the ball stops, it could choose the next direction.

Given the ball"s start position, the destination and the maze, determine whether the ball could stop at the destination.

The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes.

Example 1:

Input 1: a maze represented by a 2D array

0 0 1 0 0
0 0 0 0 0
0 0 0 1 0
1 1 0 1 1
0 0 0 0 0

Input 2: start coordinate (rowStart, colStart) = (0, 4)
Input 3: destination coordinate (rowDest, colDest) = (4, 4)

Output: true

Explanation: One possible way is : left -> down -> left -> down -> right -> down -> right.

Example 2:

Input 1: a maze represented by a 2D array

0 0 1 0 0
0 0 0 0 0
0 0 0 1 0
1 1 0 1 1
0 0 0 0 0

Input 2: start coordinate (rowStart, colStart) = (0, 4)
Input 3: destination coordinate (rowDest, colDest) = (3, 2)

Output: false

Explanation: There is no way for the ball to stop at the destination.

Note:

There is only one ball and one destination in the maze.
Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
The given maze does not contain border (like the red rectangle in the example pictures), but you could assume the border of the maze are all walls.
The maze contains at least 2 empty spaces, and both the width and height of the maze won"t exceed 100.

Solution - DFS
class Solution {
    public boolean hasPath(int[][] maze, int[] start, int[] destination) {
        int m = maze.length, n = maze[0].length;
        boolean[][] visited = new boolean[m][n];
        return dfs(maze, visited, start, destination);
    }
    private boolean dfs(int[][] maze, boolean[][] visited, int[] start, int[] destination) {
        int row = start[0], col = start[1];
        
        //check boundaries and if the point visited before
        if (row < 0 || row >= maze.length || col < 0 || col >= maze[0].length || visited[row][col]) return false;
        
        //mark as a visited stop point
        visited[row][col] = true;
        
        //if stop point is destination => true
        if (row == destination[0] && col == destination[1]) return true;
        
        //DFS on four directions
        int[][] dirs = new int[][]{{1,0},{-1,0},{0,1},{0,-1}};
        for (int i = 0; i < 4; i++) {
            int x = row, y = col;
            
            //rolling until out or hit the wall 
            while (x >= 0 && x < maze.length && y >= 0 && y < maze[0].length && maze[x][y] != 1) {
                x += dirs[i][0];
                y += dirs[i][1];
            }
            
            //back to the stop point
            x -= dirs[i][0];
            y -= dirs[i][1];
            
            //start another dfs from the stop point
            if (dfs(maze, visited, new int[]{x, y}, destination)) return true;
        }
        return false;
    }
}
Solution - BFS
class Solution {
    public boolean hasPath(int[][] maze, int[] start, int[] destination) {
        int m = maze.length, n = maze[0].length;
        Deque queue = new ArrayDeque<>();
        boolean[][] visited = new boolean[m][n];
        queue.offer(start);
        while (!queue.isEmpty()) {
            int[] cur = queue.poll();
            int row = cur[0], col = cur[1];
            if (row == destination[0] && col == destination[1]) return true;
            if (visited[row][col]) continue;
            visited[row][col] = true;
            int[][] dirs = new int[][]{{1,0},{-1,0},{0,1},{0,-1}};
            for (int[] dir: dirs) {
                int x = row;
                int y = col;
                while (x >= 0 && x < maze.length && y >= 0 && y < maze[0].length && maze[x][y] == 0) {
                    x += dir[0];
                    y += dir[1];
                }
                x -= dir[0];
                y -= dir[1];
                queue.offer(new int[]{x, y});
            }
        }
        return false;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/72454.html

相關(guān)文章

  • 490. The Maze && 505. The Maze II

    摘要:題目鏈接和上一題不一樣的是這道題要求最短的路徑,普通的遍歷和都是可以做的,但是求最短路徑的話還是用。這里相當(dāng)于每個(gè)點(diǎn)有至多條相連,每條的就是到墻之前的長(zhǎng)度。 490. The Maze 題目鏈接:https://leetcode.com/problems... 又是圖的遍歷問(wèn)題,就是簡(jiǎn)單的遍歷,所以dfs和bfs都可以做,復(fù)雜度也是一樣的。這道題要求球不能停下來(lái),即使碰到destina...

    BoYang 評(píng)論0 收藏0
  • [LintCode/LeetCode] Clone Graph [BFS/DFS]

    摘要:開(kāi)始看這道題目的時(shí)候,沒(méi)有看懂和的作用。然后對(duì)這個(gè)放入的結(jié)點(diǎn)開(kāi)始操作遍歷的所有,當(dāng)前遍歷到的的叫做。當(dāng)完成,則中沒(méi)有新的結(jié)點(diǎn)了,退出循環(huán)。返回在中更新過(guò)的,結(jié)束。 Problem Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. We use #...

    fredshare 評(píng)論0 收藏0
  • LeetCode 133:克隆圖 Clone Graph

    摘要:解題思路涉及到圖的遍歷無(wú)非就是深度優(yōu)先搜索廣度優(yōu)先搜索,可以先看前幾日的這篇文章就需要借助隊(duì)列實(shí)現(xiàn),可以借助棧也可以直接用遞歸實(shí)現(xiàn)。 題目: 給定無(wú)向連通圖中一個(gè)節(jié)點(diǎn)的引用,返回該圖的深拷貝(克?。D中的每個(gè)節(jié)點(diǎn)都包含它的值 val(Int) 和其鄰居的列表(list[Node])。 Given a reference of a node in a connected undirec...

    Simon 評(píng)論0 收藏0
  • The Maze II

    摘要:題目鏈接一般這種題,給一個(gè)起點(diǎn),給一個(gè)終點(diǎn),最后要求最短的路徑,都是用來(lái)解。的圖不是很大,也能。 The Maze II 題目鏈接:https://leetcode.com/contest/...一般這種題,給一個(gè)起點(diǎn),給一個(gè)終點(diǎn),最后要求最短的路徑,都是用bfs來(lái)解。 public class Solution { public String findShortestWay(...

    luffyZh 評(píng)論0 收藏0
  • BFS,DFS 算法原理及js實(shí)現(xiàn)

    1. 說(shuō)明 本文所有的算法嚴(yán)格按照《算法導(dǎo)論》,本文將詳細(xì)的對(duì)BFS和DFS進(jìn)行分析,并提供算法的 js 實(shí)現(xiàn),同時(shí)會(huì)對(duì)創(chuàng)建鏈表的方式進(jìn)行優(yōu)化 2. 圖的表示 圖的表示分為對(duì)頂點(diǎn)集 V 的表示和對(duì)邊集 E 的表示,這里的重點(diǎn)是如何表示邊,邊的表示分為鄰接矩陣和鄰接鏈表這兩種表示方法,鄰接矩陣適合表示邊稠密的圖,其消耗空間為|V|*|V|,如果是無(wú)向圖,則可以用上三角矩陣或者下三角矩陣來(lái)表示,是空間...

    劉德剛 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<