#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;
}
cout << res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
376 KB |
Output is correct |
5 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
64 ms |
7024 KB |
Output is correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 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 |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
352 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
356 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
352 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
356 KB |
Output is correct |
29 |
Correct |
1 ms |
352 KB |
Output is correct |
30 |
Correct |
1 ms |
356 KB |
Output is correct |
31 |
Correct |
1 ms |
352 KB |
Output is correct |
32 |
Correct |
0 ms |
604 KB |
Output is correct |
33 |
Correct |
6 ms |
1932 KB |
Output is correct |
34 |
Correct |
6 ms |
1920 KB |
Output is correct |
35 |
Correct |
16 ms |
1932 KB |
Output is correct |
36 |
Correct |
7 ms |
2568 KB |
Output is correct |
37 |
Correct |
8 ms |
2316 KB |
Output is correct |
38 |
Correct |
20 ms |
2332 KB |
Output is correct |
39 |
Correct |
8 ms |
2728 KB |
Output is correct |
40 |
Correct |
9 ms |
2768 KB |
Output is correct |
41 |
Correct |
27 ms |
2828 KB |
Output is correct |
42 |
Correct |
11 ms |
3264 KB |
Output is correct |
43 |
Correct |
11 ms |
3216 KB |
Output is correct |
44 |
Correct |
34 ms |
3260 KB |
Output is correct |
45 |
Correct |
13 ms |
3736 KB |
Output is correct |
46 |
Correct |
13 ms |
3748 KB |
Output is correct |
47 |
Correct |
40 ms |
3740 KB |
Output is correct |
48 |
Correct |
15 ms |
4348 KB |
Output is correct |
49 |
Correct |
16 ms |
4312 KB |
Output is correct |
50 |
Correct |
53 ms |
4312 KB |
Output is correct |
51 |
Correct |
17 ms |
4960 KB |
Output is correct |
52 |
Correct |
19 ms |
4960 KB |
Output is correct |
53 |
Correct |
58 ms |
4928 KB |
Output is correct |
54 |
Correct |
20 ms |
5584 KB |
Output is correct |
55 |
Correct |
21 ms |
5544 KB |
Output is correct |
56 |
Correct |
66 ms |
5580 KB |
Output is correct |
57 |
Correct |
26 ms |
6284 KB |
Output is correct |
58 |
Correct |
27 ms |
6288 KB |
Output is correct |
59 |
Correct |
90 ms |
6284 KB |
Output is correct |
60 |
Correct |
29 ms |
7064 KB |
Output is correct |
61 |
Correct |
28 ms |
7072 KB |
Output is correct |
62 |
Correct |
84 ms |
7004 KB |
Output is correct |
63 |
Correct |
94 ms |
6912 KB |
Output is correct |
64 |
Correct |
158 ms |
7028 KB |
Output is correct |
65 |
Correct |
119 ms |
7032 KB |
Output is correct |
66 |
Correct |
101 ms |
6920 KB |
Output is correct |
67 |
Incorrect |
103 ms |
7336 KB |
Output isn't correct |
68 |
Correct |
48 ms |
7048 KB |
Output is correct |
69 |
Correct |
46 ms |
7048 KB |
Output is correct |
70 |
Correct |
39 ms |
7028 KB |
Output is correct |
71 |
Correct |
43 ms |
7044 KB |
Output is correct |
72 |
Correct |
37 ms |
7004 KB |
Output is correct |
73 |
Correct |
44 ms |
7044 KB |
Output is correct |
74 |
Correct |
50 ms |
7044 KB |
Output is correct |
75 |
Correct |
61 ms |
7028 KB |
Output is correct |
76 |
Correct |
59 ms |
7064 KB |
Output is correct |
77 |
Correct |
51 ms |
7088 KB |
Output is correct |
78 |
Incorrect |
61 ms |
7072 KB |
Output isn't correct |
79 |
Correct |
45 ms |
7060 KB |
Output is correct |
80 |
Correct |
51 ms |
7032 KB |
Output is correct |
81 |
Correct |
57 ms |
7092 KB |
Output is correct |
82 |
Correct |
53 ms |
7016 KB |
Output is correct |
83 |
Correct |
64 ms |
7044 KB |
Output is correct |
84 |
Correct |
61 ms |
7032 KB |
Output is correct |
85 |
Correct |
59 ms |
7068 KB |
Output is correct |
86 |
Correct |
59 ms |
7028 KB |
Output is correct |
87 |
Correct |
60 ms |
7060 KB |
Output is correct |
88 |
Correct |
60 ms |
7024 KB |
Output is correct |
89 |
Correct |
92 ms |
7036 KB |
Output is correct |
90 |
Correct |
66 ms |
7036 KB |
Output is correct |
91 |
Correct |
70 ms |
7060 KB |
Output is correct |
92 |
Correct |
67 ms |
7068 KB |
Output is correct |