#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 = 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 |
348 KB |
Output is correct |
2 |
Correct |
1 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 |
344 KB |
Output is correct |
7 |
Correct |
137 ms |
6204 KB |
Output is correct |
8 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
9 |
Correct |
1 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 |
2 ms |
348 KB |
Output isn't correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
17 |
Correct |
0 ms |
600 KB |
Output is correct |
18 |
Incorrect |
1 ms |
496 KB |
Output isn't correct |
19 |
Correct |
0 ms |
344 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 |
344 KB |
Output isn't correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
29 |
Correct |
1 ms |
344 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 |
2 ms |
344 KB |
Output isn't correct |
33 |
Correct |
8 ms |
1540 KB |
Output is correct |
34 |
Incorrect |
24 ms |
1744 KB |
Output isn't correct |
35 |
Correct |
36 ms |
1540 KB |
Output is correct |
36 |
Correct |
11 ms |
1880 KB |
Output is correct |
37 |
Incorrect |
32 ms |
1884 KB |
Output isn't correct |
38 |
Correct |
49 ms |
1884 KB |
Output is correct |
39 |
Correct |
17 ms |
2256 KB |
Output is correct |
40 |
Incorrect |
41 ms |
2244 KB |
Output isn't correct |
41 |
Correct |
62 ms |
2240 KB |
Output is correct |
42 |
Correct |
18 ms |
2732 KB |
Output is correct |
43 |
Incorrect |
52 ms |
2708 KB |
Output isn't correct |
44 |
Correct |
96 ms |
2696 KB |
Output is correct |
45 |
Correct |
21 ms |
3144 KB |
Output is correct |
46 |
Incorrect |
63 ms |
3132 KB |
Output isn't correct |
47 |
Correct |
93 ms |
3180 KB |
Output is correct |
48 |
Correct |
24 ms |
3720 KB |
Output is correct |
49 |
Incorrect |
76 ms |
3680 KB |
Output isn't correct |
50 |
Correct |
115 ms |
3660 KB |
Output is correct |
51 |
Correct |
31 ms |
4188 KB |
Output is correct |
52 |
Incorrect |
89 ms |
4204 KB |
Output isn't correct |
53 |
Correct |
136 ms |
4596 KB |
Output is correct |
54 |
Correct |
39 ms |
4880 KB |
Output is correct |
55 |
Incorrect |
104 ms |
4804 KB |
Output isn't correct |
56 |
Correct |
163 ms |
4816 KB |
Output is correct |
57 |
Correct |
40 ms |
5460 KB |
Output is correct |
58 |
Incorrect |
126 ms |
5480 KB |
Output isn't correct |
59 |
Correct |
179 ms |
5464 KB |
Output is correct |
60 |
Correct |
45 ms |
6160 KB |
Output is correct |
61 |
Incorrect |
147 ms |
6360 KB |
Output isn't correct |
62 |
Correct |
213 ms |
6152 KB |
Output is correct |
63 |
Correct |
117 ms |
6176 KB |
Output is correct |
64 |
Incorrect |
312 ms |
6308 KB |
Output isn't correct |
65 |
Correct |
163 ms |
6176 KB |
Output is correct |
66 |
Correct |
149 ms |
6156 KB |
Output is correct |
67 |
Incorrect |
133 ms |
6332 KB |
Output isn't correct |
68 |
Correct |
88 ms |
6200 KB |
Output is correct |
69 |
Correct |
81 ms |
6208 KB |
Output is correct |
70 |
Correct |
66 ms |
6216 KB |
Output is correct |
71 |
Correct |
65 ms |
6376 KB |
Output is correct |
72 |
Incorrect |
56 ms |
6224 KB |
Output isn't correct |
73 |
Incorrect |
70 ms |
6252 KB |
Output isn't correct |
74 |
Correct |
106 ms |
6212 KB |
Output is correct |
75 |
Correct |
110 ms |
6244 KB |
Output is correct |
76 |
Correct |
95 ms |
6492 KB |
Output is correct |
77 |
Correct |
106 ms |
6192 KB |
Output is correct |
78 |
Incorrect |
109 ms |
6216 KB |
Output isn't correct |
79 |
Correct |
102 ms |
6204 KB |
Output is correct |
80 |
Correct |
98 ms |
6196 KB |
Output is correct |
81 |
Correct |
100 ms |
6208 KB |
Output is correct |
82 |
Correct |
97 ms |
6232 KB |
Output is correct |
83 |
Correct |
112 ms |
6216 KB |
Output is correct |
84 |
Correct |
125 ms |
6648 KB |
Output is correct |
85 |
Correct |
105 ms |
6232 KB |
Output is correct |
86 |
Correct |
111 ms |
6248 KB |
Output is correct |
87 |
Correct |
114 ms |
6192 KB |
Output is correct |
88 |
Correct |
120 ms |
6224 KB |
Output is correct |
89 |
Correct |
144 ms |
6196 KB |
Output is correct |
90 |
Correct |
148 ms |
6180 KB |
Output is correct |
91 |
Correct |
135 ms |
6200 KB |
Output is correct |
92 |
Correct |
122 ms |
6196 KB |
Output is correct |