-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCut Off Trees for Golf Event.cpp
66 lines (54 loc) Β· 1.87 KB
/
Cut Off Trees for Golf Event.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
57
58
59
60
61
62
63
64
65
66
class Solution {
private:
const int INF = 1061109567;
public:
int cutOffTree(vector<vector<int>>& forest) {
int n = forest.size() ;
int m = forest[0].size() ;
int ans = 0 ;
set<pair<int,pair<int,int>>> points ;
for(int i = 0; i<n ; i++){
for(int j = 0; j<m; j++){
if(forest[i][j]>1){
points.insert({forest[i][j], {i,j}}) ;
}
}
}
int sx = 0, sy = 0 ;
for(auto elem :points){
int dx = elem.second.first ;
int dy = elem.second.second ;
int bfs_dist = bfs(forest, n, m, sx, sy, dx, dy) ;
if(bfs_dist == -1) return -1 ;
ans+=bfs_dist ;
forest[dx][dy] = 1;
sx = dx ;
sy = dy ;
}
return ans ;
}
int bfs(vector<vector<int>> &forest, int n, int m , int sx, int sy, int dx, int dy){
int distance[n+2][m+2] ;
memset(distance, 0x3f, sizeof(distance));
int dirx[] = {1, 0, -1, 0} ;
int diry[] = {0, 1,0, -1} ;
queue<pair<int,int>> q;
q.push({sx,sy}) ;
distance[sx][sy] = 0 ;
while(!q.empty()){
pair<int,int> p = q.front() ;
q.pop() ;
if(p.first == dx && p.second == dy) return distance[dx][dy] ;
for(int i = 0 ;i<4 ;i++){
int x = p.first + dirx[i] ;
int y = p.second + diry[i] ;
if(x<0 || x>=n || y <0 || y>=m)continue ;
if(forest[x][y] == 0)continue ;
if(distance[x][y] != INF)continue ;
distance[x][y] = distance[p.first][p.second] + 1 ;
q.push({x,y}) ;
}
}
return -1 ;
}
};