#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using db = double;
using str = string; // yay python!
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define tcT template<class T
#define tcTU tcT, class U
// ^ lol this makes everything look weird but I'll try it
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
tcT> using PR = pair<T,T>;
// pairs
#define mp make_pair
#define fi first
#define se second
// vectors
// oops size(x), rbegin(x), rend(x) need C++17
#define sz(x) int((x).size())
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define eb emplace_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
tcT> int lwb(V<T>& a, const T& b) { return int(lb(all(a),b)-bg(a)); }
// loops
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
const int MOD = 1e9+7; // 998244353;
const int MX = 2e5+5;
const ll INF = 1e18; // not too close to LLONG_MAX
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!!
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;
tcT> bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0; } // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0; }
#define tcTUU tcT, class ...U
// TO_STRING
#define ts to_string
str ts(char c) { return str(1,c); }
str ts(const char* s) { return (str)s; }
str ts(str s) { return s; }
str ts(bool b) {
#ifdef LOCAL
return b ? "true" : "false";
#else
return ts((int)b);
#endif
}
tcTU> str ts(pair<T,U> p);
tcT> str ts(T v) { // containers with begin(), end()
#ifdef LOCAL
bool fst = 1; str res = "{";
for (const auto& x: v) {
if (!fst) res += ", ";
fst = 0; res += ts(x);
}
res += "}"; return res;
#else
bool fst = 1; str res = "";
for (const auto& x: v) {
if (!fst) res += " ";
fst = 0; res += ts(x);
}
return res;
#endif
}
tcTU> str ts(pair<T,U> p) {
#ifdef LOCAL
return "("+ts(p.fi)+", "+ts(p.se)+")";
#else
return ts(p.fi)+" "+ts(p.se);
#endif
}
// OUTPUT
tcT> void pr(T x) { cout << ts(x); }
tcTUU> void pr(const T& t, const U&... u) {
pr(t); pr(u...); }
void ps() { pr("\n"); } // print w/ spaces
tcTUU> void ps(const T& t, const U&... u) {
pr(t); if (sizeof...(u)) pr(" "); ps(u...); }
// DEBUG
void DBG() { cerr << "]" << endl; }
tcTUU> void DBG(const T& t, const U&... u) {
cerr << ts(t); if (sizeof...(u)) cerr << ", ";
DBG(u...); }
#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert
#define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \
<< __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0);
#else
#define dbg(...) 0
#define chk(...) 0
#endif
void setPrec() { cout << fixed << setprecision(15); }
void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }
// FILE I/O
void setIn(str s) { freopen(s.c_str(),"r",stdin); }
void setOut(str s) { freopen(s.c_str(),"w",stdout); }
void setIO(str s = "") {
unsyncIO(); setPrec();
// cin.exceptions(cin.failbit);
// throws exception when do smth illegal
// ex. try to read letter into int
if (sz(s)) setIn(s+".in"), setOut(s+".out"); // for USACO
}
const int nax = 805;
str f[nax]; // [state #][row][col]
int dst[nax][nax][2]; // [row][col][0 for mecho, 1 for bee]
int n, s;
pi m; // mecho starting pos
void reset() {
F0R(i, nax) {
F0R(j, nax) {
dst[i][j][0] = -1;
}
}
}
bool check(int x, int y, bool mecho) {
if (x < 0 || x >= n || y < 0 || y >= n) return 0;
if (f[y][x] != 'G' && !(mecho && f[y][x] == 'D')) return 0;
return 1;
}
bool tri(int t) {
if (dst[m.se][m.fi][1] <= t) return false;
queue<pi> q;
dst[m.se][m.fi][0] = t;
q.push(m);
while(!q.empty()) {
pi u = q.ft;
// dbg(u, dst[u.se][u.fi][0]);
if (f[u.se][u.fi] == 'D') {
return true;
}
q.pop();
F0R(i, 4) {
pi v = mp(u.fi+dx[i], u.se+dy[i]);
if (check(v.fi, v.se, 1) && dst[v.se][v.fi][0] == -1) {
dst[v.se][v.fi][0] = dst[u.se][u.fi][0]+1;
if (dst[v.se][v.fi][1] > dst[v.se][v.fi][0]) {
q.push(v);
}
}
}
}
return false;
}
void bfs(vpi h) {
queue<pi> q;
trav(x, h) {
q.push(x);
dst[x.se][x.fi][1] = 0;
}
while(!q.empty()) {
pi u = q.ft;
q.pop();
F0R(i, 4) {
pi v = mp(u.fi+dx[i], u.se+dy[i]);
if (check(v.fi, v.se, 0) && dst[v.se][v.fi][1] == -1) {
dst[v.se][v.fi][1] = dst[u.se][u.fi][1]+s;
q.push(v);
}
}
}
}
int main() {
setIO();
F0R(i, nax) F0R(j, nax) F0R(k, 2) dst[i][j][k] = -1;
cin >> n >> s;
F0R(i, n) cin >> f[i];
vpi h;
F0R(i, n) {
F0R(j, n) {
if (f[i][j] == 'H') h.pb(mp(j, i));
if (f[i][j] == 'M') {
m = {j, i};
f[i][j] = 'G';
}
if (f[i][j] == 'D') dst[i][j][1] = (1e9+10);
}
}
// BEE BFS
bfs(h);
// BEAR BINARY SEARCH
int lo = -1, hi = 2*n*n+1;
while(lo < hi) {
dbg(lo, hi);
int mid = (lo+hi+1)/2;
reset();
if (tri(mid*s)) lo = mid;
else hi = mid-1;
}
ps(lo);
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:127:19: warning: statement has no effect [-Wunused-value]
127 | #define dbg(...) 0
| ^
mecho.cpp:236:3: note: in expansion of macro 'dbg'
236 | dbg(lo, hi);
| ^~~
mecho.cpp: In function 'void setIn(str)':
mecho.cpp:134:28: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
134 | void setIn(str s) { freopen(s.c_str(),"r",stdin); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
mecho.cpp: In function 'void setOut(str)':
mecho.cpp:135:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | void setOut(str s) { freopen(s.c_str(),"w",stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5324 KB |
Output is correct |
2 |
Correct |
6 ms |
5324 KB |
Output is correct |
3 |
Correct |
5 ms |
5408 KB |
Output is correct |
4 |
Correct |
5 ms |
5324 KB |
Output is correct |
5 |
Correct |
5 ms |
5352 KB |
Output is correct |
6 |
Correct |
6 ms |
5324 KB |
Output is correct |
7 |
Correct |
95 ms |
6184 KB |
Output is correct |
8 |
Correct |
6 ms |
5324 KB |
Output is correct |
9 |
Correct |
6 ms |
5324 KB |
Output is correct |
10 |
Correct |
6 ms |
5324 KB |
Output is correct |
11 |
Correct |
6 ms |
5324 KB |
Output is correct |
12 |
Correct |
8 ms |
5324 KB |
Output is correct |
13 |
Correct |
7 ms |
5324 KB |
Output is correct |
14 |
Correct |
8 ms |
5324 KB |
Output is correct |
15 |
Correct |
7 ms |
5324 KB |
Output is correct |
16 |
Correct |
7 ms |
5324 KB |
Output is correct |
17 |
Correct |
6 ms |
5324 KB |
Output is correct |
18 |
Correct |
7 ms |
5324 KB |
Output is correct |
19 |
Correct |
7 ms |
5324 KB |
Output is correct |
20 |
Correct |
6 ms |
5324 KB |
Output is correct |
21 |
Correct |
7 ms |
5324 KB |
Output is correct |
22 |
Correct |
7 ms |
5412 KB |
Output is correct |
23 |
Correct |
7 ms |
5324 KB |
Output is correct |
24 |
Correct |
7 ms |
5324 KB |
Output is correct |
25 |
Correct |
7 ms |
5324 KB |
Output is correct |
26 |
Correct |
8 ms |
5324 KB |
Output is correct |
27 |
Correct |
8 ms |
5324 KB |
Output is correct |
28 |
Correct |
8 ms |
5324 KB |
Output is correct |
29 |
Correct |
8 ms |
5420 KB |
Output is correct |
30 |
Correct |
8 ms |
5320 KB |
Output is correct |
31 |
Correct |
8 ms |
5324 KB |
Output is correct |
32 |
Correct |
8 ms |
5424 KB |
Output is correct |
33 |
Correct |
14 ms |
5580 KB |
Output is correct |
34 |
Correct |
11 ms |
5540 KB |
Output is correct |
35 |
Correct |
28 ms |
5544 KB |
Output is correct |
36 |
Correct |
12 ms |
5708 KB |
Output is correct |
37 |
Correct |
12 ms |
5452 KB |
Output is correct |
38 |
Correct |
34 ms |
5580 KB |
Output is correct |
39 |
Correct |
13 ms |
5620 KB |
Output is correct |
40 |
Correct |
13 ms |
5580 KB |
Output is correct |
41 |
Correct |
45 ms |
5580 KB |
Output is correct |
42 |
Correct |
13 ms |
5580 KB |
Output is correct |
43 |
Correct |
13 ms |
5676 KB |
Output is correct |
44 |
Correct |
52 ms |
5656 KB |
Output is correct |
45 |
Correct |
14 ms |
5724 KB |
Output is correct |
46 |
Correct |
14 ms |
5708 KB |
Output is correct |
47 |
Correct |
56 ms |
5708 KB |
Output is correct |
48 |
Correct |
15 ms |
5708 KB |
Output is correct |
49 |
Correct |
18 ms |
5836 KB |
Output is correct |
50 |
Correct |
68 ms |
5708 KB |
Output is correct |
51 |
Correct |
16 ms |
5860 KB |
Output is correct |
52 |
Correct |
16 ms |
5864 KB |
Output is correct |
53 |
Correct |
81 ms |
5836 KB |
Output is correct |
54 |
Correct |
16 ms |
5836 KB |
Output is correct |
55 |
Correct |
17 ms |
5932 KB |
Output is correct |
56 |
Correct |
97 ms |
5836 KB |
Output is correct |
57 |
Correct |
20 ms |
6092 KB |
Output is correct |
58 |
Correct |
18 ms |
6004 KB |
Output is correct |
59 |
Correct |
117 ms |
6004 KB |
Output is correct |
60 |
Correct |
19 ms |
6032 KB |
Output is correct |
61 |
Correct |
19 ms |
6068 KB |
Output is correct |
62 |
Correct |
121 ms |
6064 KB |
Output is correct |
63 |
Correct |
107 ms |
5964 KB |
Output is correct |
64 |
Correct |
205 ms |
6060 KB |
Output is correct |
65 |
Correct |
157 ms |
5964 KB |
Output is correct |
66 |
Correct |
126 ms |
6068 KB |
Output is correct |
67 |
Correct |
119 ms |
6092 KB |
Output is correct |
68 |
Correct |
51 ms |
6092 KB |
Output is correct |
69 |
Correct |
53 ms |
6112 KB |
Output is correct |
70 |
Correct |
41 ms |
6092 KB |
Output is correct |
71 |
Correct |
47 ms |
6092 KB |
Output is correct |
72 |
Correct |
39 ms |
6092 KB |
Output is correct |
73 |
Correct |
38 ms |
6396 KB |
Output is correct |
74 |
Correct |
60 ms |
6404 KB |
Output is correct |
75 |
Correct |
74 ms |
6384 KB |
Output is correct |
76 |
Correct |
69 ms |
6348 KB |
Output is correct |
77 |
Correct |
68 ms |
6348 KB |
Output is correct |
78 |
Correct |
89 ms |
6344 KB |
Output is correct |
79 |
Correct |
51 ms |
6340 KB |
Output is correct |
80 |
Correct |
63 ms |
6336 KB |
Output is correct |
81 |
Correct |
70 ms |
6348 KB |
Output is correct |
82 |
Correct |
64 ms |
6348 KB |
Output is correct |
83 |
Correct |
87 ms |
6328 KB |
Output is correct |
84 |
Correct |
80 ms |
6328 KB |
Output is correct |
85 |
Correct |
87 ms |
6296 KB |
Output is correct |
86 |
Correct |
79 ms |
6320 KB |
Output is correct |
87 |
Correct |
77 ms |
6312 KB |
Output is correct |
88 |
Correct |
82 ms |
6220 KB |
Output is correct |
89 |
Correct |
87 ms |
6240 KB |
Output is correct |
90 |
Correct |
119 ms |
6236 KB |
Output is correct |
91 |
Correct |
94 ms |
6220 KB |
Output is correct |
92 |
Correct |
91 ms |
6220 KB |
Output is correct |