#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fi first
#define se second
#define mp make_pair
#define eb emplace_back
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORU(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define IOS cin.tie(0)->sync_with_stdio(false);
#define PROB "IOI09_mecho"
void Fi(){
if(fopen(PROB".inp", "r")){
freopen(PROB".inp", "r", stdin);
freopen(PROB".out", "w", stdout);
}
}
const int INF = 1e9;
const int N = 800 + 1;
int n, spd;
int a[N][N];
using pi = pair<int, int>;
pi S, E;
queue<pi> qe;
int d[N][N];
const int dx[] = {0, 0, -1, +1};
const int dy[] = {-1, +1, 0, 0};
void bfsBee(){
while(!qe.empty()){
auto [x, y] = qe.front(); qe.pop();
FOR(direc, 0, 4){
int i = x + dx[direc];
int j = y + dy[direc];
if(i < 1 || n < i || j < 1 || n < j) continue;
if(a[i][j] == 1 && d[i][j] == -1){
d[i][j] = d[x][y] + 1;
qe.emplace(i, j);
}
}
}
}
pi D[N][N];
bool bfsMecho(const int &T){
FORU(i, 1, n) FORU(j, 1, n) D[i][j] = {INF, INF};
qe.emplace(S); D[S.fi][S.se] = {T, 0};
while(!qe.empty()){
auto [x, y] = qe.front(); qe.pop();
// cout << x << " " << y << " " << D[x][y].fi << " " << D[x][y].se << "\n";
FOR(direc, 0, 4){
int i = x + dx[direc];
int j = y + dy[direc];
if(i < 1 || n < i || j < 1 || n < j) continue;
if(a[i][j] && D[i][j].fi == INF){
D[i][j] = D[x][y];
if(++D[i][j].se >= spd){
++D[i][j].fi;
D[i][j].se = 0;
}
if(a[i][j] == 2){
while(!qe.empty()) qe.pop();
return true;
}
if(D[i][j].fi < d[i][j]){
qe.emplace(i, j);
}
}
}
}
return false;
}
int main(){
IOS;
Fi();
cin >> n >> spd;
memset(d, -1, sizeof(d));
FORU(i, 1, n){
string s; cin >> s;
FORU(j, 1, n){
switch(s[j - 1]){
case 'T': a[i][j] = 0; break;
case 'G': a[i][j] = 1; break;
case 'M': a[i][j] = 1; S = {i, j}; break;
case 'D': a[i][j] = 2; E = {i, j}; break;
case 'H': a[i][j] = 0; qe.emplace(i, j); d[i][j] = 0; break;
}
}
}
bfsBee();
int l = 0, r = d[S.fi][S.se] - 1, ans = -1;
while(l <= r){
int m = (l + r) / 2;
if(bfsMecho(m)) l = m + 1, ans = m;
else r = m - 1;
}
cout << ans;
return 0;
}
Compilation message
mecho.cpp: In function 'void Fi()':
mecho.cpp:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | freopen(PROB".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:21:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | freopen(PROB".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2764 KB |
Output is correct |
2 |
Correct |
1 ms |
2764 KB |
Output is correct |
3 |
Correct |
1 ms |
2764 KB |
Output is correct |
4 |
Correct |
1 ms |
2764 KB |
Output is correct |
5 |
Correct |
2 ms |
2892 KB |
Output is correct |
6 |
Correct |
1 ms |
2892 KB |
Output is correct |
7 |
Correct |
106 ms |
10444 KB |
Output is correct |
8 |
Correct |
1 ms |
2892 KB |
Output is correct |
9 |
Correct |
1 ms |
2892 KB |
Output is correct |
10 |
Correct |
1 ms |
2892 KB |
Output is correct |
11 |
Correct |
1 ms |
2892 KB |
Output is correct |
12 |
Correct |
1 ms |
3276 KB |
Output is correct |
13 |
Correct |
2 ms |
3148 KB |
Output is correct |
14 |
Correct |
2 ms |
3276 KB |
Output is correct |
15 |
Correct |
1 ms |
2892 KB |
Output is correct |
16 |
Correct |
2 ms |
2892 KB |
Output is correct |
17 |
Correct |
1 ms |
3020 KB |
Output is correct |
18 |
Correct |
1 ms |
3020 KB |
Output is correct |
19 |
Correct |
1 ms |
2980 KB |
Output is correct |
20 |
Correct |
1 ms |
3020 KB |
Output is correct |
21 |
Correct |
2 ms |
3148 KB |
Output is correct |
22 |
Correct |
2 ms |
3020 KB |
Output is correct |
23 |
Correct |
2 ms |
3148 KB |
Output is correct |
24 |
Correct |
2 ms |
3124 KB |
Output is correct |
25 |
Correct |
1 ms |
3276 KB |
Output is correct |
26 |
Correct |
2 ms |
3276 KB |
Output is correct |
27 |
Correct |
2 ms |
3276 KB |
Output is correct |
28 |
Correct |
2 ms |
3276 KB |
Output is correct |
29 |
Correct |
1 ms |
3276 KB |
Output is correct |
30 |
Correct |
2 ms |
3276 KB |
Output is correct |
31 |
Correct |
2 ms |
3404 KB |
Output is correct |
32 |
Correct |
1 ms |
3404 KB |
Output is correct |
33 |
Correct |
6 ms |
6092 KB |
Output is correct |
34 |
Correct |
6 ms |
6092 KB |
Output is correct |
35 |
Correct |
19 ms |
6092 KB |
Output is correct |
36 |
Correct |
8 ms |
6604 KB |
Output is correct |
37 |
Correct |
7 ms |
6608 KB |
Output is correct |
38 |
Correct |
24 ms |
6616 KB |
Output is correct |
39 |
Correct |
8 ms |
6988 KB |
Output is correct |
40 |
Correct |
9 ms |
7012 KB |
Output is correct |
41 |
Correct |
31 ms |
6988 KB |
Output is correct |
42 |
Correct |
9 ms |
7556 KB |
Output is correct |
43 |
Correct |
14 ms |
7468 KB |
Output is correct |
44 |
Correct |
44 ms |
7500 KB |
Output is correct |
45 |
Correct |
11 ms |
8012 KB |
Output is correct |
46 |
Correct |
13 ms |
8040 KB |
Output is correct |
47 |
Correct |
53 ms |
8028 KB |
Output is correct |
48 |
Correct |
14 ms |
8396 KB |
Output is correct |
49 |
Correct |
16 ms |
8396 KB |
Output is correct |
50 |
Correct |
56 ms |
8500 KB |
Output is correct |
51 |
Correct |
17 ms |
8868 KB |
Output is correct |
52 |
Correct |
16 ms |
8908 KB |
Output is correct |
53 |
Correct |
84 ms |
8980 KB |
Output is correct |
54 |
Correct |
17 ms |
9448 KB |
Output is correct |
55 |
Correct |
19 ms |
9360 KB |
Output is correct |
56 |
Correct |
104 ms |
9408 KB |
Output is correct |
57 |
Correct |
21 ms |
9932 KB |
Output is correct |
58 |
Correct |
20 ms |
9920 KB |
Output is correct |
59 |
Correct |
98 ms |
9912 KB |
Output is correct |
60 |
Correct |
22 ms |
10316 KB |
Output is correct |
61 |
Correct |
24 ms |
10360 KB |
Output is correct |
62 |
Correct |
139 ms |
10368 KB |
Output is correct |
63 |
Correct |
110 ms |
10360 KB |
Output is correct |
64 |
Correct |
168 ms |
10308 KB |
Output is correct |
65 |
Correct |
169 ms |
10368 KB |
Output is correct |
66 |
Correct |
126 ms |
10364 KB |
Output is correct |
67 |
Correct |
115 ms |
10360 KB |
Output is correct |
68 |
Correct |
51 ms |
10388 KB |
Output is correct |
69 |
Correct |
49 ms |
10488 KB |
Output is correct |
70 |
Correct |
45 ms |
10368 KB |
Output is correct |
71 |
Correct |
38 ms |
10380 KB |
Output is correct |
72 |
Correct |
37 ms |
10316 KB |
Output is correct |
73 |
Correct |
39 ms |
10564 KB |
Output is correct |
74 |
Correct |
59 ms |
10572 KB |
Output is correct |
75 |
Correct |
57 ms |
10636 KB |
Output is correct |
76 |
Correct |
62 ms |
10640 KB |
Output is correct |
77 |
Correct |
64 ms |
10636 KB |
Output is correct |
78 |
Correct |
66 ms |
10564 KB |
Output is correct |
79 |
Correct |
53 ms |
10612 KB |
Output is correct |
80 |
Correct |
62 ms |
10704 KB |
Output is correct |
81 |
Correct |
59 ms |
10572 KB |
Output is correct |
82 |
Correct |
57 ms |
10580 KB |
Output is correct |
83 |
Correct |
79 ms |
10572 KB |
Output is correct |
84 |
Correct |
71 ms |
10576 KB |
Output is correct |
85 |
Correct |
78 ms |
10572 KB |
Output is correct |
86 |
Correct |
79 ms |
10564 KB |
Output is correct |
87 |
Correct |
68 ms |
10564 KB |
Output is correct |
88 |
Correct |
112 ms |
10524 KB |
Output is correct |
89 |
Correct |
85 ms |
10640 KB |
Output is correct |
90 |
Correct |
84 ms |
10500 KB |
Output is correct |
91 |
Correct |
83 ms |
10512 KB |
Output is correct |
92 |
Correct |
93 ms |
10436 KB |
Output is correct |