-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path675.cpp
More file actions
53 lines (53 loc) · 1.75 KB
/
675.cpp
File metadata and controls
53 lines (53 loc) · 1.75 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
class Solution {
public:
int dirs[5] = {-1,0,1,0,-1};
int bfs(vector<vector<int>>& forest, int sr, int sc, int er, int ec){
if(sr==er && sc==ec)
return 0;
int m = forest.size(), n = forest[0].size();
queue<pair<int,int>> q;
q.push({sr,sc});
vector<vector<int>> visited(m,vector<int>(n,0));
visited[sr][sc] = 1;
int step = 0;
while(!q.empty()){
step++;
int size = q.size();
while(size--){
auto curr = q.front();q.pop();
for(int i=0; i<4; i++){
int nr = curr.first + dirs[i];
int nc = curr.second + dirs[i+1];
if(nr<0 || nc<0 || nr>=m || nc>=n || visited[nr][nc]==1 || forest[nr][nc]==0)
continue;
if(nr==er && nc==ec)
return step;
visited[nr][nc] = 1;
q.push({nr,nc});
}
}
}
return -1;
}
int cutOffTree(vector<vector<int>>& forest) {
if(forest.empty() || forest[0].empty())
return 0;
int m = forest.size(), n = forest[0].size();
vector<vector<int>> trees;
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
if(forest[i][j]>1)
trees.push_back({forest[i][j],i,j});
sort(trees.begin(),trees.end());
int res = 0;
for(int i=0, row =0, col = 0; i<trees.size(); i++){
int step = bfs(forest,row,col,trees[i][1],trees[i][2]);
if(step==-1)
return -1;
res += step;
row = trees[i][1];
col = trees[i][2];
}
return res;
}
};