forked from Nanduag0/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAs_Far_From_Land_As_Possible.txt
59 lines (56 loc) · 1.43 KB
/
As_Far_From_Land_As_Possible.txt
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
57
58
59
class Solution {
public:
int maxDistance(vector<vector<int>>& grid)
{
queue<pair<int,int>> que;
for(int i=0;i<grid.size();i++)
{
for(int j=0;j<grid[i].size();j++)
{
if(grid[i][j])
{
que.push(make_pair(i,j));
grid[i][j]=0;
}
else
grid[i][j]=INT_MAX;
}
}
int n=grid.size();
if(que.empty() || que.size()== n*n) return -1;
int maxim=-1;
vector<vector<int>> vec = {{-1,0},{1,0},{0,1},{0,-1}};
int dis=0;
while(!que.empty())
{
pair<int,int> p=que.front();
que.pop();
int a=p.first;
int b=p.second;
for(auto it : vec)
{
int ao=a+it[0];
int bo=b+it[1];
if(ao>=0 && ao<grid.size() && bo>=0 && bo<grid[0].size())
{
if(grid[ao][bo]==INT_MAX)
{
que.push({ao,bo});
grid[ao][bo]=grid[a][b]+1;
}
}
}
}
int ans = 0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
//cout<<grid[i][j]<<' ';
ans = max(ans,grid[i][j]);
}
//cout<<endl;
}
return ans;
}
};