#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define LSB(x) ((x) & -(x))
#define sz(x) (int)(x.size())
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef DEBUG
#define dbg(x...) cerr << "\e[DEBUG] "<<__func__<<": "<<__LINE__<<", [" << #x << "] = ["; _print(x);
#else
#define dbg(x...)
#endif
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
size_t operator()(string x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
uint64_t res = 0;
for (int i = 0; i < sz(x); i++){
res ^= x[i] + FIXED_RANDOM;
res = splitmix64(res);
}
return res;
}
};
const ll MOD = 1e9 + 7;
const ll INFLL = 1e18 + 5;
const int INF = 1e9 + 5;
char nl = '\n';
ll modPow(ll n, ll k){
if (k == 0) return 1;
ll res = modPow(n, k / 2);
res = res * res % MOD;
if (k & 1) return res * n % MOD;
return res;
}
const int mxN = 51;
int dr[4] = {0, 0, 1, -1};
int dc[4] = {1, -1, 0, 0};
void TestCase(){
int n, m;
cin >> n >> m;
vector<string> grid(n);
vector<vector<int>> bad(n, vector<int>(n, INF));
vector<vector<int>> dist(n, vector<int>(n, INF));
pair<int, int> s, e;
queue<pair<int, int>> q;
for (int i = 0; i < n; i++){
cin >> grid[i];
for (int j = 0; j < n; j++){
if (grid[i][j] == 'H') {
bad[i][j] = 0;
q.push({i, j});
} else if (grid[i][j] == 'M'){
s = {i, j};
} else if (grid[i][j] == 'D'){
e = {i, j};
}
}
}
auto inside = [&](int i, int j) -> bool { return i >= 0 && i < n && j >= 0 && j < n; };
while (!q.empty()){
auto now = q.front(); q.pop();
for (int i = 0; i < 4; i++){
int nr = now.f + dr[i];
int nc = now.s + dc[i];
if (!inside(nr, nc) || grid[nr][nc] != 'G') continue;
if (bad[nr][nc] == INF){
bad[nr][nc] = bad[now.f][now.s] + 1;
q.push({nr, nc});
}
}
}
auto check = [&](int mins) -> bool {
dist.assign(n, vector<int>(n, INF));
q.push(s);
dist[s.f][s.s] = 0;
while (!q.empty()){
auto now = q.front(); q.pop();
int elap = mins + dist[now.f][now.s] / m;
for (int i = 0; i < 4; i++){
int nr = now.f + dr[i];
int nc = now.s + dc[i];
if (!inside(nr, nc) || (grid[nr][nc] != 'G' && grid[nr][nc] != 'D') || elap >= bad[nr][nc]) continue;
if (dist[nr][nc] == INF){
dist[nr][nc] = dist[now.f][now.s] + 1;
q.push({nr, nc});
}
}
}
return dist[e.f][e.s] != INF;
};
int lo = 0, hi = 1e6;
int ans = -1;
while (lo <= hi){
int mid = (lo + hi) / 2;
if (check(mid)){
lo = mid+1;
ans = mid;
} else {
hi = mid-1;
}
}
cout << ans << nl;
}
int main(){
//ios_base::sync_with_stdio(0);
//cin.tie(0);
int T = 1;
//cin >> T;
while (T--){
TestCase();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
308 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
308 KB |
Output isn't correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
164 ms |
6888 KB |
Output is correct |
8 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
304 KB |
Output isn't correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
16 |
Correct |
1 ms |
308 KB |
Output is correct |
17 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Incorrect |
1 ms |
304 KB |
Output isn't correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Incorrect |
7 ms |
1492 KB |
Output isn't correct |
34 |
Correct |
7 ms |
1464 KB |
Output is correct |
35 |
Correct |
24 ms |
1492 KB |
Output is correct |
36 |
Incorrect |
7 ms |
1876 KB |
Output isn't correct |
37 |
Correct |
6 ms |
1876 KB |
Output is correct |
38 |
Correct |
31 ms |
1876 KB |
Output is correct |
39 |
Incorrect |
9 ms |
2260 KB |
Output isn't correct |
40 |
Correct |
11 ms |
2332 KB |
Output is correct |
41 |
Correct |
37 ms |
2260 KB |
Output is correct |
42 |
Incorrect |
13 ms |
2972 KB |
Output isn't correct |
43 |
Correct |
14 ms |
3036 KB |
Output is correct |
44 |
Correct |
62 ms |
3028 KB |
Output is correct |
45 |
Incorrect |
14 ms |
3540 KB |
Output isn't correct |
46 |
Correct |
14 ms |
3528 KB |
Output is correct |
47 |
Correct |
60 ms |
3524 KB |
Output is correct |
48 |
Incorrect |
16 ms |
4024 KB |
Output isn't correct |
49 |
Correct |
19 ms |
4112 KB |
Output is correct |
50 |
Correct |
96 ms |
4108 KB |
Output is correct |
51 |
Incorrect |
23 ms |
4700 KB |
Output isn't correct |
52 |
Correct |
20 ms |
4592 KB |
Output is correct |
53 |
Correct |
101 ms |
4692 KB |
Output is correct |
54 |
Incorrect |
21 ms |
5332 KB |
Output isn't correct |
55 |
Correct |
26 ms |
5284 KB |
Output is correct |
56 |
Correct |
121 ms |
5352 KB |
Output is correct |
57 |
Incorrect |
29 ms |
6028 KB |
Output isn't correct |
58 |
Correct |
25 ms |
5972 KB |
Output is correct |
59 |
Correct |
118 ms |
6048 KB |
Output is correct |
60 |
Incorrect |
26 ms |
6740 KB |
Output isn't correct |
61 |
Correct |
28 ms |
6788 KB |
Output is correct |
62 |
Correct |
175 ms |
6780 KB |
Output is correct |
63 |
Correct |
118 ms |
6908 KB |
Output is correct |
64 |
Correct |
165 ms |
6740 KB |
Output is correct |
65 |
Correct |
169 ms |
6776 KB |
Output is correct |
66 |
Incorrect |
151 ms |
6776 KB |
Output isn't correct |
67 |
Correct |
111 ms |
6676 KB |
Output is correct |
68 |
Correct |
53 ms |
6804 KB |
Output is correct |
69 |
Correct |
57 ms |
6692 KB |
Output is correct |
70 |
Correct |
49 ms |
6796 KB |
Output is correct |
71 |
Correct |
42 ms |
6732 KB |
Output is correct |
72 |
Incorrect |
43 ms |
6804 KB |
Output isn't correct |
73 |
Incorrect |
91 ms |
7048 KB |
Output isn't correct |
74 |
Correct |
91 ms |
7052 KB |
Output is correct |
75 |
Correct |
82 ms |
6988 KB |
Output is correct |
76 |
Correct |
81 ms |
7060 KB |
Output is correct |
77 |
Correct |
85 ms |
7056 KB |
Output is correct |
78 |
Correct |
97 ms |
7020 KB |
Output is correct |
79 |
Correct |
69 ms |
7092 KB |
Output is correct |
80 |
Correct |
72 ms |
7032 KB |
Output is correct |
81 |
Correct |
84 ms |
7024 KB |
Output is correct |
82 |
Correct |
74 ms |
7028 KB |
Output is correct |
83 |
Correct |
103 ms |
6940 KB |
Output is correct |
84 |
Correct |
83 ms |
6944 KB |
Output is correct |
85 |
Correct |
82 ms |
6988 KB |
Output is correct |
86 |
Correct |
120 ms |
6968 KB |
Output is correct |
87 |
Correct |
96 ms |
6984 KB |
Output is correct |
88 |
Correct |
93 ms |
6864 KB |
Output is correct |
89 |
Correct |
96 ms |
6932 KB |
Output is correct |
90 |
Correct |
126 ms |
6932 KB |
Output is correct |
91 |
Correct |
111 ms |
6940 KB |
Output is correct |
92 |
Correct |
89 ms |
6836 KB |
Output is correct |