-
Notifications
You must be signed in to change notification settings - Fork 481
/
1162.cpp
40 lines (37 loc) · 1.05 KB
/
1162.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
class Solution
{
public:
int maxDistance(vector<vector<int>>& grid)
{
queue<pair<int, int>> data;
int n = grid.size(), m = grid[0].size();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (grid[i][j]) data.emplace(i, j);
}
}
if (n * m == data.size() or 0 == data.size()) return -1;
int d[4][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}};
int res = 0;
while (!data.empty())
{
int dataLen = data.size();
for (int i = 0; i < dataLen; i++)
{
auto [u, v] = data.front();
data.pop();
for (auto [xd, yd] : d)
{
int x = u + xd, y = v + yd;
if (x < 0 or x >= n or y < 0 or y >= m or grid[x][y]) continue;
grid[x][y] = 1;
data.emplace(x, y);
}
}
res++;
}
return res - 1;
}
};