LeetCode 1091. Shortest Path in Binary Matrix
原题链接在这里:https://leetcode.com/problems/shortest-path-in-binary-matrix/
题目:
In an N by N square grid, each cell is either empty (0) or blocked (1).
A clear path from top-left to bottom-right has length k
if and only if it is composed of cells C_1, C_2, ..., C_k
such that:
- Adjacent cells
C_i
andC_{i+1}
are connected 8-directionally (ie., they are different and share an edge or corner) C_1
is at location(0, 0)
(ie. has valuegrid[0][0]
)C_k
is at location(N-1, N-1)
(ie. has valuegrid[N-1][N-1]
)- If
C_i
is located at(r, c)
, thengrid[r][c]
is empty (ie.grid[r][c] == 0
).
Return the length of the shortest such clear path from top-left to bottom-right. If such a path does not exist, return -1.
Example 1:
Input: [[0,1],[1,0]]
Output: 2
![]()
Example 2:
Input: [[0,0,0],[1,1,0],[1,1,0]]
Output: 4
Note:
1 <= grid.length == grid[0].length <= 100
grid[r][c]
is0
or1
题解:
When doing BFS, for each level, mark the current size.
When polling current size node, pop up to next level.
If current polled node is bottom right node, return level.
Otherwise, for its 8 surrounding node, check if it is not visited nor block, add it to the queue.
Time Complexity: O(m*n). m = grid.length. n = grid[0].length.
Space: O(m*n).
AC Java:
class Solution {
public int shortestPathBinaryMatrix(int[][] grid) {
if(grid == null || grid.length == 0 || grid[0].length == 0){
return -1;
} int m = grid.length;
int n = grid[0].length;
if(grid[0][0] == 1 || grid[m-1][n-1] == 1){
return -1;
} LinkedList<int []> que = new LinkedList<>();
boolean [][] visited = new boolean[m][n];
visited[0][0] = true;
que.add(new int[]{0, 0}); int level = 1;
while(!que.isEmpty()){
int size = que.size();
while(size-->0){
int [] cur = que.poll();
if(cur[0] == m-1 && cur[1] == n-1){
return level;
} for(int i = -1; i<=1; i++){
for(int j = -1; j<=1; j++){
int x = cur[0]+i;
int y = cur[1]+j;
if(x<0 || x>=m || y<0 || y>=n || visited[x][y] || grid[x][y]==1){
continue;
} visited[x][y] = true;
que.add(new int[]{x, y});
}
}
} level++;
} return -1;
}
}
LeetCode 1091. Shortest Path in Binary Matrix的更多相关文章
- 【leetcode】1091. Shortest Path in Binary Matrix
题目如下: In an N by N square grid, each cell is either empty (0) or blocked (1). A clear path from top- ...
- [LeetCode] 847. Shortest Path Visiting All Nodes 访问所有结点的最短路径
An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...
- [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- LeetCode Longest Increasing Path in a Matrix
原题链接在这里:https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ Given an integer matrix, ...
- [LeetCode] 864. Shortest Path to Get All Keys 获得所有钥匙的最短路径
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@& ...
- LeetCode 847. Shortest Path Visiting All Nodes
题目链接:https://leetcode.com/problems/shortest-path-visiting-all-nodes/ 题意:已知一条无向图,问经过所有点的最短路径是多长,边权都为1 ...
- [Leetcode]847. Shortest Path Visiting All Nodes(BFS|DP)
题解 题意 给出一个无向图,求遍历所有点的最小花费 分析 1.BFS,设置dis[status][k]表示遍历的点数状态为status,当前遍历到k的最小花费,一次BFS即可 2.使用DP 代码 // ...
- LeetCode 1293. Shortest Path in a Grid with Obstacles Elimination
题目 非常简单的BFS 暴搜 struct Node { int x; int y; int k; int ans; Node(){} Node(int x,int y,int k,int ans) ...
- leetcode 847. Shortest Path Visiting All Nodes 无向连通图遍历最短路径
设计最短路径 用bfs 天然带最短路径 每一个状态是 当前的阶段 和已经访问过的节点 下面是正确但是超时的代码 class Solution: def shortestPathLength(self, ...
随机推荐
- windows版mysql安装
https://blog.csdn.net/ycxzuoxin/article/details/80908447
- Oracle查询所有字段另加两个拼接字段的操作
Oracle查询所有字段,再加两个字段拼接, select a.*,(SNO||SNAME) from TEST_STUDENT a; 同理,查询所有字段,其中两个字段求和:(SNO和SAGE都是NU ...
- Docker 安装 Redis, 搭建 Redis 环境
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...
- SetWindowLong函数GetWindowLong函数
这两个函数具体应用如下:SetWindowLong函数GetWindowLong函数 Delphi窗口化游戏 var Thwnd:HWND;//声明变量 句柄变量 devmodel1:DEVMODE; ...
- SQL根据指定节点ID获取所有父级节点和子级节点
--根据指定节点ID获取所有子节点-- WITH TEMP AS ( SELECT * FROM table_name WHERE Id=' --表的主键ID UNION ALL SELECT T0. ...
- SpringBoot,SSM和SSH
Springboot的概念: 是提供的全新框架,使用来简化Spring的初始搭建和开发过程,使用了特定的方式来进行配置,让开发人员不在需要定义样板化的配置.此框架不需要配置xml,依赖于想MAVEN这 ...
- JavaScript 运算符(Operator)
一.算数运算符 1.加法(+) 表示操作数相加: 处理特殊值规则: 如果两个操作数都是字符串,则将第二个操作数与第一个操作数拼接起来: 如果只有一个操作数是字符串,则将另一个操作数转换为字符串,然后 ...
- jvm学习笔记之class文件的加载、初始化
编写的java文件在要真正运行时,会首先被编译成 “.class"结尾的二进制文件,然后被虚拟机加载.那么在虚拟机中一个class文件要成为java实例,需要经历好几个步骤: 1.装载:装载 ...
- NumPy 之 ndarray 多维数组初识
why 回顾我的数据分析入门, 最开始时SPSS+EXCEL,正好15年初是上大一下的时候, 因为统计学的还蛮好的, SPSS傻瓜式操作,上手挺方便,可渐渐地发现,使用软件的最不好的地方是不够灵活, ...
- Golang: 读写之外的其他文件操作
在上一篇文章中,我们介绍了常用的文件读写操作,今天接着来研究一下,除了读写以外的其他常见文件操作. 一.创建目录: package main import ( "fmt" &quo ...