-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasfarpossible.cpp
56 lines (55 loc) · 1.43 KB
/
asfarpossible.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<bits/stdc++.h>
using namespace std;
class Solution
{
public:
int maxDistance(vector<vector<int>> &grid)
{
int res = -1, n = grid.size(), m = grid[0].size();
vector<vector<int>> dp(n + 2, vector<int>(m + 2, 1e9));
bool p = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (grid[i - 1][j - 1] == 1)
{
dp[i][j] = 0;
p = 1;
}
else
{
dp[i][j] = dp[i][j - 1] + 1;
}
}
for (int j = m; j >= 0; j--)
{
dp[i][j] = min(dp[i][j], dp[i][j + 1] + 1);
}
}
if (!p)
return -1;
for (int j = 1; j <= m; j++)
{
for (int i = 1; i <= n; i++)
{
dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
}
for (int i = n; i >= 0; i--)
{
dp[i][j] = min(dp[i][j], dp[i + 1][j] + 1);
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (grid[i - 1][j - 1] == 0)
{
res = max(res, dp[i][j]);
}
}
}
return res;
}
};