LintCode第974题-求矩阵各节点的最短路径(以0为标准)

描述

给定一个由0和1组成的矩阵,求每个单元格最近的0的距离。

两个相邻细胞之间的距离是1。

给定矩阵的元素数不超过10,000。

在给定的矩阵中至少有一个0。

单元格在四个方向上相邻:上,下,左和右。

样例

例1:

复制代码
输入:
[[0,0,0],
 [0,0,0],
 [0,0,0],
 [0,0,0],
 [0,0,0]]
输出:
[[0,0,0],
 [0,0,0],
 [0,0,0],
 [0,0,0],
 [0,0,0]]

例2:

复制代码
复制代码
输入:
[[0,1,0,1,1],
 [1,1,0,0,1],
 [0,0,0,1,0],
 [1,0,1,1,1],
 [1,0,0,0,1]]
输出:
[[0,1,0,1,2],
 [1,1,0,0,1],
 [0,0,0,1,0],
 [1,0,1,1,1],
 [1,0,0,0,1]]

本题主要考察广度优先搜索

如需更快更简洁的算法请跳至思路2

思路1:

思路1虽然也是广度优先搜索算法 但是是单源的广度优先搜索算法 即逐个继续遍历扩展其周围的元素

外层循环为每一层,内层循环为每一层的当前输入值

然后通过两个变量 path来记录路径的长度 队列来记录为未满足为0的位置索引 每次判断但凡队列的点相邻但凡没有一个符合条件就先让path+1

然后记录所有不符合的点 直至循环出相邻有0的点 因为题目已经告知给定的矩阵至少有一个0

这就是每一个元素的遍历

最后将所有元素都这样执行一遍就可以得到每一个单元距离0的距离

即简要理解为针对每个 1 向外找最近 0

代码如下:

import java.util.*;

public class Solution {

/**

* @param matrix: a 0-1 matrix

* @return: return a matrix

*/

public int\[\]\[\] updateMatrix(int\[\]\[\] matrix) {

int m = matrix.length;

int n = matrix0.length;

int\[\]\[\] pathMatrix = new intmn; // 最终结果矩阵

int path; // 当前单元格的路径

Queue<int\[\]> integerQueue = new LinkedList<>(); // 存坐标的队列

// 遍历每个单元格

for (int i = 0; i < m; i++) {

for (int j = 0; j < n; j++) {

if (matrixij == 0) {

pathMatrixij = 0;

} else {

// BFS 查找最近的 0

boolean\[\]\[\] visited = new booleanmn;

integerQueue.clear();

integerQueue.offer(new int\[\]{i, j});

visitedij = true;

path = 0;

boolean found = false;

while (!integerQueue.isEmpty() && !found) {

int size = integerQueue.size();

path++; // 每扩展一层,路径 +1

for (int q = 0; q < size; q++) {

int\[\] pos = integerQueue.poll();

int x = pos0;

int y = pos1;

int\[\]\[\] dirs = {{-1,0},{1,0},{0,-1},{0,1}}; // 上下左右

for (int\[\] dir : dirs) {

int nx = x + dir0;

int ny = y + dir1;

if (nx >= 0 && nx < m && ny >= 0 && ny < n && !visitednxny) {

if (matrixnxny == 0) {

pathMatrixij = path;

found = true;

break;

} else {

integerQueue.offer(new int\[\]{nx, ny});

visitednxny = true;

}

}

}

if (found) break;

}

}

}

}

}

return pathMatrix;

}

}

时间复杂度为:O(m × n)^2

思路2:多源广度优先算法

同时从多个起点出发进行 BFS,也就是说:
不是一个一个来,而是全部起点一起"向外一层层扩散"

即所有 0 一起扩散 访问一次就是最短路径 无冗余访问 即类似于同步水波扩散

需要注意的是

m代表的是行数

n代表的是列数

代码如下:

public class Solution {

public int\[\]\[\] updateMatrix(int\[\]\[\] matrix) {

int m = matrix.length;

int n = matrix0.length;

int\[\]\[\] pathMatrix = new intmn;

boolean\[\]\[\] visited = new booleanmn;

Queue<int\[\]> integerQueue = new LinkedList<>();

//注意这里用LinkList 因为效率高 入队出队都是O(1) 而且LinkList实现了Deque,Deque又继承了队列,所以Queue可以直接用其实现类 LinkList.

// 将所有为0的点入队,作为BFS的起点

for (int y = 0; y < m; y++) {

for (int x = 0; x < n; x++) {

if (matrixyx == 0) {

integerQueue.offer(new int\[\]{y, x});

visitedyx = true;

}

}

}

// 上下左右

int\[\]\[\] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

while (!integerQueue.isEmpty()) {

int\[\] point = integerQueue.poll();

int y = point0, x = point1;

for (int\[\] dir : directions) {

int newY = y + dir0;

int newX = x + dir1;

if (newY >= 0 && newY < m && newX >= 0 && newX < n && !visitednewYnewX) {

pathMatrixnewYnewX = pathMatrixyx + 1;

visitednewYnewX = true;

integerQueue.offer(new int\[\]{newY, newX});

}

}

}

return pathMatrix;

}

}

相关推荐
倒头就睡的小比特2 天前
算法竞赛C++常用的STL
c++·算法
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
猎头南楼2 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
旖旎夜光2 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark2 天前
大规模并行计算中的负载均衡算法研究4
算法
Because_of_Her12 天前
并查集-听课笔记
笔记·算法·并查集
码流子2 天前
高速公路安全监测实践:碰撞监测预警+物联网底座,从感知到处置的闭环
大数据·人工智能·物联网·算法·架构
another heaven2 天前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法
wzdark2 天前
从算法设计模式看编程思维的抽象能力4
算法
2601_962218612 天前
万象生鲜系统称重自动多退少补算法解决生鲜非标品痛点
大数据·数据库·人工智能·python·算法