原题链接在这里: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).

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 and C_{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 value grid[0][0])
  • C_k is at location (N-1, N-1) (ie. has value grid[N-1][N-1])
  • If C_i is located at (r, c), then grid[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. 1 <= grid.length == grid[0].length <= 100
  2. grid[r][c] is 0 or 1

题解:

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的更多相关文章

  1. 【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- ...

  2. [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 ...

  3. [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 ...

  4. LeetCode Longest Increasing Path in a Matrix

    原题链接在这里:https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ Given an integer matrix, ...

  5. [LeetCode] 864. Shortest Path to Get All Keys 获得所有钥匙的最短路径

    We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@& ...

  6. LeetCode 847. Shortest Path Visiting All Nodes

    题目链接:https://leetcode.com/problems/shortest-path-visiting-all-nodes/ 题意:已知一条无向图,问经过所有点的最短路径是多长,边权都为1 ...

  7. [Leetcode]847. Shortest Path Visiting All Nodes(BFS|DP)

    题解 题意 给出一个无向图,求遍历所有点的最小花费 分析 1.BFS,设置dis[status][k]表示遍历的点数状态为status,当前遍历到k的最小花费,一次BFS即可 2.使用DP 代码 // ...

  8. 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) ...

  9. leetcode 847. Shortest Path Visiting All Nodes 无向连通图遍历最短路径

    设计最短路径 用bfs 天然带最短路径 每一个状态是 当前的阶段 和已经访问过的节点 下面是正确但是超时的代码 class Solution: def shortestPathLength(self, ...

随机推荐

  1. 科大讯飞语音芯片xfs5152CE,分享遇到的一些坑

    首先 芯片手册的I2C地址是写地址,是8位的,真正的地址是7位地址,应该是0x40,最低位是读写位,读置1,为0x81,写置0,为0x80. 如果是模拟I2C倒无所谓,最坑的是我用的是寄存器,所以必须 ...

  2. Prometheus 配置采集目标

    Prometheus 配置采集目标 1.根据配置的任务(job)以http/s周期性的收刮(scrape/pull)2.指定目标(target)上的指标(metric).目标(target)3.可以以 ...

  3. Python列表(list)所有元素的同一操作

    针对很普遍的每个元素的操作会遍历每个元素进行操作. 这里给出了几种写法,列表每个元素自增等数学操作同理: 示例:整形列表ilist加1个数.元素类型转字符串: ilist = [1, 2, 3, 10 ...

  4. Dapper安装与使用

    1.VS2015直接使用nuget包搜索Dapper,安装时报错:显示版本不兼容. 于是使用命令安装dapper低版本.  步骤:  打开项目,vs工具---Nuget包管理器--程序包管理器控制台 ...

  5. Wireshark教程之二:Wireshark捕获数据分析

    使用 Wireshark 选择需要抓包的网络方式,并设置过滤器条件,当有数据通信后即可抓到对应的数据包,这里将分析其每一帧数据包的结构. 以HTTP协议为例,一帧数据包一般包括以下几个部分: Fram ...

  6. 新版GRANAFA K8S插件 K8S NODE 图表不显示问题解决方法

    原文:https://www.wchao.site/archives/granafa-k8s 其他参考:https://blog.csdn.net/bbwangj/article/details/82 ...

  7. k8s+jenkins(DevOps全流程)

    k8s集群搭建是参照别人的,链接找不到了.需要5台机器,3主2从,主最少搭建3台才能实现高可用. 流程jenkins打包代码 >> 把打包代码生成镜像 >> 备份镜像库旧镜像 ...

  8. spring整合MyBatis思路

    目录 整合目标 需要的jar 整合思路 加入配置文件 整合目标 控制层采用springMVC.持久层使用mybatis实现. 需要的jar spring(包括springmvc) mybatis my ...

  9. 记一下python的method resolution order(MRO)机制

    一直用python都是拿着cookbook和库的文档直接撸,很少会把细节过得那么彻底,遇到问题才会翻文档. 今天看到这个例子的时候我突然触及了我的盲区,我不确定这样的继承层级调用super.foo() ...

  10. QTGraphics-View拖拽以及鼠标指针操作

    因为QGraphicsView继承自QWidget,它也提供了像QWidget那样的拖拽功能. 另外,为了方便,Graphics View框架也为场景以及每个item提供拖拽支持.当视图接收到拖拽事件 ...