#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <stack>
#include <cstring>
#include <chrono>
#include <random>
#include <numeric>
#include <bitset>
#include <array>
#include <functional>
#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << ": " << x << '\n'
#define uniq(x) x.erase(unique(all(x)), x.end())
#define rall(x) x.rbegin(), x.rend()
#define each(x, v) for(auto &x : v)
#define mp make_pair
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
void setIO() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int n, s;
int dr[4] = {0, -1, 0, 1};
int dc[4] = {-1, 0, 1, 0};
char mat[800][800];
vector<pii> hives;
pii mecho, home;
int dist[800][800][2];
bool vis[800][800];
bool ok(int r, int c) {
return (r >= 0 && r < n && c >= 0 && c < n && !vis[r][c] && mat[r][c] != 'T');
}
void hive_fill() {
queue<pair<int, int> > q;
for(pii &x : hives) {
q.push(x);
vis[x.first][x.second] = true;
}
while(!q.empty()) {
int r = q.front().first;
int c = q.front().second;
q.pop();
for(int i=0; i<4; i++) {
int newR = r + dr[i];
int newC = c + dc[i];
if(!ok(newR, newC) || mat[newR][newC] == 'D') continue;
vis[newR][newC] = true;
dist[newR][newC][1] = dist[r][c][1] + 1;
// if(mat[newR][newC] == 'G')
q.push({ newR, newC });
}
}
}
int calcMinutes(int steps) {
return steps / s;
}
bool check(int mid) {
queue<pii> q;
memset(vis, 0, sizeof(vis));
q.push(mecho);
vis[mecho.first][mecho.second] = true;
while(!q.empty()) {
int r = q.front().first;
int c = q.front().second;
q.pop();
if(r == home.first && c == home.second)
return true;
for(int i=0; i<4; i++) {
int newR = r + dr[i];
int newC = c + dc[i];
if(!ok(newR, newC)) continue;
if(mid >= dist[newR][newC][1] && mat[newR][newC] != 'D')
continue;
if(mid + calcMinutes(dist[r][c][0] + 1) >= dist[newR][newC][1] && mat[newR][newC] != 'D')
continue;
q.push({ newR, newC });
vis[newR][newC] = true;
dist[newR][newC][0] = dist[r][c][0] + 1;
}
}
return false;
}
int32_t main() {
setIO();
cin >> n >> s;
memset(dist, 0, sizeof(dist));
memset(vis, 0, sizeof(vis));
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
cin >> mat[i][j];
if(mat[i][j] == 'H') hives.pb({i, j});
else if(mat[i][j] == 'M') mecho = {i, j};
else if(mat[i][j] == 'D') home = {i, j};
}
}
hive_fill();
int ans = -1;
int l=0, r=dist[mecho.first][mecho.second][1]-1;
while(l <= r) {
int mid = (l + r) / 2;
if(check(mid)) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
// for(int i=0; i<n; i++) {
// for(int j=0; j<n; j++) {
// cout << dist[i][j][1];
// if(j == n-1) cout << '\n';
// else cout << " ";
// }
// }
// cout << "-----------------\n";
// for(int i=0; i<n; i++) {
// for(int j=0; j<n; j++) {
// cout << dist[i][j][0];
// if(j == n-1) cout << '\n';
// else cout << " ";
// }
// }
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5844 KB |
Output is correct |
2 |
Correct |
2 ms |
5844 KB |
Output is correct |
3 |
Correct |
2 ms |
5844 KB |
Output is correct |
4 |
Correct |
2 ms |
5884 KB |
Output is correct |
5 |
Correct |
2 ms |
5844 KB |
Output is correct |
6 |
Correct |
2 ms |
5844 KB |
Output is correct |
7 |
Correct |
97 ms |
6808 KB |
Output is correct |
8 |
Correct |
2 ms |
5972 KB |
Output is correct |
9 |
Correct |
3 ms |
5844 KB |
Output is correct |
10 |
Correct |
3 ms |
5972 KB |
Output is correct |
11 |
Correct |
2 ms |
5972 KB |
Output is correct |
12 |
Correct |
3 ms |
5972 KB |
Output is correct |
13 |
Correct |
2 ms |
5972 KB |
Output is correct |
14 |
Correct |
3 ms |
5972 KB |
Output is correct |
15 |
Correct |
2 ms |
5972 KB |
Output is correct |
16 |
Correct |
2 ms |
5972 KB |
Output is correct |
17 |
Correct |
2 ms |
5972 KB |
Output is correct |
18 |
Correct |
2 ms |
5972 KB |
Output is correct |
19 |
Correct |
2 ms |
5972 KB |
Output is correct |
20 |
Correct |
2 ms |
5972 KB |
Output is correct |
21 |
Correct |
2 ms |
5972 KB |
Output is correct |
22 |
Correct |
2 ms |
5972 KB |
Output is correct |
23 |
Correct |
2 ms |
5972 KB |
Output is correct |
24 |
Correct |
3 ms |
5972 KB |
Output is correct |
25 |
Correct |
4 ms |
5972 KB |
Output is correct |
26 |
Correct |
2 ms |
5972 KB |
Output is correct |
27 |
Correct |
3 ms |
5972 KB |
Output is correct |
28 |
Correct |
3 ms |
5972 KB |
Output is correct |
29 |
Correct |
4 ms |
5972 KB |
Output is correct |
30 |
Correct |
3 ms |
5972 KB |
Output is correct |
31 |
Correct |
3 ms |
5972 KB |
Output is correct |
32 |
Correct |
2 ms |
5972 KB |
Output is correct |
33 |
Correct |
5 ms |
6228 KB |
Output is correct |
34 |
Correct |
5 ms |
6228 KB |
Output is correct |
35 |
Correct |
20 ms |
6236 KB |
Output is correct |
36 |
Correct |
6 ms |
6280 KB |
Output is correct |
37 |
Correct |
6 ms |
6152 KB |
Output is correct |
38 |
Correct |
27 ms |
6168 KB |
Output is correct |
39 |
Correct |
6 ms |
6228 KB |
Output is correct |
40 |
Correct |
6 ms |
6316 KB |
Output is correct |
41 |
Correct |
37 ms |
6228 KB |
Output is correct |
42 |
Correct |
7 ms |
6356 KB |
Output is correct |
43 |
Correct |
7 ms |
6352 KB |
Output is correct |
44 |
Correct |
42 ms |
6356 KB |
Output is correct |
45 |
Correct |
8 ms |
6356 KB |
Output is correct |
46 |
Correct |
8 ms |
6356 KB |
Output is correct |
47 |
Correct |
61 ms |
6356 KB |
Output is correct |
48 |
Correct |
12 ms |
6356 KB |
Output is correct |
49 |
Correct |
10 ms |
6356 KB |
Output is correct |
50 |
Correct |
58 ms |
6432 KB |
Output is correct |
51 |
Correct |
10 ms |
6356 KB |
Output is correct |
52 |
Correct |
11 ms |
6372 KB |
Output is correct |
53 |
Correct |
72 ms |
6476 KB |
Output is correct |
54 |
Correct |
18 ms |
6488 KB |
Output is correct |
55 |
Correct |
12 ms |
6516 KB |
Output is correct |
56 |
Correct |
83 ms |
6508 KB |
Output is correct |
57 |
Correct |
13 ms |
6480 KB |
Output is correct |
58 |
Correct |
14 ms |
6500 KB |
Output is correct |
59 |
Correct |
92 ms |
6536 KB |
Output is correct |
60 |
Correct |
14 ms |
6484 KB |
Output is correct |
61 |
Correct |
14 ms |
6484 KB |
Output is correct |
62 |
Correct |
109 ms |
6576 KB |
Output is correct |
63 |
Correct |
90 ms |
6568 KB |
Output is correct |
64 |
Correct |
152 ms |
6576 KB |
Output is correct |
65 |
Correct |
151 ms |
6576 KB |
Output is correct |
66 |
Correct |
108 ms |
6572 KB |
Output is correct |
67 |
Correct |
98 ms |
6572 KB |
Output is correct |
68 |
Correct |
41 ms |
6612 KB |
Output is correct |
69 |
Correct |
36 ms |
6604 KB |
Output is correct |
70 |
Correct |
35 ms |
6612 KB |
Output is correct |
71 |
Correct |
28 ms |
6592 KB |
Output is correct |
72 |
Correct |
28 ms |
6608 KB |
Output is correct |
73 |
Correct |
35 ms |
6776 KB |
Output is correct |
74 |
Correct |
56 ms |
6876 KB |
Output is correct |
75 |
Correct |
56 ms |
6848 KB |
Output is correct |
76 |
Correct |
58 ms |
6848 KB |
Output is correct |
77 |
Correct |
62 ms |
6876 KB |
Output is correct |
78 |
Correct |
68 ms |
6836 KB |
Output is correct |
79 |
Correct |
48 ms |
6808 KB |
Output is correct |
80 |
Correct |
58 ms |
6836 KB |
Output is correct |
81 |
Correct |
65 ms |
6836 KB |
Output is correct |
82 |
Correct |
54 ms |
6740 KB |
Output is correct |
83 |
Correct |
73 ms |
6804 KB |
Output is correct |
84 |
Correct |
60 ms |
6788 KB |
Output is correct |
85 |
Correct |
61 ms |
6740 KB |
Output is correct |
86 |
Correct |
72 ms |
6740 KB |
Output is correct |
87 |
Correct |
63 ms |
6792 KB |
Output is correct |
88 |
Correct |
73 ms |
6740 KB |
Output is correct |
89 |
Correct |
71 ms |
6744 KB |
Output is correct |
90 |
Correct |
79 ms |
6740 KB |
Output is correct |
91 |
Correct |
75 ms |
6740 KB |
Output is correct |
92 |
Correct |
84 ms |
6756 KB |
Output is correct |