#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;
if(dis[start.vf][start.vs] != 1e18)r = dis[start.vf][start.vs]-1;
int ans = -1;
while(l <= r){
int mid = (l + r)/2;
dis1[start.vf][start.vs] = s * mid;
memset(vis,0,sizeof vis);
vis[start.vf][start.vs] = 1;
q.push(mp(start.vf , start.vs));
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' || arr[node.vf + dx[i]][node.vs + dy[i]] == 'D') && dis[node.vf + dx[i]][node.vs + dy[i]] > (dis1[node.vf][node.vs] + 1)/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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
81 ms |
9412 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
312 KB |
Output is correct |
12 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
320 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
332 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
0 ms |
332 KB |
Output is correct |
29 |
Correct |
0 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
316 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
320 KB |
Output is correct |
33 |
Correct |
5 ms |
1984 KB |
Output is correct |
34 |
Correct |
5 ms |
1996 KB |
Output is correct |
35 |
Correct |
21 ms |
2092 KB |
Output is correct |
36 |
Correct |
7 ms |
2636 KB |
Output is correct |
37 |
Correct |
7 ms |
2508 KB |
Output is correct |
38 |
Correct |
27 ms |
2636 KB |
Output is correct |
39 |
Correct |
8 ms |
3148 KB |
Output is correct |
40 |
Correct |
9 ms |
3148 KB |
Output is correct |
41 |
Correct |
37 ms |
3220 KB |
Output is correct |
42 |
Correct |
11 ms |
3916 KB |
Output is correct |
43 |
Correct |
12 ms |
3896 KB |
Output is correct |
44 |
Correct |
46 ms |
3916 KB |
Output is correct |
45 |
Correct |
14 ms |
4672 KB |
Output is correct |
46 |
Correct |
17 ms |
4684 KB |
Output is correct |
47 |
Correct |
64 ms |
4668 KB |
Output is correct |
48 |
Correct |
14 ms |
5452 KB |
Output is correct |
49 |
Correct |
15 ms |
5496 KB |
Output is correct |
50 |
Correct |
72 ms |
5500 KB |
Output is correct |
51 |
Correct |
16 ms |
6348 KB |
Output is correct |
52 |
Correct |
18 ms |
6236 KB |
Output is correct |
53 |
Correct |
83 ms |
6348 KB |
Output is correct |
54 |
Correct |
19 ms |
7244 KB |
Output is correct |
55 |
Correct |
22 ms |
7284 KB |
Output is correct |
56 |
Correct |
105 ms |
7268 KB |
Output is correct |
57 |
Correct |
22 ms |
8132 KB |
Output is correct |
58 |
Correct |
27 ms |
8192 KB |
Output is correct |
59 |
Correct |
111 ms |
8208 KB |
Output is correct |
60 |
Correct |
23 ms |
9256 KB |
Output is correct |
61 |
Correct |
27 ms |
9256 KB |
Output is correct |
62 |
Correct |
133 ms |
9276 KB |
Output is correct |
63 |
Correct |
108 ms |
9240 KB |
Output is correct |
64 |
Correct |
220 ms |
9468 KB |
Output is correct |
65 |
Correct |
165 ms |
9284 KB |
Output is correct |
66 |
Correct |
123 ms |
9252 KB |
Output is correct |
67 |
Correct |
115 ms |
9320 KB |
Output is correct |
68 |
Correct |
50 ms |
9248 KB |
Output is correct |
69 |
Correct |
49 ms |
9284 KB |
Output is correct |
70 |
Correct |
41 ms |
9284 KB |
Output is correct |
71 |
Correct |
44 ms |
9240 KB |
Output is correct |
72 |
Incorrect |
35 ms |
9344 KB |
Output isn't correct |
73 |
Incorrect |
72 ms |
9596 KB |
Output isn't correct |
74 |
Correct |
58 ms |
9520 KB |
Output is correct |
75 |
Correct |
80 ms |
9540 KB |
Output is correct |
76 |
Correct |
70 ms |
9516 KB |
Output is correct |
77 |
Correct |
63 ms |
9500 KB |
Output is correct |
78 |
Correct |
85 ms |
9568 KB |
Output is correct |
79 |
Correct |
50 ms |
9540 KB |
Output is correct |
80 |
Correct |
57 ms |
9564 KB |
Output is correct |
81 |
Correct |
68 ms |
9668 KB |
Output is correct |
82 |
Correct |
61 ms |
9540 KB |
Output is correct |
83 |
Correct |
76 ms |
9532 KB |
Output is correct |
84 |
Correct |
68 ms |
9456 KB |
Output is correct |
85 |
Correct |
78 ms |
9496 KB |
Output is correct |
86 |
Correct |
75 ms |
9524 KB |
Output is correct |
87 |
Correct |
82 ms |
9544 KB |
Output is correct |
88 |
Correct |
79 ms |
9548 KB |
Output is correct |
89 |
Correct |
78 ms |
9468 KB |
Output is correct |
90 |
Correct |
81 ms |
9412 KB |
Output is correct |
91 |
Correct |
81 ms |
9484 KB |
Output is correct |
92 |
Correct |
92 ms |
9480 KB |
Output is correct |