-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1162.as-far-from-land-as-possible.cpp
More file actions
66 lines (63 loc) · 1.55 KB
/
Copy path1162.as-far-from-land-as-possible.cpp
File metadata and controls
66 lines (63 loc) · 1.55 KB
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
60
61
62
63
64
65
/*
* @lc app=leetcode id=1162 lang=cpp
*
* [1162] As Far from Land as Possible
*/
// @lc code=start
auto speedup = [](){
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
return 0;
}();
class Solution {
int length;
int move[4][2] = {
{-1,0},
{0, 1},
{1, 0},
{0,-1}
};
bool isValid(int first, int second) {
return first >= 0 && second >= 0 && first < length && second < length;
}
int bfs(vector<vector<int>>& grid, queue<tuple<int, int, int>>& q) {
int ans = 0;
while(q.size()) {
auto [ x, y, cnt ] = q.front();
// int x = get<0>(q.front());
// int y = get<1>(q.front());
// int cnt = get<2>(q.front());
q.pop();
if(cnt > ans) ans = cnt;
for(int i = 0; i < 4; ++i) {
if(isValid(x+move[i][0], y+move[i][1]) && !grid[x+move[i][0]][y+move[i][1]]) {
q.push(make_tuple(x+move[i][0], y+move[i][1], cnt+1));
grid[x+move[i][0]][y+move[i][1]] = 1;
}
}
}
return ans;
}
public:
int maxDistance(vector<vector<int>>& grid) {
queue<tuple<int, int, int>> q;
length = grid.size();
for(int i = 0; i < length; i++) {
for(int j = 0; j < length; j++) {
if(grid[i][j] == 1) {
q.push(make_tuple(i, j, 0));
}
}
}
if (q.size() == length * length || q.empty()) {
return -1;
}
return bfs(grid, q);
}
};
// Accepted
// 37/37 cases passed (77 ms)
// Your runtime beats 58.09 % of cpp submissions
// Your memory usage beats 43 % of cpp submissions (21.5 MB)
// @lc code=end