# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
417566 | nkato | Mecho (IOI09_mecho) | C++17 | 202 ms | 7020 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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) {
if (x < 0 || x >= n || y < 0 || y >= n) return 0;
if (f[y][x] == 'T') return 0;
return 1;
}
bool tri(int t) {
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]);
dbg(v);
if (check(v.fi, v.se) && dst[v.se][v.fi][0] == -1) {
dst[v.se][v.fi][0] = dst[u.se][u.fi][0]+1;
dbg(dst[v.se][v.fi][1]*s, dst[v.se][v.fi][0]);
if ((dst[v.se][v.fi][1]*s) >= 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) && dst[v.se][v.fi][1] == -1) {
dst[v.se][v.fi][1] = dst[u.se][u.fi][1]+1;
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};
}
}
// BEE BFS
bfs(h);
// BEAR BINARY SEARCH
int lo = -1, hi = 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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |