#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, 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 |
3 ms |
5464 KB |
Output is correct |
2 |
Correct |
4 ms |
5468 KB |
Output is correct |
3 |
Correct |
2 ms |
5468 KB |
Output is correct |
4 |
Correct |
2 ms |
5468 KB |
Output is correct |
5 |
Correct |
3 ms |
5304 KB |
Output is correct |
6 |
Correct |
2 ms |
5468 KB |
Output is correct |
7 |
Correct |
160 ms |
10796 KB |
Output is correct |
8 |
Correct |
2 ms |
5468 KB |
Output is correct |
9 |
Correct |
2 ms |
5432 KB |
Output is correct |
10 |
Correct |
3 ms |
5464 KB |
Output is correct |
11 |
Correct |
2 ms |
5468 KB |
Output is correct |
12 |
Incorrect |
3 ms |
5468 KB |
Output isn't correct |
13 |
Incorrect |
3 ms |
5468 KB |
Output isn't correct |
14 |
Correct |
4 ms |
5468 KB |
Output is correct |
15 |
Correct |
2 ms |
5468 KB |
Output is correct |
16 |
Correct |
2 ms |
5464 KB |
Output is correct |
17 |
Correct |
2 ms |
5468 KB |
Output is correct |
18 |
Correct |
2 ms |
5468 KB |
Output is correct |
19 |
Correct |
2 ms |
5464 KB |
Output is correct |
20 |
Correct |
2 ms |
5468 KB |
Output is correct |
21 |
Correct |
3 ms |
5468 KB |
Output is correct |
22 |
Correct |
2 ms |
5468 KB |
Output is correct |
23 |
Correct |
2 ms |
5468 KB |
Output is correct |
24 |
Correct |
2 ms |
5468 KB |
Output is correct |
25 |
Correct |
3 ms |
5468 KB |
Output is correct |
26 |
Correct |
2 ms |
5468 KB |
Output is correct |
27 |
Correct |
2 ms |
5468 KB |
Output is correct |
28 |
Correct |
4 ms |
5464 KB |
Output is correct |
29 |
Correct |
2 ms |
5464 KB |
Output is correct |
30 |
Correct |
2 ms |
5468 KB |
Output is correct |
31 |
Correct |
2 ms |
5468 KB |
Output is correct |
32 |
Correct |
3 ms |
5468 KB |
Output is correct |
33 |
Correct |
17 ms |
6488 KB |
Output is correct |
34 |
Correct |
21 ms |
6484 KB |
Output is correct |
35 |
Correct |
28 ms |
6684 KB |
Output is correct |
36 |
Correct |
22 ms |
6972 KB |
Output is correct |
37 |
Correct |
24 ms |
6972 KB |
Output is correct |
38 |
Correct |
38 ms |
6968 KB |
Output is correct |
39 |
Correct |
39 ms |
7132 KB |
Output is correct |
40 |
Correct |
28 ms |
7320 KB |
Output is correct |
41 |
Correct |
49 ms |
7324 KB |
Output is correct |
42 |
Correct |
35 ms |
7692 KB |
Output is correct |
43 |
Correct |
34 ms |
7688 KB |
Output is correct |
44 |
Correct |
73 ms |
7688 KB |
Output is correct |
45 |
Correct |
47 ms |
8076 KB |
Output is correct |
46 |
Correct |
44 ms |
8112 KB |
Output is correct |
47 |
Correct |
73 ms |
8084 KB |
Output is correct |
48 |
Correct |
51 ms |
8532 KB |
Output is correct |
49 |
Correct |
51 ms |
8512 KB |
Output is correct |
50 |
Correct |
98 ms |
8664 KB |
Output is correct |
51 |
Correct |
58 ms |
9076 KB |
Output is correct |
52 |
Correct |
61 ms |
8936 KB |
Output is correct |
53 |
Correct |
102 ms |
9156 KB |
Output is correct |
54 |
Correct |
102 ms |
9536 KB |
Output is correct |
55 |
Correct |
71 ms |
9624 KB |
Output is correct |
56 |
Correct |
121 ms |
9612 KB |
Output is correct |
57 |
Correct |
78 ms |
10172 KB |
Output is correct |
58 |
Correct |
80 ms |
10356 KB |
Output is correct |
59 |
Correct |
144 ms |
10232 KB |
Output is correct |
60 |
Correct |
92 ms |
10824 KB |
Output is correct |
61 |
Correct |
125 ms |
10780 KB |
Output is correct |
62 |
Correct |
183 ms |
10904 KB |
Output is correct |
63 |
Correct |
156 ms |
10752 KB |
Output is correct |
64 |
Correct |
201 ms |
10840 KB |
Output is correct |
65 |
Correct |
211 ms |
10936 KB |
Output is correct |
66 |
Correct |
177 ms |
10728 KB |
Output is correct |
67 |
Correct |
174 ms |
10740 KB |
Output is correct |
68 |
Correct |
142 ms |
10724 KB |
Output is correct |
69 |
Correct |
105 ms |
10980 KB |
Output is correct |
70 |
Correct |
108 ms |
10800 KB |
Output is correct |
71 |
Correct |
105 ms |
10772 KB |
Output is correct |
72 |
Incorrect |
125 ms |
10760 KB |
Output isn't correct |
73 |
Incorrect |
126 ms |
11084 KB |
Output isn't correct |
74 |
Correct |
133 ms |
10812 KB |
Output is correct |
75 |
Correct |
122 ms |
10948 KB |
Output is correct |
76 |
Correct |
114 ms |
10808 KB |
Output is correct |
77 |
Correct |
123 ms |
10908 KB |
Output is correct |
78 |
Correct |
130 ms |
10972 KB |
Output is correct |
79 |
Correct |
134 ms |
10892 KB |
Output is correct |
80 |
Correct |
123 ms |
10764 KB |
Output is correct |
81 |
Correct |
123 ms |
10816 KB |
Output is correct |
82 |
Correct |
137 ms |
10912 KB |
Output is correct |
83 |
Correct |
136 ms |
10960 KB |
Output is correct |
84 |
Correct |
134 ms |
10860 KB |
Output is correct |
85 |
Correct |
129 ms |
10820 KB |
Output is correct |
86 |
Correct |
151 ms |
10804 KB |
Output is correct |
87 |
Correct |
155 ms |
10752 KB |
Output is correct |
88 |
Correct |
136 ms |
10844 KB |
Output is correct |
89 |
Correct |
149 ms |
10824 KB |
Output is correct |
90 |
Correct |
150 ms |
10940 KB |
Output is correct |
91 |
Correct |
148 ms |
10772 KB |
Output is correct |
92 |
Correct |
132 ms |
10796 KB |
Output is correct |