#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]) 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, 1e12, 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 |
5468 KB |
Output is correct |
5 |
Correct |
1 ms |
5468 KB |
Output is correct |
6 |
Correct |
1 ms |
5468 KB |
Output is correct |
7 |
Correct |
113 ms |
11420 KB |
Output is correct |
8 |
Correct |
2 ms |
5468 KB |
Output is correct |
9 |
Correct |
2 ms |
5468 KB |
Output is correct |
10 |
Correct |
2 ms |
5468 KB |
Output is correct |
11 |
Correct |
2 ms |
5328 KB |
Output is correct |
12 |
Incorrect |
2 ms |
5468 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
5468 KB |
Output isn't correct |
14 |
Correct |
2 ms |
5468 KB |
Output is correct |
15 |
Correct |
1 ms |
5468 KB |
Output is correct |
16 |
Correct |
1 ms |
5468 KB |
Output is correct |
17 |
Correct |
1 ms |
5468 KB |
Output is correct |
18 |
Correct |
2 ms |
5484 KB |
Output is correct |
19 |
Correct |
2 ms |
5284 KB |
Output is correct |
20 |
Correct |
1 ms |
5340 KB |
Output is correct |
21 |
Correct |
2 ms |
5468 KB |
Output is correct |
22 |
Correct |
1 ms |
5468 KB |
Output is correct |
23 |
Correct |
1 ms |
5468 KB |
Output is correct |
24 |
Correct |
2 ms |
5464 KB |
Output is correct |
25 |
Correct |
1 ms |
5360 KB |
Output is correct |
26 |
Correct |
2 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 |
5336 KB |
Output is correct |
30 |
Correct |
2 ms |
5468 KB |
Output is correct |
31 |
Correct |
2 ms |
5464 KB |
Output is correct |
32 |
Correct |
2 ms |
5468 KB |
Output is correct |
33 |
Correct |
12 ms |
6596 KB |
Output is correct |
34 |
Correct |
14 ms |
6600 KB |
Output is correct |
35 |
Correct |
24 ms |
6612 KB |
Output is correct |
36 |
Correct |
17 ms |
6948 KB |
Output is correct |
37 |
Correct |
16 ms |
6952 KB |
Output is correct |
38 |
Correct |
32 ms |
6960 KB |
Output is correct |
39 |
Correct |
20 ms |
7440 KB |
Output is correct |
40 |
Correct |
21 ms |
7460 KB |
Output is correct |
41 |
Correct |
41 ms |
7440 KB |
Output is correct |
42 |
Correct |
26 ms |
7912 KB |
Output is correct |
43 |
Correct |
30 ms |
7864 KB |
Output is correct |
44 |
Correct |
53 ms |
7872 KB |
Output is correct |
45 |
Correct |
30 ms |
8352 KB |
Output is correct |
46 |
Correct |
29 ms |
8364 KB |
Output is correct |
47 |
Correct |
63 ms |
8312 KB |
Output is correct |
48 |
Correct |
40 ms |
8828 KB |
Output is correct |
49 |
Correct |
36 ms |
8876 KB |
Output is correct |
50 |
Correct |
74 ms |
8940 KB |
Output is correct |
51 |
Correct |
41 ms |
9536 KB |
Output is correct |
52 |
Correct |
41 ms |
9512 KB |
Output is correct |
53 |
Correct |
86 ms |
9432 KB |
Output is correct |
54 |
Correct |
50 ms |
10172 KB |
Output is correct |
55 |
Correct |
52 ms |
10044 KB |
Output is correct |
56 |
Correct |
100 ms |
10108 KB |
Output is correct |
57 |
Correct |
56 ms |
11004 KB |
Output is correct |
58 |
Correct |
61 ms |
10976 KB |
Output is correct |
59 |
Correct |
115 ms |
10788 KB |
Output is correct |
60 |
Correct |
69 ms |
11524 KB |
Output is correct |
61 |
Correct |
64 ms |
11536 KB |
Output is correct |
62 |
Correct |
136 ms |
11404 KB |
Output is correct |
63 |
Correct |
136 ms |
11424 KB |
Output is correct |
64 |
Correct |
184 ms |
11328 KB |
Output is correct |
65 |
Correct |
194 ms |
11348 KB |
Output is correct |
66 |
Correct |
142 ms |
11464 KB |
Output is correct |
67 |
Correct |
133 ms |
11380 KB |
Output is correct |
68 |
Correct |
88 ms |
11432 KB |
Output is correct |
69 |
Correct |
104 ms |
11380 KB |
Output is correct |
70 |
Correct |
76 ms |
11500 KB |
Output is correct |
71 |
Correct |
78 ms |
11424 KB |
Output is correct |
72 |
Incorrect |
95 ms |
11408 KB |
Output isn't correct |
73 |
Incorrect |
96 ms |
11496 KB |
Output isn't correct |
74 |
Correct |
93 ms |
11656 KB |
Output is correct |
75 |
Correct |
109 ms |
11484 KB |
Output is correct |
76 |
Correct |
90 ms |
11492 KB |
Output is correct |
77 |
Correct |
93 ms |
11476 KB |
Output is correct |
78 |
Correct |
107 ms |
11468 KB |
Output is correct |
79 |
Correct |
98 ms |
11452 KB |
Output is correct |
80 |
Correct |
92 ms |
11424 KB |
Output is correct |
81 |
Correct |
98 ms |
11508 KB |
Output is correct |
82 |
Correct |
95 ms |
11412 KB |
Output is correct |
83 |
Correct |
107 ms |
11436 KB |
Output is correct |
84 |
Correct |
99 ms |
11372 KB |
Output is correct |
85 |
Correct |
98 ms |
11488 KB |
Output is correct |
86 |
Correct |
105 ms |
11544 KB |
Output is correct |
87 |
Correct |
102 ms |
11484 KB |
Output is correct |
88 |
Correct |
109 ms |
11488 KB |
Output is correct |
89 |
Correct |
108 ms |
11512 KB |
Output is correct |
90 |
Correct |
116 ms |
11568 KB |
Output is correct |
91 |
Correct |
101 ms |
11476 KB |
Output is correct |
92 |
Correct |
126 ms |
11468 KB |
Output is correct |