#include<iostream>
#include<vector>
#include<queue>
#include<string>
#include<fstream>
#include<algorithm>
#define For(i, n) for(int i = 0; i < n; i++)
#define ffor(i, a, n) for(int i = a; i < n; i++)
#define vec vector
#define ff first
#define ss second
#define pb push_back
#define pairs pair<int, int>
#define NEK 1000000000
using namespace std;
vec<pairs> r = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
void BFS(queue<pairs>&q, vec<vec<char>>&p, vec<vec<int>>&d) {
while (!q.empty()) {
pairs x = q.front(); q.pop();
For(i, 4) {
pairs t = { x.ff + r[i].ff, x.ss + r[i].ss };
if (t.ff < 0 || t.ss < 0 || t.ff >= p.size() || t.ss >= p.size()) continue;
if (p[t.ff][t.ss] == 'T' || p[t.ff][t.ss] == 'D') continue;
if (d[t.ff][t.ss] != -1) continue;
d[t.ff][t.ss] = d[x.ff][x.ss] + 1;
q.push(t);
}
}
}
bool BFS2(queue<pairs> q, vec<vec<char>>& p, vec<vec<int>>& d, int& s, pairs& e, int& c) {
vec<vec<int>> d2(p.size(), vec<int>(p.size(), -1));
d2[q.front().ff][q.front().ss] = 0;
if (d2[q.front().ff][q.front().ss] >= (d[q.front().ff][q.front().ss] - c)) return false;
while (!q.empty()) {
pairs x = q.front(); q.pop();
if (x.ff == e.ff && x.ss == e.ss) return true;
For(i, 4) {
pairs t = { x.ff + r[i].ff, x.ss + r[i].ss };
if (t.ff < 0 || t.ss < 0 || t.ff >= p.size() || t.ss >= p.size()) {
continue;
}
if (p[t.ff][t.ss] == 'T' || d2[t.ff][t.ss] != -1) continue;
d2[t.ff][t.ss] = d2[x.ff][x.ss] + 1;
if ((d2[t.ff][t.ss] / s) >= (d[t.ff][t.ss] - c) && d[t.ff][t.ss] != -1) continue;
q.push(t);
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//ifstream cin("atlarge.in");
//ofstream cout("atlarge.out");
int n, v;
cin >> n >> v;
vec<vec<char>> p(n, vec<char>(n));
pairs s, e;
queue<pairs> q;
vec<vec<int>> d1(n, vec<int>(n, -1));
For(i, n) {
For(j, n) {
cin >> p[i][j];
if (p[i][j] == 'M') {
s = { i, j };
}
if (p[i][j] == 'D') {
e = { i, j };
}
if (p[i][j] == 'H') {
q.push({ i, j });
d1[i][j] = 0;
}
}
}
BFS(q, p, d1);
int l = 0, r = n*n;
q.push(s);
while (l < r) {
int c = (l + r) / 2 + 1;
if (BFS2(q, p, d1, v, e, c)) l = c;
else r = c-1;
}
if (BFS2(q, p, d1, v, e, l)) cout << l << '\n';
else cout << -1 << '\n';
return 0;
}
Compilation message
mecho.cpp: In function 'void BFS(std::queue<std::pair<int, int> >&, std::vector<std::vector<char> >&, std::vector<std::vector<int> >&)':
mecho.cpp:25:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | if (t.ff < 0 || t.ss < 0 || t.ff >= p.size() || t.ss >= p.size()) continue;
| ~~~~~^~~~~~~~~~~
mecho.cpp:25:57: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | if (t.ff < 0 || t.ss < 0 || t.ff >= p.size() || t.ss >= p.size()) continue;
| ~~~~~^~~~~~~~~~~
mecho.cpp: In function 'bool BFS2(std::queue<std::pair<int, int> >, std::vector<std::vector<char> >&, std::vector<std::vector<int> >&, int&, std::pair<int, int>&, int&)':
mecho.cpp:43:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | if (t.ff < 0 || t.ss < 0 || t.ff >= p.size() || t.ss >= p.size()) {
| ~~~~~^~~~~~~~~~~
mecho.cpp:43:57: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | if (t.ff < 0 || t.ss < 0 || t.ff >= p.size() || t.ss >= p.size()) {
| ~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
84 ms |
6288 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
6 ms |
1572 KB |
Output is correct |
34 |
Correct |
5 ms |
1572 KB |
Output is correct |
35 |
Correct |
18 ms |
1612 KB |
Output is correct |
36 |
Correct |
6 ms |
1884 KB |
Output is correct |
37 |
Correct |
6 ms |
1912 KB |
Output is correct |
38 |
Correct |
22 ms |
1884 KB |
Output is correct |
39 |
Correct |
8 ms |
2332 KB |
Output is correct |
40 |
Correct |
8 ms |
2284 KB |
Output is correct |
41 |
Correct |
32 ms |
2260 KB |
Output is correct |
42 |
Correct |
10 ms |
2700 KB |
Output is correct |
43 |
Correct |
9 ms |
2700 KB |
Output is correct |
44 |
Correct |
45 ms |
2708 KB |
Output is correct |
45 |
Correct |
12 ms |
3212 KB |
Output is correct |
46 |
Correct |
12 ms |
3212 KB |
Output is correct |
47 |
Correct |
42 ms |
3248 KB |
Output is correct |
48 |
Correct |
16 ms |
3752 KB |
Output is correct |
49 |
Correct |
14 ms |
3756 KB |
Output is correct |
50 |
Correct |
56 ms |
3688 KB |
Output is correct |
51 |
Correct |
19 ms |
4308 KB |
Output is correct |
52 |
Correct |
17 ms |
4188 KB |
Output is correct |
53 |
Correct |
73 ms |
4292 KB |
Output is correct |
54 |
Correct |
20 ms |
4900 KB |
Output is correct |
55 |
Correct |
19 ms |
4900 KB |
Output is correct |
56 |
Correct |
90 ms |
4884 KB |
Output is correct |
57 |
Correct |
25 ms |
5508 KB |
Output is correct |
58 |
Correct |
25 ms |
5508 KB |
Output is correct |
59 |
Correct |
86 ms |
5496 KB |
Output is correct |
60 |
Correct |
25 ms |
6472 KB |
Output is correct |
61 |
Correct |
26 ms |
6248 KB |
Output is correct |
62 |
Correct |
116 ms |
6208 KB |
Output is correct |
63 |
Correct |
116 ms |
6664 KB |
Output is correct |
64 |
Correct |
168 ms |
6180 KB |
Output is correct |
65 |
Correct |
155 ms |
6192 KB |
Output is correct |
66 |
Correct |
122 ms |
6220 KB |
Output is correct |
67 |
Correct |
125 ms |
6208 KB |
Output is correct |
68 |
Correct |
64 ms |
6224 KB |
Output is correct |
69 |
Correct |
52 ms |
6252 KB |
Output is correct |
70 |
Correct |
44 ms |
6220 KB |
Output is correct |
71 |
Correct |
46 ms |
6244 KB |
Output is correct |
72 |
Correct |
45 ms |
6292 KB |
Output is correct |
73 |
Correct |
43 ms |
6332 KB |
Output is correct |
74 |
Correct |
57 ms |
6264 KB |
Output is correct |
75 |
Correct |
73 ms |
6284 KB |
Output is correct |
76 |
Correct |
61 ms |
6284 KB |
Output is correct |
77 |
Correct |
67 ms |
6468 KB |
Output is correct |
78 |
Correct |
85 ms |
6208 KB |
Output is correct |
79 |
Correct |
49 ms |
6252 KB |
Output is correct |
80 |
Correct |
63 ms |
6216 KB |
Output is correct |
81 |
Correct |
68 ms |
6240 KB |
Output is correct |
82 |
Correct |
60 ms |
6172 KB |
Output is correct |
83 |
Correct |
77 ms |
6296 KB |
Output is correct |
84 |
Correct |
78 ms |
6316 KB |
Output is correct |
85 |
Correct |
78 ms |
6240 KB |
Output is correct |
86 |
Correct |
79 ms |
6232 KB |
Output is correct |
87 |
Correct |
73 ms |
6240 KB |
Output is correct |
88 |
Correct |
78 ms |
6256 KB |
Output is correct |
89 |
Correct |
74 ms |
6088 KB |
Output is correct |
90 |
Correct |
80 ms |
6232 KB |
Output is correct |
91 |
Correct |
88 ms |
6172 KB |
Output is correct |
92 |
Correct |
84 ms |
6244 KB |
Output is correct |