#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define mp make_pair
#define f first
#define s second
#define FOR(i, a, b, s) for (int i = (a); i < (b); i += s)
#define F0R(i, a) for (int i = 0; i < (a); i++)
vector<vector<int>> g;
pii mecho, home;
int n, s;
pii d[4] = {{0,1},{0,-1},{1,0},{-1,0}};
bool check(int t) {
vector<vector<int>> dist(n+2,vector<int>(n+2,INT_MAX));
dist[mecho.f][mecho.s] = t*s;
queue<pii> q; q.push(mecho);
while (q.size()) {
pii cur = q.front(); q.pop();
if (dist[cur.f][cur.s]>=g[cur.f][cur.s]) {continue;}
for (pii y : d) {
if (dist[cur.f+y.f][cur.s+y.s]==INT_MAX) {
dist[cur.f+y.f][cur.s+y.s] = dist[cur.f][cur.s]+1;
q.push({cur.f+y.f,cur.s+y.s});
}
}
} return (dist[home.f][home.s]!=INT_MAX);
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> s;
g = vector<vector<int>>(n+2,vector<int>(n+2,INT_MAX));
char c; queue<pii> q;
F0R(i,n) {
F0R(j,n) {
cin >> c;
if (c=='M') {mecho = {i+1,j+1};}
else if (c=='D') {home = {i+1,j+1};}
else if (c=='H') {g[i+1][j+1]=0; q.push({i+1,j+1});}
else if (c=='G') {g[i+1][j+1]=-1;}
else if (c=='T') {g[i+1][j+1]=-2;}
}
}
F0R(i,n+1) {g[0][i] = g[i][0] = g[n+1][i] = g[i][n+1] = -2;}
while (q.size()) {
pii x = q.front(); q.pop();
for (pii y : d) {
if (g[x.f+y.f][x.s+y.s]==-1) {
g[x.f+y.f][x.s+y.s]=g[x.f][x.s]+s;
q.push({x.f+y.f,x.s+y.s});
}
}
}
int l = 0, h = n*n, m = (l+h)/2, ans = -1;
while (l <= h) {
if (check(m)) {ans = m; l = m+1;}
else {h = m-1;}
m = (l+h)/2;
} cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
54 ms |
5628 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
13 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
14 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
5 ms |
1456 KB |
Output is correct |
34 |
Correct |
6 ms |
1444 KB |
Output is correct |
35 |
Correct |
14 ms |
1452 KB |
Output is correct |
36 |
Correct |
7 ms |
1628 KB |
Output is correct |
37 |
Correct |
8 ms |
1748 KB |
Output is correct |
38 |
Correct |
19 ms |
1756 KB |
Output is correct |
39 |
Correct |
9 ms |
2084 KB |
Output is correct |
40 |
Correct |
10 ms |
2084 KB |
Output is correct |
41 |
Correct |
27 ms |
2132 KB |
Output is correct |
42 |
Correct |
11 ms |
2524 KB |
Output is correct |
43 |
Correct |
11 ms |
2452 KB |
Output is correct |
44 |
Correct |
32 ms |
2444 KB |
Output is correct |
45 |
Correct |
13 ms |
2880 KB |
Output is correct |
46 |
Correct |
20 ms |
2920 KB |
Output is correct |
47 |
Correct |
42 ms |
2888 KB |
Output is correct |
48 |
Correct |
15 ms |
3324 KB |
Output is correct |
49 |
Correct |
16 ms |
3400 KB |
Output is correct |
50 |
Correct |
42 ms |
3324 KB |
Output is correct |
51 |
Correct |
19 ms |
3876 KB |
Output is correct |
52 |
Correct |
19 ms |
3828 KB |
Output is correct |
53 |
Correct |
55 ms |
3876 KB |
Output is correct |
54 |
Correct |
20 ms |
4408 KB |
Output is correct |
55 |
Correct |
22 ms |
4412 KB |
Output is correct |
56 |
Correct |
63 ms |
4384 KB |
Output is correct |
57 |
Correct |
23 ms |
4956 KB |
Output is correct |
58 |
Correct |
26 ms |
4964 KB |
Output is correct |
59 |
Correct |
74 ms |
4952 KB |
Output is correct |
60 |
Correct |
28 ms |
5564 KB |
Output is correct |
61 |
Correct |
33 ms |
5628 KB |
Output is correct |
62 |
Correct |
85 ms |
5576 KB |
Output is correct |
63 |
Correct |
89 ms |
5596 KB |
Output is correct |
64 |
Correct |
148 ms |
5548 KB |
Output is correct |
65 |
Correct |
117 ms |
5528 KB |
Output is correct |
66 |
Correct |
101 ms |
5520 KB |
Output is correct |
67 |
Correct |
100 ms |
5560 KB |
Output is correct |
68 |
Correct |
52 ms |
5580 KB |
Output is correct |
69 |
Correct |
49 ms |
5608 KB |
Output is correct |
70 |
Correct |
43 ms |
5588 KB |
Output is correct |
71 |
Correct |
43 ms |
5592 KB |
Output is correct |
72 |
Incorrect |
56 ms |
5560 KB |
Output isn't correct |
73 |
Incorrect |
45 ms |
5628 KB |
Output isn't correct |
74 |
Correct |
41 ms |
5600 KB |
Output is correct |
75 |
Correct |
49 ms |
5584 KB |
Output is correct |
76 |
Correct |
44 ms |
5576 KB |
Output is correct |
77 |
Correct |
41 ms |
5592 KB |
Output is correct |
78 |
Correct |
47 ms |
5608 KB |
Output is correct |
79 |
Correct |
35 ms |
5604 KB |
Output is correct |
80 |
Correct |
39 ms |
5592 KB |
Output is correct |
81 |
Correct |
41 ms |
5588 KB |
Output is correct |
82 |
Correct |
42 ms |
5588 KB |
Output is correct |
83 |
Correct |
49 ms |
5596 KB |
Output is correct |
84 |
Correct |
44 ms |
5596 KB |
Output is correct |
85 |
Correct |
47 ms |
5632 KB |
Output is correct |
86 |
Correct |
48 ms |
5660 KB |
Output is correct |
87 |
Correct |
50 ms |
5568 KB |
Output is correct |
88 |
Correct |
50 ms |
5660 KB |
Output is correct |
89 |
Correct |
51 ms |
5660 KB |
Output is correct |
90 |
Correct |
53 ms |
5600 KB |
Output is correct |
91 |
Correct |
46 ms |
5592 KB |
Output is correct |
92 |
Correct |
52 ms |
5556 KB |
Output is correct |