原题链接在这里: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. Lua函数声明与调用

    lua编程中,我们经常也会遇到函数的声明定义和调用. [1]lua中函数定义与调用的方法 lua有两种函数定义和调用的方法(本质都是用属性,方式不同而已): (1)点号形式 (2)冒号形式 两种方法的 ...

  2. JAVA WEB项目目录结构以及web应用部署的根目录,编译路径和项目根目录的区别

    本文链接:https://blog.csdn.net/l00149133/article/details/78984083 web应用部署的根目录,编译路径和项目的根目录有什么区别? 直接上例子: 你 ...

  3. 数据结构部分总结(c语言版)

    这是一个 /*此头文件适用于串其中包括最基本的函数操作 OK代表成功NO代表失败FS为特殊失败的标志 注:此头文件中的初始化使用'0'代表结束的 使用者可以根据需要自行改变,最后一 个函数为KMP算法 ...

  4. Akka-CQRS(11)- akka-http for http-web-service: Marshalling-数据序列化

    前面几篇讨论了关于gRPC方式的前后端连接集成方式.gRPC也是一个开放的标准,但讲到普及性就远远不及基于http/1.1协议的web-service了.特别是gRPC的前端编程还是有一定的门槛,所以 ...

  5. <面试题分享> 记两次58面试

    说明 来北京找工作,有个猎头看我的简历不错,帮我投了两个58同城的面试,投的都比较高,题也注重原理,较难,这里分享出来,给有需要的人和自己提个醒,保持空杯 面试题内容 2019.05.07 北京58企 ...

  6. K8S学习笔记之Grafana App for Kubernetes的配置

    Grafana有一套针对Kubernetes监控的APP,和Grafana-Zabbix App类似,但是配置咋一看比较麻烦,主要参数都是来自K8S. 这款APP的详细介绍请参考Grafana App ...

  7. mysql 中的 not like 另一种简化方法。

    第一种 not like 方法 select * from table where `zongbu` not like '%北京%' and `zongbu` not like '%上海%' and ...

  8. BZOJ3209: 花神的数论题(数位DP)

    题目: 3209: 花神的数论题 解析: 二进制的数位DP 因为\([1,n]\)中每一个数对应的二进制数是唯一的,我们枚举\(1\)的个数\(k\),计算有多少个数的二进制中有\(k\)个\(1\) ...

  9. Map List Set的区别

    Map List Map的主要区别如下: set List 是实现了Collection接口的子接口:Map本身就是一个接口: list数据存放时有序的,允许有重复元素:set数据存放时无序的,不允许 ...

  10. 【开发笔记】- 将MySQL数据库表中自增ID从0开始

    命令: 用于清空某表的数据 且让自增的id重新从0开始 truncate table 你的表名