#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int INF = 0x3fffffff;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
#define name2(a,b,c,...) c
#define rep1(b) for(int i = 0; i < b; i++)
#define rep2(i,b) for(int i = 0; i < b; i++)
#define rep(...) name2(__VA_ARGS__,rep2,rep1)(__VA_ARGS__)
#define each(i,a) for(auto&& i : a)
template<class T, class U> bool chmin(T& a, const U& b){ if(a > b){ a = b; return 1; } return 0; }
template<class T, class U> bool chmax(T& a, const U& b){ if(a < b){ a = b; return 1; } return 0; }
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, s;
cin >> n >> s;
vector<string> a(n);
each(i, a) cin >> i;
vector<vector<int>> bee(n, vector<int>(n, INF));
queue<pii> q;
rep(n) rep(j, n) if(a[i][j] == 'H'){
bee[i][j] = 0;
q.emplace(i, j);
}
while(q.size()){
auto [x, y] = q.front();
q.pop();
rep(4){
int x2 = x + dx[i], y2 = y + dy[i];
if(x2 < 0 || y2 < 0 || x2 >= n || y2 >= n || a[x2][y2] != 'G' && a[x2][y2] != 'M') continue;
if(chmin(bee[x2][y2], bee[x][y] + 1)) q.emplace(x2, y2);
}
}
pii start, goal;
rep(n) rep(j, n) if(a[i][j] == 'M') start = {i, j};
rep(n) rep(j, n) if(a[i][j] == 'D') goal = {i, j};
auto check = [&](int v) -> bool {
vector<vector<int>> m(n, vector<int>(n, INF));
m[start.first][start.second] = 0;
q.push(start);
while(q.size()){
auto [x, y] = q.front();
q.pop();
int next = m[x][y] + 1;
rep(4){
int x2 = x + dx[i], y2 = y + dy[i];
if(x2 < 0 || y2 < 0 || x2 >= n || y2 >= n || a[x2][y2] == 'T') continue;
if(next / s + v < bee[x2][y2] && chmin(m[x2][y2], next)) q.emplace(x2, y2);
}
}
return m[goal.first][goal.second] != INF;
};
int ok = -1, ng = bee[start.first][start.second];
while(ng - ok > 1){
int cen = (ok + ng) / 2;
if(check(cen)) ok = cen;
else ng = cen;
}
cout << ok << endl;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:34:75: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
if(x2 < 0 || y2 < 0 || x2 >= n || y2 >= n || a[x2][y2] != 'G' && a[x2][y2] != 'M') continue;
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
114 ms |
6156 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
10 ms |
1560 KB |
Output is correct |
34 |
Correct |
11 ms |
1560 KB |
Output is correct |
35 |
Correct |
25 ms |
1500 KB |
Output is correct |
36 |
Correct |
13 ms |
1884 KB |
Output is correct |
37 |
Correct |
14 ms |
1792 KB |
Output is correct |
38 |
Correct |
38 ms |
1884 KB |
Output is correct |
39 |
Correct |
15 ms |
2256 KB |
Output is correct |
40 |
Correct |
16 ms |
2224 KB |
Output is correct |
41 |
Correct |
45 ms |
2224 KB |
Output is correct |
42 |
Correct |
17 ms |
2696 KB |
Output is correct |
43 |
Correct |
19 ms |
2724 KB |
Output is correct |
44 |
Correct |
55 ms |
2692 KB |
Output is correct |
45 |
Correct |
20 ms |
3168 KB |
Output is correct |
46 |
Correct |
22 ms |
3108 KB |
Output is correct |
47 |
Correct |
66 ms |
3136 KB |
Output is correct |
48 |
Correct |
23 ms |
3648 KB |
Output is correct |
49 |
Correct |
25 ms |
3712 KB |
Output is correct |
50 |
Correct |
77 ms |
3632 KB |
Output is correct |
51 |
Correct |
28 ms |
4188 KB |
Output is correct |
52 |
Correct |
29 ms |
4208 KB |
Output is correct |
53 |
Correct |
91 ms |
4200 KB |
Output is correct |
54 |
Correct |
32 ms |
4832 KB |
Output is correct |
55 |
Correct |
33 ms |
4852 KB |
Output is correct |
56 |
Correct |
117 ms |
4816 KB |
Output is correct |
57 |
Correct |
36 ms |
5480 KB |
Output is correct |
58 |
Correct |
38 ms |
5488 KB |
Output is correct |
59 |
Correct |
129 ms |
5452 KB |
Output is correct |
60 |
Correct |
40 ms |
6152 KB |
Output is correct |
61 |
Correct |
44 ms |
6272 KB |
Output is correct |
62 |
Correct |
169 ms |
6136 KB |
Output is correct |
63 |
Correct |
136 ms |
6156 KB |
Output is correct |
64 |
Correct |
227 ms |
6164 KB |
Output is correct |
65 |
Correct |
223 ms |
6164 KB |
Output is correct |
66 |
Correct |
156 ms |
6204 KB |
Output is correct |
67 |
Correct |
144 ms |
6272 KB |
Output is correct |
68 |
Correct |
63 ms |
6176 KB |
Output is correct |
69 |
Correct |
56 ms |
6160 KB |
Output is correct |
70 |
Correct |
51 ms |
6168 KB |
Output is correct |
71 |
Correct |
44 ms |
6184 KB |
Output is correct |
72 |
Correct |
44 ms |
6208 KB |
Output is correct |
73 |
Correct |
50 ms |
6136 KB |
Output is correct |
74 |
Correct |
86 ms |
6236 KB |
Output is correct |
75 |
Correct |
81 ms |
6152 KB |
Output is correct |
76 |
Correct |
85 ms |
6180 KB |
Output is correct |
77 |
Correct |
90 ms |
6144 KB |
Output is correct |
78 |
Correct |
113 ms |
6228 KB |
Output is correct |
79 |
Correct |
77 ms |
6200 KB |
Output is correct |
80 |
Correct |
83 ms |
6200 KB |
Output is correct |
81 |
Correct |
90 ms |
6152 KB |
Output is correct |
82 |
Correct |
82 ms |
6144 KB |
Output is correct |
83 |
Correct |
108 ms |
6272 KB |
Output is correct |
84 |
Correct |
89 ms |
6148 KB |
Output is correct |
85 |
Correct |
89 ms |
6156 KB |
Output is correct |
86 |
Correct |
107 ms |
6152 KB |
Output is correct |
87 |
Correct |
91 ms |
6160 KB |
Output is correct |
88 |
Correct |
106 ms |
6184 KB |
Output is correct |
89 |
Correct |
104 ms |
6160 KB |
Output is correct |
90 |
Correct |
115 ms |
6152 KB |
Output is correct |
91 |
Correct |
114 ms |
6148 KB |
Output is correct |
92 |
Correct |
115 ms |
6240 KB |
Output is correct |