-
Notifications
You must be signed in to change notification settings - Fork 3
/
01-matrix.cpp
35 lines (30 loc) · 1.01 KB
/
01-matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//Runtime: 225 ms
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
int r = matrix.size();
int c = matrix[0].size();
vector<vector<int> > DP(r, vector<int>(c,INT_MAX));
for(int m=0;m<=1;m++)
{
for(int i = m?r-1:0;m?i>=0:i<r;m?i--:i++)
{
for(int j = m?c-1:0;m?j>=0:j<c;m?j--:j++)
{
if(matrix[i][j] == 0)
DP[i][j] = 0;
else
{
int up = i>0?DP[i-1][j]:INT_MAX;
int dn = i+1<r?DP[i+1][j]:INT_MAX;
int rt = j+1<c?DP[i][j+1]:INT_MAX;
int lt = j>0?DP[i][j-1]:INT_MAX;
if(up!=INT_MAX || dn != INT_MAX || rt != INT_MAX || lt != INT_MAX)
DP[i][j] = 1 + min(min(up,dn),min(rt,lt));
}
}
}
}
return DP;
}
};