美文网首页
[Java] 542. 01 Matrix

[Java] 542. 01 Matrix

作者: 葵sunshine | 来源:发表于2019-07-26 23:39 被阅读0次

Description

Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.
The distance between two adjacent cells is 1.

Example

记录矩阵中每个元素到其最近0的距离

Solution

法1:BFS

(看到这个题突然想到扫雷。。。

  1. 以0为中心向外扩散传播,第一层将0加入队列,第二层将0周围元素赋值为1加入队列,以此类推;
  2. 因为计算距离最小为0,位置为0的地方值一定还为0,所以首先可以考虑将1的位置赋值为整数最大值;
  3. matrix[r][c] <= matrix[cell[0]][cell[1]] + 1 说明此点已被遍历过;
class Solution {
    public int[][] updateMatrix(int[][] matrix) {
        // BFS  先将所有0的位置加入队列,将所有1的位置设为INF  
        int m = matrix.length;
        int n = matrix[0].length;
        
        Queue<int[]> queue = new LinkedList<>();
        for(int i = 0;i<m;i++){
            for(int j = 0;j<n;j++){
                if (matrix[i][j] == 0) {
                    queue.offer(new int[] {i, j});
                }
                else{
                    matrix[i][j] = Integer.MAX_VALUE;
                }
            }
        }
        
        int[][] dirs = {{-1,0},{1,0},{0,-1},{0,1}};  //四个方向
        
        while(!queue.isEmpty()){
            int[] cell = queue.poll();   //存进队列中的是坐标点
            for(int[] d : dirs){
                int r = cell[0]+d[0];
                int c = cell[1]+d[1];
                if(r >= m || r < 0 || c >= n || c < 0 || matrix[r][c] <= matrix[cell[0]][cell[1]] + 1)
                    continue;
                queue.add(new int[] {r,c});
                matrix[r][c] = matrix[cell[0]][cell[1]] + 1;
            }   
        }
        return matrix;
    }
}

另:BFS常用visited数组标记原来在队列中,现在已出队列的点,将此标记法也贴一下

    public int[][] updateMatrix(int[][] matrix) {
        // BFS  先将所有0的位置加入队列  visited标记是否访问过
        int m = matrix.length;
        int n = matrix[0].length;
        boolean[][] visited = new boolean[m][n];
        
        Queue<int[]> queue = new LinkedList<>();
        for(int i = 0;i<m;i++){
            for(int j = 0;j<n;j++){
                if (matrix[i][j] == 0) {
                    queue.offer(new int[] {i, j});
                    visited[i][j] = true;  //表示访问过了
                }
            }
        }
         
        int[][] dirs = {{-1,0},{1,0},{0,-1},{0,1}};  //四个方向
        
        while(!queue.isEmpty()){
            int[] cell = queue.poll();   //存进队列中的是坐标点
            for(int[] d : dirs){
                int r = cell[0]+d[0];
                int c = cell[1]+d[1];
                if(r >= m || r < 0 || c >= n || c < 0 ||visited[r][c] == true)  //遇到不合法的点或访问过的点,跳过
                    continue;
                queue.add(new int[] {r,c});  //即将以其为起点进行搜索
                matrix[r][c] = matrix[cell[0]][cell[1]] + 1;
                visited[r][c] = true;
            }   
        }
        return matrix;
    }
}
法2:动态规划

相关文章

  • [Java] 542. 01 Matrix

    Description Given a matrix consists of 0 and 1, find the ...

  • 542. 01 Matrix

    Given a matrix consists of 0 and 1, find the distance of ...

  • 542. 01 Matrix

    这道题不看答案,没想出思路,做题还是不够,看了bfs的思路,然后手写了一遍,记录一下。这道题从bfs的角度来看,思...

  • 2018-09-18 542. 01 Matrix

    题意:给你一个矩阵只包含元素0和1,求的一个矩阵,该矩阵在原矩阵为1的位置得出该元素距离最近的0的距离(仅能上下左...

  • LeetCode 542. 01 矩阵

    542. 01 矩阵 题目来源:https://leetcode-cn.com/problems/01-matri...

  • 3.Matrix Android ApkChecker

    Matrix-ApkChecker 的使用 java -jar ApkChecker.jar Matrix-Apk...

  • Lecture 03

    01. Matrix Multiplication (4 ways) 02. Inverse Matrix 03....

  • 542. 01 矩阵

    题目描述 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离。 两个相邻元素间的距离为 1 。 ...

  • 542. 01矩阵

    题目描述 给定一个由0和1组成的矩阵,找出每个元素到最近0的距离,相邻元素间的距离为1。例如:输入 输出 注意: ...

  • ARTS打卡第一周

    ARTS打卡第一周 Algorithm:每周至少做一个 leetcode 的算法题 542. 01 矩阵 Revi...

网友评论

      本文标题:[Java] 542. 01 Matrix

      本文链接:https://www.haomeiwen.com/subject/aijlrctx.html