The sword refers to offer107: the distance in the matrix

Question:
Given a matrix mat consisting of 0s and 1s, please output a matrix of the same size, where each grid is the distance from the corresponding position element in mat to the nearest 0.
The distance between two adjacent elements is 1.
Example 1:
insert image description here
Input: mat = [[0,0,0],[0,1,0],[0,0,0]]
Output: [[0,0,0],[0,1,0] ,[0,0,0]]
Example 2:
insert image description here
Input: mat = [[0,0,0],[0,1,0],[1,1,1]]
Output: [[0,0,0 ],[0,1,0],[1,2,1]]
Analysis:
Since this problem is related to the closest distance of the unweighted graph, it can be solved by applying breadth-first traversal search. In other words, the problem is Find the distance of each grid from the nearest 0, so you can add all 0s as initial nodes to the queue, and then use the node with a value of 0 as the starting point to do a breadth-first search, if you reach a grid after d steps, then The distance of the grid from the nearest 0 is d.
Create a two-dimensional array dists with the same size as the input matrix, which is used to record the final result, that is, the distance of each grid from the nearest 0. If matrix[i][j] is 0, then the grid force is the distance to the nearest 0 Naturally, it is 0. If matrix[i][j] is 1, first initialize dists[i][j] with the largest integer value, and then search for the corresponding node and then update its value.
If the grid with coordinates (r, s) has been reached before, then the value of dists[r][s] must not be greater than dist+1, because this is the breadth-first search used, and the second breadth-first search can guarantee that from When the starting node reaches any node, it must follow the shortest path. When reaching the grid with coordinates (r, s) for the first time, the value of dists[r][s] recorded must be from the grid with the value 0 to the grid. Therefore, when the grid of (r,s) is reached again, the value of dist[r][s]>dist+1 is false. By comparing the distances, repeated visits to a grid can be avoided. The time complexity of the algorithm is O(mn).

Code:

package com.kuang;

import java.util.LinkedList;
import java.util.Queue;

public class UpdateMatrix {
    
    
    public int[][] updateMatrix(int[][] mat) {
    
    
        int rows = mat.length;
        int cols = mat[0].length;
        int[][] dists = new int[rows][cols];
        Queue<int[]> queue = new LinkedList<>();
        for (int i = 0; i < rows; i++) {
    
    
            for (int j = 0; j < cols; j++) {
    
    
                if (mat[i][j] == 0){
    
    
                    queue.add(new int[]{
    
    i,j});
                    dists[i][j] = 0;
                }else {
    
    
                    dists[i][j] = Integer.MAX_VALUE;
                }
            }
        }
        int[][] dirs = {
    
    {
    
    -1,0},{
    
    1,0},{
    
    0,-1},{
    
    0,1}};
        while (!queue.isEmpty()){
    
    
            int[] pos = queue.remove();
            int dist = dists[pos[0]][pos[1]];
            for (int[] dir : dirs){
    
    
                int r = pos[0] + dir[0];
                int s = pos[1] + dir[1];
                if (r>=0&&s>=0&&r<rows&&s<cols){
    
    
                    if (dists[r][s] > dist +1){
    
    
                        dists[r][s] = dist+1;
                        queue.add(new int[]{
    
    r,s});
                    }
                }
            }
        }
        return dists;
    }
}

insert image description here

Guess you like

Origin blog.csdn.net/Jiaodaqiaobiluo/article/details/123848075
Recommended