[leetcode] 542. 01 Matrix (Medium)

给予一个矩阵,矩阵有1有0,计算每一个1到0需要走几步,只能走上下左右。

解法一:

利用dp,从左上角遍历一遍,再从右下角遍历一遍,dp存储当前位置到0的最短距离。

十分粗心的搞错了col和row,改了半天…………

Runtime: 132 ms, faster than 98.88% of C++ online submissions for 01 Matrix.

class Solution
{
public:
  vector<vector<int>> updateMatrix(vector<vector<int>> &matrix)
  {
    if (matrix.size() == 0 || matrix[0].size() == 0)
      return matrix;

    int n;
    int m;
    n = matrix.size();
    m = matrix[0].size();
    int rangeNum = n + m;
    vector<vector<int>> dis(n, vector<int>(m, 0));

    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
      {
        if (matrix[i][j] == 0)
          dis[i][j] = 0;
        else
        {
          int up = (i > 0) ? dis[i - 1][j] : rangeNum;
          int left = (j > 0) ? dis[i][j - 1] : rangeNum;
          dis[i][j] = min(left, up) + 1;
        }
      }

    for (int i = n - 1; i >= 0; i--)
      for (int j = m - 1; j >= 0; j--)
      {
        if (matrix[i][j] == 0)
          dis[i][j] = 0;
        else
        {
          int right = (j + 1) < m ? dis[i][j + 1] : rangeNum;
          int down = (i + 1) < n ? dis[i + 1][j] : rangeNum;
          dis[i][j] = min(min(right, down) + 1, dis[i][j]);
        }
      }
    return dis;
  }
};

解法二:

BFS

class Solution
{
private:
  bool isValid(int m, int n, int x, int y)
  {
    return x >= 0 && y >= 0 && x < m && y < n;
  }

  int getShortestDistance(int m, int n, int x, int y, vector<vector<int>> &distance)
  {
    int result = distance[x][y];

    if (isValid(m, n, x, y + 1) && distance[x][y + 1] != INT_MAX)
    {
      result = min(result, 1 + distance[x][y + 1]);
    }
    if (isValid(m, n, x, y - 1) && distance[x][y - 1] != INT_MAX)
    {
      result = min(result, 1 + distance[x][y - 1]);
    }
    if (isValid(m, n, x + 1, y) && distance[x + 1][y] != INT_MAX)
    {
      result = min(result, 1 + distance[x + 1][y]);
    }
    if (isValid(m, n, x - 1, y) && distance[x - 1][y] != INT_MAX)
    {
      result = min(result, 1 + distance[x - 1][y]);
    }
    return result;
  }

public:
  vector<vector<int>> updateMatrix(vector<vector<int>> &matrix)
  {
    int m = matrix.size();
    int n = matrix[0].size();
    vector<vector<int>> distance(m, vector<int>(n, INT_MAX));
    queue<pair<int, int>> visit;

    for (int i = 0; i < m; i++)
    {
      for (int j = 0; j < n; j++)
      {
        if (matrix[i][j] == 0)
        {
          distance[i][j] = 0;
          visit.push(make_pair(i, j + 1));
          visit.push(make_pair(i, j - 1));
          visit.push(make_pair(i + 1, j));
          visit.push(make_pair(i - 1, j));
        }
      }
    }

    while (!visit.empty())
    {
      pair<int, int> cur = visit.front();
      visit.pop();
      int x = cur.first;
      int y = cur.second;

      if (isValid(m, n, x, y))
      {
        int shortestD = getShortestDistance(m, n, x, y, distance);
        if (shortestD < distance[x][y])
        {
          distance[x][y] = shortestD;
          visit.push(make_pair(x, y + 1));
          visit.push(make_pair(x, y - 1));
          visit.push(make_pair(x + 1, y));
          visit.push(make_pair(x - 1, y));
        }
      }
    }
    return distance;
  }
};

原文地址:https://www.cnblogs.com/ruoh3kou/p/10016396.html

时间: 2024-09-30 04:44:19

[leetcode] 542. 01 Matrix (Medium)的相关文章

LeetCode 542. 01 Matrix

输入:只包含0,1的矩阵 输出:元素1到达最近0的距离 算法思想:广度优先搜索. 元素为0为可达区域,元素为1为不可达区域,我们的目标是为了从可达区域不断地扩展至不可达区域,在扩展的过程中,也就计算出了这些不可达区域到达最近可达区域的距离. 每个可达元素都记录了到当前位置的距离,因此在后续的遍历中,如果是经由当前节点到达的下一节点,这个距离会被累加. 1 #include <iostream> 2 #include <vector> 3 #include <queue>

【LeetCode】542. 01 Matrix

Problem 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 1: Input: 0 0 0 0 1 0 0 0 0 Output: 0 0 0 0 1 0 0 0 0 Example 2: Input: 0 0 0 0 1 0 1 1 1

Leetcode 542.01矩阵

01矩阵 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 0 1 0 0 0 0 示例 2: 输入: 0 0 0 0 1 0 1 1 1 输出: 0 0 0 0 1 0 1 2 1 注意: 给定矩阵的元素个数不超过 10000. 给定矩阵中至少有一个元素是 0. 矩阵中的元素只在四个方向上相邻: 上.下.左.右. 思路 先把所有0入队,把1置为MAX_VALUE,然

542. 01 Matrix

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 1: Input: 0 0 0 0 1 0 0 0 0 Output: 0 0 0 0 1 0 0 0 0 Example 2: Input: 0 0 0 0 1 0 1 1 1 Output: 0 0 0 0 1 0

LeetCode——542. 01 矩阵

给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 0 1 0 0 0 0 示例 2: 输入: 0 0 0 0 1 0 1 1 1 输出: 0 0 0 0 1 0 1 2 1 注意: 给定矩阵的元素个数不超过 10000. 给定矩阵中至少有一个元素是 0. 矩阵中的元素只在四个方向上相邻: 上.下.左.右. 一 我们可以首先遍历一次矩阵,将值为0的点都存入queue,将

Leetcode 542:01 矩阵 01

Leetcode 542:01 矩阵 01 Matrix### 题目: 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 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. 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出

Leetcode 54. Spiral Matrix &amp; 59. Spiral Matrix II

54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5] Example 2: Input:

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

LeetCode:Spiral Matrix - 螺旋输出矩阵中的元素

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix/ 3.题目内容 英文:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 中文:给出一个m行n列的矩阵,以螺旋顺序返回矩阵中的所有元素. 例如:现有矩阵如下: [  [ 1,