-
Notifications
You must be signed in to change notification settings - Fork 0
/
new.cpp
72 lines (57 loc) · 2.07 KB
/
new.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
67
68
69
70
71
72
class Solution {
public:
vector<int> roww = {0,0,-1,1};
vector<int> coll = {-1,1,0,0};
void bfs(vector<vector<int>>& grid,vector<vector<int>>& score,int n) {
queue<pair<int, int>> q;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++){
if(grid[i][j]) {
score[i][j] = 0;
q.push({i, j});
}
}
}
while(!q.empty()){
auto t = q.front();
q.pop();
int x = t.first, y = t.second;
int s = score[x][y];
for(int i =0; i < 4; i++){
int newX = x + roww[i];
int newY = y + coll[i];
if(newX >= 0 && newX < n && newY >= 0 && newY < n && score[newX][newY] > 1 + s) {
score[newX][newY] = 1 + s;
q.push({newX, newY});
}
}
}
}
int maximumSafenessFactor(vector<vector<int>>& grid) {
ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
int n = grid.size();
if(grid[0][0] || grid[n - 1][n - 1]) return 0;
vector<vector<int>> score(n,vector<int>(n,INT_MAX));
bfs(grid, score, n);
vector<vector<bool>> vis(n, vector<bool>(n, false));
priority_queue<pair<int,pair<int,int>>> pq;
pq.push({score[0][0], {0,0}});
while(!pq.empty()){
auto temp = pq.top().second;
auto safe = pq.top().first;
pq.pop();
if(temp.first == n - 1 && temp.second == n - 1) return safe;
vis[temp.first][temp.second] = true;
for(int i = 0; i < 4; i++) {
int newX = temp.first + roww[i];
int newY = temp.second + coll[i];
if(newX >= 0 && newX < n && newY >= 0 && newY < n && !vis[newX][newY]){
int s = min(safe, score[newX][newY]);
pq.push({s, {newX, newY}});
vis[newX][newY] = true;
}
}
}
return -1;
}
};