#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
bool remender(ll a , ll b){return a%b;}
void clear( std::queue<pair<int,int>> &q )
{
std::queue<pair<int,int>> empty;
std::swap( q, empty );
}
void solve(){
int dx[4] = {0,0,-1,1} , dy[4] = {1,-1,0,0};
int n , s;
cin >> n >> s;
char arr[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++)cin >> arr[i][j];
}
queue<pair<int,int>> q;
ll dis[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++)dis[i][j] = 1e18;
}
bool vis[n][n];
memset(vis,0,sizeof vis);
pair<int,int> end, start;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(arr[i][j] == 'H'){q.push(mp(i,j));dis[i][j] = 0;vis[i][j] = 1;}
if(arr[i][j] == 'D')end = mp(i,j);
if(arr[i][j] == 'M')start = mp(i,j);
}
}
while(q.size()){
pair<int,int> node = q.front();
q.pop();
for(int i = 0; i < 4; i++){
if(node.vf + dx[i] >= 0 && node.vf + dx[i] < n && node.vs + dy[i] < n && node.vs + dy[i] >= 0 && !vis[node.vf + dx[i]][node.vs + dy[i]] && arr[node.vf + dx[i]][node.vs + dy[i]] == 'G'){
q.push(mp(node.vf + dx[i] , node.vs + dy[i]));
dis[node.vf + dx[i]][node.vs + dy[i]] = dis[node.vf][node.vs] + 1;
vis[node.vf + dx[i]][node.vs + dy[i]] = 1;
}
}
}
int dis1[n][n];
int l = 0 , r = n * n + 3;
int ans = -1;
while(l <= r){
int mid = (l + r)/2;
memset(dis1,-1,sizeof dis1);
dis1[start.vf][start.vs] = s * mid;
memset(vis,0,sizeof vis);
vis[start.vf][start.vs] = 1;
clear(q);
q.push(mp(start.vf , start.vs));
while(q.size() && !vis[end.vf][end.vs]){
pair<int,int> node = q.front();
q.pop();
for(int i = 0; i < 4; i++){
if(node.vf + dx[i] >= 0 && node.vf + dx[i] < n && node.vs + dy[i] < n && node.vs + dy[i] >= 0 && !vis[node.vf + dx[i]][node.vs + dy[i]] && (arr[node.vf + dx[i]][node.vs + dy[i]] == 'G' || arr[node.vf + dx[i]][node.vs + dy[i]] == 'D') && dis[node.vf + dx[i]][node.vs + dy[i]] > (dis1[node.vf][node.vs])/s){
q.push(mp(node.vf + dx[i] , node.vs + dy[i]));
dis1[node.vf + dx[i]][node.vs + dy[i]] = dis1[node.vf][node.vs] + 1;
vis[node.vf + dx[i]][node.vs + dy[i]] = 1;
}
}
}
if(vis[end.vf][end.vs]){
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
cout << ans << '\n';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;cin >> t;while(t--)
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
312 KB |
Output isn't correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
84 ms |
9244 KB |
Output is correct |
8 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
26 |
Correct |
0 ms |
332 KB |
Output is correct |
27 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Incorrect |
5 ms |
1996 KB |
Output isn't correct |
34 |
Correct |
4 ms |
1996 KB |
Output is correct |
35 |
Correct |
24 ms |
1996 KB |
Output is correct |
36 |
Incorrect |
6 ms |
2508 KB |
Output isn't correct |
37 |
Correct |
6 ms |
2508 KB |
Output is correct |
38 |
Correct |
28 ms |
2624 KB |
Output is correct |
39 |
Incorrect |
8 ms |
3136 KB |
Output isn't correct |
40 |
Correct |
8 ms |
3148 KB |
Output is correct |
41 |
Correct |
36 ms |
3148 KB |
Output is correct |
42 |
Incorrect |
8 ms |
3788 KB |
Output isn't correct |
43 |
Correct |
9 ms |
3812 KB |
Output is correct |
44 |
Correct |
46 ms |
3864 KB |
Output is correct |
45 |
Incorrect |
11 ms |
4556 KB |
Output isn't correct |
46 |
Correct |
11 ms |
4556 KB |
Output is correct |
47 |
Correct |
50 ms |
4556 KB |
Output is correct |
48 |
Incorrect |
12 ms |
5380 KB |
Output isn't correct |
49 |
Correct |
14 ms |
5324 KB |
Output is correct |
50 |
Correct |
62 ms |
5324 KB |
Output is correct |
51 |
Incorrect |
15 ms |
6124 KB |
Output isn't correct |
52 |
Correct |
14 ms |
6196 KB |
Output is correct |
53 |
Correct |
76 ms |
6220 KB |
Output is correct |
54 |
Incorrect |
16 ms |
7072 KB |
Output isn't correct |
55 |
Correct |
20 ms |
7156 KB |
Output is correct |
56 |
Correct |
92 ms |
7116 KB |
Output is correct |
57 |
Incorrect |
19 ms |
8140 KB |
Output isn't correct |
58 |
Correct |
19 ms |
8120 KB |
Output is correct |
59 |
Correct |
125 ms |
8148 KB |
Output is correct |
60 |
Incorrect |
22 ms |
9128 KB |
Output isn't correct |
61 |
Correct |
25 ms |
9120 KB |
Output is correct |
62 |
Correct |
131 ms |
9164 KB |
Output is correct |
63 |
Correct |
116 ms |
9204 KB |
Output is correct |
64 |
Correct |
212 ms |
9204 KB |
Output is correct |
65 |
Correct |
153 ms |
9156 KB |
Output is correct |
66 |
Incorrect |
140 ms |
9156 KB |
Output isn't correct |
67 |
Correct |
117 ms |
9152 KB |
Output is correct |
68 |
Correct |
50 ms |
9156 KB |
Output is correct |
69 |
Correct |
59 ms |
9244 KB |
Output is correct |
70 |
Correct |
39 ms |
9152 KB |
Output is correct |
71 |
Correct |
47 ms |
9172 KB |
Output is correct |
72 |
Incorrect |
37 ms |
9164 KB |
Output isn't correct |
73 |
Incorrect |
75 ms |
9388 KB |
Output isn't correct |
74 |
Correct |
62 ms |
9440 KB |
Output is correct |
75 |
Correct |
74 ms |
9408 KB |
Output is correct |
76 |
Correct |
65 ms |
9412 KB |
Output is correct |
77 |
Correct |
75 ms |
9540 KB |
Output is correct |
78 |
Correct |
76 ms |
9388 KB |
Output is correct |
79 |
Correct |
49 ms |
9380 KB |
Output is correct |
80 |
Correct |
60 ms |
9412 KB |
Output is correct |
81 |
Correct |
67 ms |
9380 KB |
Output is correct |
82 |
Correct |
62 ms |
9408 KB |
Output is correct |
83 |
Correct |
86 ms |
9416 KB |
Output is correct |
84 |
Correct |
71 ms |
9388 KB |
Output is correct |
85 |
Correct |
78 ms |
9456 KB |
Output is correct |
86 |
Correct |
78 ms |
9416 KB |
Output is correct |
87 |
Correct |
76 ms |
9300 KB |
Output is correct |
88 |
Correct |
79 ms |
9360 KB |
Output is correct |
89 |
Correct |
85 ms |
9404 KB |
Output is correct |
90 |
Correct |
82 ms |
9360 KB |
Output is correct |
91 |
Correct |
82 ms |
9360 KB |
Output is correct |
92 |
Correct |
89 ms |
9288 KB |
Output is correct |