#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
using namespace std;
int n, s;
const int maxn = 1000;
const int inf = 1e9;
char a[maxn + 3][maxn + 3];
pii st, en;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
bool valid(int i, int j)
{
return (i >= 1 && j >= 1 && i <= n && j <= n);
}
bool reach(int x, int y)
{
return x/s < y;
}
int main()
{
// freopen("metro.in", "r", stdin);
// freopen("metro.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> s;
vector<vector<int>>time(n + 3, vector<int>(n + 3, inf));
queue<pii>q;
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= n; ++j)
{
cin >> a[i][j];
if(a[i][j] == 'M') st.fi = i, st.se = j;
else if(a[i][j] == 'D')
{
en.fi = i, en.se = j;
}
else if(a[i][j] == 'H')
{
q.push({i, j});
time[i][j] = 0;
}
}
}
while(!q.empty())
{
int x = q.front().fi;
int y = q.front().se;
q.pop();
for(int k = 0; k < 4; k++)
{
int i = x + dx[k];
int j = y + dy[k];
if(valid(i, j) && (a[i][j] == 'G' || a[i][j] == 'M') && time[i][j] == inf)
{
q.push({i, j});
time[i][j] = time[x][y] + 1;
}
}
}
/*for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
cout << time[i][j] << ' ';
} cout << '\n';
}*/
int l = 0, r = n * n, mid, res = 0;
while(l <= r)
{
mid = (l + r) / 2;
vector<vector<int>>mecho_time(n + 3, vector<int>(n + 3, inf));
mecho_time[st.fi][st.se] = 0;
q.push(st);
if(time[st.fi][st.se] <= mid){
q.pop();
}
while(!q.empty()){
int x = q.front().fi;
int y = q.front().se;
q.pop();
for(int k = 0; k < 4; k++)
{
int i = x + dx[k];
int j = y + dy[k];
if(valid(i, j) && (a[i][j] == 'G' || a[i][j] == 'M') && mecho_time[i][j] == inf && reach(mecho_time[x][y] + 1, time[i][j] - mid))
{
q.push({i, j});
mecho_time[i][j] = mecho_time[x][y] + 1;
}
}
}
bool Reach = false;
for(int i = 0; i < 4; i++)
{
int nx = en.fi + dx[i], ny = en.se + dy[i];
if(valid(nx, ny) && (a[nx][ny] == 'G' || a[nx][ny] == 'M') && reach(mecho_time[nx][ny], time[nx][ny] - mid))
{
Reach = true;
break;
}
}
if(Reach) {
res = mid;
l = mid + 1;
}
else r = mid - 1;
}
if(res)cout << res;
else cout << -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
66 ms |
7032 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
472 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
356 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
352 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
356 KB |
Output is correct |
29 |
Correct |
0 ms |
352 KB |
Output is correct |
30 |
Correct |
0 ms |
356 KB |
Output is correct |
31 |
Correct |
1 ms |
352 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
5 ms |
1940 KB |
Output is correct |
34 |
Correct |
5 ms |
1920 KB |
Output is correct |
35 |
Correct |
16 ms |
1936 KB |
Output is correct |
36 |
Correct |
7 ms |
2332 KB |
Output is correct |
37 |
Correct |
7 ms |
2316 KB |
Output is correct |
38 |
Correct |
19 ms |
2332 KB |
Output is correct |
39 |
Correct |
8 ms |
2728 KB |
Output is correct |
40 |
Correct |
9 ms |
2744 KB |
Output is correct |
41 |
Correct |
27 ms |
2756 KB |
Output is correct |
42 |
Correct |
10 ms |
3244 KB |
Output is correct |
43 |
Correct |
11 ms |
3212 KB |
Output is correct |
44 |
Correct |
32 ms |
3288 KB |
Output is correct |
45 |
Correct |
13 ms |
3800 KB |
Output is correct |
46 |
Correct |
13 ms |
3684 KB |
Output is correct |
47 |
Correct |
38 ms |
3740 KB |
Output is correct |
48 |
Correct |
15 ms |
4356 KB |
Output is correct |
49 |
Correct |
16 ms |
4360 KB |
Output is correct |
50 |
Correct |
50 ms |
4512 KB |
Output is correct |
51 |
Correct |
17 ms |
4960 KB |
Output is correct |
52 |
Correct |
23 ms |
4956 KB |
Output is correct |
53 |
Correct |
57 ms |
4932 KB |
Output is correct |
54 |
Correct |
20 ms |
5616 KB |
Output is correct |
55 |
Correct |
21 ms |
5584 KB |
Output is correct |
56 |
Correct |
66 ms |
5580 KB |
Output is correct |
57 |
Correct |
24 ms |
6280 KB |
Output is correct |
58 |
Correct |
30 ms |
6284 KB |
Output is correct |
59 |
Correct |
76 ms |
6292 KB |
Output is correct |
60 |
Correct |
28 ms |
6940 KB |
Output is correct |
61 |
Correct |
31 ms |
6936 KB |
Output is correct |
62 |
Correct |
88 ms |
7004 KB |
Output is correct |
63 |
Incorrect |
84 ms |
6908 KB |
Output isn't correct |
64 |
Correct |
148 ms |
7160 KB |
Output is correct |
65 |
Correct |
115 ms |
7000 KB |
Output is correct |
66 |
Correct |
103 ms |
6920 KB |
Output is correct |
67 |
Correct |
93 ms |
6908 KB |
Output is correct |
68 |
Incorrect |
45 ms |
7044 KB |
Output isn't correct |
69 |
Correct |
46 ms |
7116 KB |
Output is correct |
70 |
Correct |
38 ms |
7056 KB |
Output is correct |
71 |
Correct |
42 ms |
7044 KB |
Output is correct |
72 |
Correct |
35 ms |
7020 KB |
Output is correct |
73 |
Correct |
48 ms |
7068 KB |
Output is correct |
74 |
Correct |
50 ms |
7060 KB |
Output is correct |
75 |
Correct |
55 ms |
7060 KB |
Output is correct |
76 |
Correct |
53 ms |
7064 KB |
Output is correct |
77 |
Correct |
56 ms |
7096 KB |
Output is correct |
78 |
Correct |
57 ms |
7052 KB |
Output is correct |
79 |
Correct |
44 ms |
7040 KB |
Output is correct |
80 |
Correct |
49 ms |
7072 KB |
Output is correct |
81 |
Correct |
55 ms |
7072 KB |
Output is correct |
82 |
Correct |
48 ms |
7064 KB |
Output is correct |
83 |
Incorrect |
60 ms |
7068 KB |
Output isn't correct |
84 |
Correct |
54 ms |
7064 KB |
Output is correct |
85 |
Correct |
60 ms |
7064 KB |
Output is correct |
86 |
Correct |
60 ms |
7032 KB |
Output is correct |
87 |
Correct |
57 ms |
7036 KB |
Output is correct |
88 |
Incorrect |
59 ms |
7068 KB |
Output isn't correct |
89 |
Correct |
63 ms |
7056 KB |
Output is correct |
90 |
Correct |
69 ms |
7044 KB |
Output is correct |
91 |
Correct |
65 ms |
7024 KB |
Output is correct |
92 |
Correct |
67 ms |
7052 KB |
Output is correct |