#include <bits/stdc++.h>
using namespace std;
//Soltan Cristian
#define ll long long
#define all(x) x.begin(), x.end()
#define mod 1000000007
#define pb push_back
#define st first
#define nd second
#define sz(x) (ll)x.size()
#define rall(x) x.rbegin(), x.rend()
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
const int mxa = 2 * 1e5 + 2;
// string __fname = "maxflow";
// ifstream in(__fname + ".in");
// ofstream out (__fname + ".out");
// #define cin in
// #define cout out
int n, s;
vector<vector<char>> mapa;
bool validB(int x, int y){
if(x >= 0 && x < n && y >= 0 && y < n && mapa[x][y] != 'T' && mapa[x][y] != 'D'){
return true;
}
return false;
}
bool valid(int x, int y){
if(x >= 0 && x < n && y >= 0 && y < n && mapa[x][y] != 'T'){
return true;
}
return false;
}
void solve(){
cin >> n >> s;
mapa = vector<vector<char>>(n, vector<char>(n));
vector<pair<int,int>> stup;
pair<int, int> start, finish;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
char x;
cin >> x;
if(x == 'H') stup.pb({i, j});
if(x == 'M') start = {i, j};
if(x == 'D') finish = {i, j};
mapa[i][j] = x;
}
}
vector<vector<int>> bees(n, vector<int>(n, INT_MAX));
queue<pair<int, int>> qB;
for(int i = 0; i < sz(stup); i++){
bees[stup[i].st][stup[i].nd] = 0;
qB.push({stup[i].st, stup[i].nd});
}
// cout << "a\n";
while(!qB.empty()){
pair<int, int> u = qB.front();
qB.pop();
for(int i = 0; i < 4; i++){
if(!validB(u.st + dx[i], u.nd + dy[i])) continue;
if(bees[u.st][u.nd] + s < bees[u.st + dx[i]][u.nd + dy[i]]){
qB.push({u.st + dx[i], u.nd + dy[i]});
bees[u.st + dx[i]][u.nd + dy[i]] = bees[u.st][u.nd] + s;
}
}
}
int l = 0, r = 2 * n * n;
int ans = 0;
while(l <= r){
int mid = (l + r) / 2;
vector<vector<int>> h(n, vector<int>(n, INT_MAX));
h[start.st][start.nd] = mid;
queue<pair<int,int>> q;
q.push({start.st, start.nd});
while(!q.empty()){
pair<int, int> u = q.front();
q.pop();
for(int i = 0; i < 4; i++){
if(!valid(u.st + dx[i], u.nd + dy[i])) continue;
if(h[u.st][u.nd] + 1 < bees[u.st + dx[i]][u.nd + dy[i]] &&
h[u.st][u.nd] + 1 < h[u.st + dx[i]][u.nd + dy[i]]){
q.push({u.st + dx[i], u.nd + dy[i]});
h[u.st + dx[i]][u.nd + dy[i]] = h[u.st][u.nd] + 1;
}
}
}
if(h[finish.st][finish.nd] < INT_MAX){
ans = max(ans, mid);
l = mid + 1;
}
else r = mid - 1;
}
// cout << ans << '\n';
cout << ans / s << '\n';
}
int main()
{
// ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << (setprecision(10));
// int t;
// cin >> t;
// while(t--)
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
356 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
135 ms |
6356 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 |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
14 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
33 |
Correct |
9 ms |
1544 KB |
Output is correct |
34 |
Incorrect |
25 ms |
1548 KB |
Output isn't correct |
35 |
Correct |
36 ms |
1668 KB |
Output is correct |
36 |
Correct |
11 ms |
1884 KB |
Output is correct |
37 |
Incorrect |
35 ms |
1896 KB |
Output isn't correct |
38 |
Correct |
53 ms |
1880 KB |
Output is correct |
39 |
Correct |
14 ms |
2264 KB |
Output is correct |
40 |
Incorrect |
42 ms |
2244 KB |
Output isn't correct |
41 |
Correct |
62 ms |
2432 KB |
Output is correct |
42 |
Correct |
21 ms |
2688 KB |
Output is correct |
43 |
Incorrect |
58 ms |
2668 KB |
Output isn't correct |
44 |
Correct |
79 ms |
2888 KB |
Output is correct |
45 |
Correct |
21 ms |
3120 KB |
Output is correct |
46 |
Incorrect |
66 ms |
3384 KB |
Output isn't correct |
47 |
Correct |
92 ms |
3372 KB |
Output is correct |
48 |
Correct |
32 ms |
3716 KB |
Output is correct |
49 |
Incorrect |
78 ms |
3680 KB |
Output isn't correct |
50 |
Correct |
116 ms |
3848 KB |
Output is correct |
51 |
Correct |
30 ms |
4176 KB |
Output is correct |
52 |
Incorrect |
95 ms |
4188 KB |
Output isn't correct |
53 |
Correct |
131 ms |
4564 KB |
Output is correct |
54 |
Correct |
37 ms |
4880 KB |
Output is correct |
55 |
Incorrect |
111 ms |
5092 KB |
Output isn't correct |
56 |
Correct |
170 ms |
5140 KB |
Output is correct |
57 |
Correct |
42 ms |
5460 KB |
Output is correct |
58 |
Incorrect |
127 ms |
5676 KB |
Output isn't correct |
59 |
Correct |
186 ms |
5832 KB |
Output is correct |
60 |
Correct |
46 ms |
6244 KB |
Output is correct |
61 |
Incorrect |
160 ms |
6464 KB |
Output isn't correct |
62 |
Correct |
225 ms |
6556 KB |
Output is correct |
63 |
Correct |
118 ms |
6152 KB |
Output is correct |
64 |
Correct |
262 ms |
6344 KB |
Output is correct |
65 |
Correct |
162 ms |
6188 KB |
Output is correct |
66 |
Correct |
149 ms |
6160 KB |
Output is correct |
67 |
Incorrect |
130 ms |
6152 KB |
Output isn't correct |
68 |
Correct |
99 ms |
6316 KB |
Output is correct |
69 |
Correct |
89 ms |
6208 KB |
Output is correct |
70 |
Correct |
69 ms |
6292 KB |
Output is correct |
71 |
Correct |
64 ms |
6216 KB |
Output is correct |
72 |
Incorrect |
57 ms |
6224 KB |
Output isn't correct |
73 |
Incorrect |
83 ms |
6664 KB |
Output isn't correct |
74 |
Correct |
135 ms |
6612 KB |
Output is correct |
75 |
Correct |
106 ms |
6676 KB |
Output is correct |
76 |
Correct |
94 ms |
6684 KB |
Output is correct |
77 |
Correct |
106 ms |
6660 KB |
Output is correct |
78 |
Incorrect |
111 ms |
6636 KB |
Output isn't correct |
79 |
Correct |
101 ms |
6640 KB |
Output is correct |
80 |
Correct |
107 ms |
6572 KB |
Output is correct |
81 |
Correct |
105 ms |
6616 KB |
Output is correct |
82 |
Correct |
95 ms |
6612 KB |
Output is correct |
83 |
Correct |
111 ms |
6664 KB |
Output is correct |
84 |
Correct |
124 ms |
6524 KB |
Output is correct |
85 |
Correct |
108 ms |
6652 KB |
Output is correct |
86 |
Correct |
113 ms |
6664 KB |
Output is correct |
87 |
Correct |
128 ms |
6872 KB |
Output is correct |
88 |
Correct |
125 ms |
6624 KB |
Output is correct |
89 |
Correct |
144 ms |
6560 KB |
Output is correct |
90 |
Correct |
127 ms |
6864 KB |
Output is correct |
91 |
Correct |
136 ms |
6612 KB |
Output is correct |
92 |
Correct |
121 ms |
6660 KB |
Output is correct |