题目来源于知识星球—英雄算法联盟,一月算法集训专题
前言
经过漫长的休息之后,在寒假以及新年到来之际再次刷起算法,培养自己的算法素养和算法思维,加油!今天只有一道题。
剑指 Offer II 107.矩阵中的距离(中等)
1.题目描述
2.解题思路
这道题看的是题解,目前还不太明白,后续会回过头来及时理解理解。
3.代码演示(C++)
class Solution
{
public:
const int dirs [4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
vector<vector<int>> updateMatrix(vector<vector<int>>& mat)
{
int INF = (int)1e9;
int Row = (int)mat.size();
int Col = (int)mat[0].size();
vector<vector<int>> dist(Row, vector<int>(Col, INF));
for (int r = 0; r < Row; r ++)
for (int c = 0; c < Col; c ++)
if (mat[r][c] == 0)
dist[r][c] = 0;
//----只能左上
for (int r = Row - 1; r > -1; r --)
{
for (int c = Col - 1; c > -1; c --)
{
dist[r][c] = min(dist[r][c], r+1<Row ? dist[r+1][c] + 1 : INF);
dist[r][c] = min(dist[r][c], c+1<Col ? dist[r][c+1] + 1 : INF);
}
}
//----只能左下
for (int r = 0; r < Row; r ++)
{
for (int c = Col - 1; c > -1; c --)
{
dist[r][c] = min(dist[r][c], 0<=r-1 ? dist[r-1][c] + 1 : INF);
dist[r][c] = min(dist[r][c], c+1<Col ? dist[r][c+1] + 1 : INF);
}
}
//----只能右上
for (int r = Row - 1; r > -1; r --)
{
for (int c = 0; c < Col; c ++)
{
dist[r][c] = min(dist[r][c], r+1<Row ? dist[r+1][c] + 1 : INF);
dist[r][c] = min(dist[r][c], 0<=c-1 ? dist[r][c-1] + 1 : INF);
}
}
//----只能右下
for (int r = 0; r < Row; r ++)
{
for (int c = 0; c < Col; c ++)
{
dist[r][c] = min(dist[r][c], 0<=r-1 ? dist[r-1][c] + 1 : INF);
dist[r][c] = min(dist[r][c], 0<=c-1 ? dist[r][c-1] + 1 : INF);
}
}
return dist;
}
};