#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define print(x) for (auto el: x) cout << el << " "; cout << '\n'
#define f first
#define s second
using ll = long long;
using llb = long double;
using vl = vector<ll>;
using pll = pair<ll,ll>;
// functions
void setIO(string s) {freopen((s + ".in").c_str(), "r", stdin);freopen((s + ".out").c_str(), "w", stdout);}
void yes() { cout<<"YES\n"; }
void no() { cout<<"NO\n"; }
// geometry
const double PI = 3.14159265358979323846;
double DEG_to_RAD (double d) {return d*PI/180.0;}
double RAD_to_DEG (double r) {return r*180.0/ PI;}
// values
const ll INF = 1e18;
const ll MOD = 1000000007;
const ll MAX_N = 800;
ll dx[4] = {1,-1,0,0}, dy[4] = {0,0,1,-1};
pll m, house;
ll n, s, db[MAX_N][MAX_N]; queue<pll> qb;
bitset<MAX_N> b[MAX_N]; // board;
bool possible(ll min) {
vector<vl> d(n, vl(n, -1));
queue<pll> q; q.push(m); d[m.f][m.s] = 0;
while(q.size()) {
pll a = q.front(); q.pop();
for (int i=0;i<4;i++) {
ll x = a.f + dx[i], y = a.s + dy[i], c = d[a.f][a.s] + 1;
if (x == house.f && y == house.s) return true;
if (x < 0 || x >= n || y < 0 || y >= n) continue;
if (!b[x][y] || d[x][y] != -1) continue;
if (c/s + min >= db[x][y] && db[x][y] != -1) continue;
d[x][y] = c;
q.push({x,y});
}
}
return false;
}
ll last_true(ll lo, ll hi, function<bool(ll)> f) {
ll ans = -1;
while(lo <= hi) {
ll med = (hi-lo)/2 + lo;
if (f(med)) {
lo = med + 1;
ans = med;
} else {
hi = med - 1;
}
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s;
for (auto &row: db) for (auto &el: row) el = -1;
for (int i=0;i<n;i++) {
string t; cin >> t;
for (int j=0;j<n;j++) {
b[i][j] = t[j] == 'G';
if (t[j] == 'H') {
db[i][j] = 0;
qb.push({i,j});
}
if (t[j] == 'M') {
m = {i,j};
}
if (t[j] == 'D') {
house = {i,j};
}
}
}
while(qb.size()) {
pll a = qb.front(); qb.pop();
for (int i=0;i<4;i++) {
ll x = a.f + dx[i], y = a.s + dy[i];
if (x < 0 || x >= n || y < 0 || y >= n) continue;
if (!b[x][y] || db[x][y] != -1) continue;
db[x][y] = db[a.f][a.s]+1;
qb.push({x,y});
}
}
cout << last_true(0, 1e17, possible) << endl;
}
Compilation message
mecho.cpp: In function 'void setIO(std::string)':
mecho.cpp:15:30: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | void setIO(string s) {freopen((s + ".in").c_str(), "r", stdin);freopen((s + ".out").c_str(), "w", stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:15:71: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | void setIO(string s) {freopen((s + ".in").c_str(), "r", stdin);freopen((s + ".out").c_str(), "w", stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5468 KB |
Output is correct |
2 |
Correct |
1 ms |
5468 KB |
Output is correct |
3 |
Correct |
1 ms |
5468 KB |
Output is correct |
4 |
Correct |
1 ms |
5448 KB |
Output is correct |
5 |
Correct |
2 ms |
5468 KB |
Output is correct |
6 |
Correct |
1 ms |
5464 KB |
Output is correct |
7 |
Correct |
141 ms |
11384 KB |
Output is correct |
8 |
Correct |
1 ms |
5464 KB |
Output is correct |
9 |
Correct |
1 ms |
5468 KB |
Output is correct |
10 |
Correct |
1 ms |
5468 KB |
Output is correct |
11 |
Correct |
1 ms |
5468 KB |
Output is correct |
12 |
Incorrect |
1 ms |
5464 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
5468 KB |
Output isn't correct |
14 |
Correct |
1 ms |
5468 KB |
Output is correct |
15 |
Correct |
1 ms |
5468 KB |
Output is correct |
16 |
Correct |
1 ms |
5336 KB |
Output is correct |
17 |
Correct |
1 ms |
5468 KB |
Output is correct |
18 |
Correct |
2 ms |
5468 KB |
Output is correct |
19 |
Correct |
1 ms |
5468 KB |
Output is correct |
20 |
Correct |
2 ms |
5468 KB |
Output is correct |
21 |
Correct |
1 ms |
5468 KB |
Output is correct |
22 |
Correct |
1 ms |
5468 KB |
Output is correct |
23 |
Correct |
1 ms |
5264 KB |
Output is correct |
24 |
Correct |
1 ms |
5468 KB |
Output is correct |
25 |
Correct |
1 ms |
5468 KB |
Output is correct |
26 |
Correct |
1 ms |
5468 KB |
Output is correct |
27 |
Correct |
2 ms |
5468 KB |
Output is correct |
28 |
Correct |
1 ms |
5468 KB |
Output is correct |
29 |
Correct |
2 ms |
5468 KB |
Output is correct |
30 |
Correct |
2 ms |
5340 KB |
Output is correct |
31 |
Correct |
1 ms |
5468 KB |
Output is correct |
32 |
Correct |
2 ms |
5468 KB |
Output is correct |
33 |
Correct |
17 ms |
6788 KB |
Output is correct |
34 |
Correct |
17 ms |
6600 KB |
Output is correct |
35 |
Correct |
30 ms |
6788 KB |
Output is correct |
36 |
Correct |
23 ms |
7128 KB |
Output is correct |
37 |
Correct |
21 ms |
7124 KB |
Output is correct |
38 |
Correct |
37 ms |
6952 KB |
Output is correct |
39 |
Correct |
28 ms |
7520 KB |
Output is correct |
40 |
Correct |
27 ms |
7512 KB |
Output is correct |
41 |
Correct |
46 ms |
7344 KB |
Output is correct |
42 |
Correct |
32 ms |
7932 KB |
Output is correct |
43 |
Correct |
43 ms |
7904 KB |
Output is correct |
44 |
Correct |
57 ms |
7980 KB |
Output is correct |
45 |
Correct |
42 ms |
8340 KB |
Output is correct |
46 |
Correct |
42 ms |
8404 KB |
Output is correct |
47 |
Correct |
71 ms |
8424 KB |
Output is correct |
48 |
Correct |
49 ms |
8992 KB |
Output is correct |
49 |
Correct |
50 ms |
8836 KB |
Output is correct |
50 |
Correct |
86 ms |
8964 KB |
Output is correct |
51 |
Correct |
57 ms |
9680 KB |
Output is correct |
52 |
Correct |
75 ms |
9656 KB |
Output is correct |
53 |
Correct |
104 ms |
9496 KB |
Output is correct |
54 |
Correct |
67 ms |
10132 KB |
Output is correct |
55 |
Correct |
69 ms |
10156 KB |
Output is correct |
56 |
Correct |
123 ms |
10088 KB |
Output is correct |
57 |
Correct |
78 ms |
10788 KB |
Output is correct |
58 |
Correct |
78 ms |
10720 KB |
Output is correct |
59 |
Correct |
150 ms |
10884 KB |
Output is correct |
60 |
Correct |
116 ms |
11340 KB |
Output is correct |
61 |
Correct |
85 ms |
11452 KB |
Output is correct |
62 |
Correct |
157 ms |
11452 KB |
Output is correct |
63 |
Correct |
158 ms |
11396 KB |
Output is correct |
64 |
Correct |
220 ms |
11652 KB |
Output is correct |
65 |
Correct |
266 ms |
11360 KB |
Output is correct |
66 |
Correct |
192 ms |
11428 KB |
Output is correct |
67 |
Correct |
163 ms |
11408 KB |
Output is correct |
68 |
Correct |
110 ms |
11432 KB |
Output is correct |
69 |
Correct |
141 ms |
11448 KB |
Output is correct |
70 |
Correct |
143 ms |
11704 KB |
Output is correct |
71 |
Correct |
112 ms |
11528 KB |
Output is correct |
72 |
Incorrect |
109 ms |
11416 KB |
Output isn't correct |
73 |
Incorrect |
124 ms |
11556 KB |
Output isn't correct |
74 |
Correct |
118 ms |
11468 KB |
Output is correct |
75 |
Correct |
126 ms |
11540 KB |
Output is correct |
76 |
Correct |
115 ms |
11600 KB |
Output is correct |
77 |
Correct |
118 ms |
11552 KB |
Output is correct |
78 |
Correct |
133 ms |
11508 KB |
Output is correct |
79 |
Correct |
118 ms |
11484 KB |
Output is correct |
80 |
Correct |
124 ms |
11516 KB |
Output is correct |
81 |
Correct |
123 ms |
11456 KB |
Output is correct |
82 |
Correct |
118 ms |
11456 KB |
Output is correct |
83 |
Correct |
130 ms |
11504 KB |
Output is correct |
84 |
Correct |
151 ms |
11440 KB |
Output is correct |
85 |
Correct |
136 ms |
11380 KB |
Output is correct |
86 |
Correct |
133 ms |
11512 KB |
Output is correct |
87 |
Correct |
133 ms |
11436 KB |
Output is correct |
88 |
Correct |
128 ms |
11404 KB |
Output is correct |
89 |
Correct |
137 ms |
11436 KB |
Output is correct |
90 |
Correct |
160 ms |
11440 KB |
Output is correct |
91 |
Correct |
136 ms |
11516 KB |
Output is correct |
92 |
Correct |
133 ms |
11704 KB |
Output is correct |