# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
529632 | thezomb1e | Mecho (IOI09_mecho) | C++17 | 385 ms | 6432 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//thatsramen
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define eb emplace_back
#define pb push_back
#define ft first
#define sd second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define dbg(...) dbg_out(__VA_ARGS__)
using ll = long long;
using ld = long double;
using namespace std;
using namespace __gnu_pbds;
//Constants
const ll INF = 5 * 1e18;
const int IINF = 2 * 1e9;
// const ll MOD = 1e9 + 7;
const ll MOD = 998244353;
const ll dx[8] = {1, 0, -1, 0, -1, -1, 1, 1}, dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
const ld PI = 3.14159265359;
//Templates
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) {return os << '(' << p.first << ", " << p.second << ')';}
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) {os << '['; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << ']';}
void dbg_out() {cerr << endl;}
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << H << ' '; dbg_out(T...); }
template<typename T> void mins(T& x, T y) {x = min(x, y);}
template<typename T> void maxs(T& x, T y) {x = max(x, y);}
template<typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using omset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
//order_of_key(k): number of elements strictly less than k
//find_by_order(k): k-th element in the set
void setPrec() {cout << fixed << setprecision(15);}
void unsyncIO() {cin.tie(0)->sync_with_stdio(0);}
void setIn(string s) {freopen(s.c_str(), "r", stdin);}
void setOut(string s) {freopen(s.c_str(), "w", stdout);}
void setIO(string s = "") {
unsyncIO(); setPrec();
if(s.size()) setIn(s + ".in"), setOut(s + ".out");
}
//#define TEST_CASES
void solve() {
int n, s;
cin >> n >> s;
vector<vector<char>> grid(n, vector<char> (n));
pair<int, int> start, home;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> grid[i][j];
if (grid[i][j] == 'D') {
home = {i, j};
}
if (grid[i][j] == 'M') {
start = {i, j};
}
}
}
queue<pair<int, int>> que;
vector<vector<int>> d1(n, vector<int> (n)), d2(n, vector<int> (n));
auto ok = [&](int x, int y) {
return x >= 0 && y >= 0 && x < n && y < n && (grid[x][y] == 'G' || grid[x][y] == 'M');
};
int lo = 0, hi = 2000, ans = -1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
d1[i][j] = IINF;
d2[i][j] = IINF;
if (grid[i][j] == 'H') {
que.push({i, j});
d2[i][j] = 0;
}
}
}
while (!que.empty()) {
auto cur = que.front();
que.pop();
int x = cur.ft, y = cur.sd;
for (int i = 0; i < 4; i++) {
int tox = x + dx[i], toy = y + dy[i];
if (ok(tox, toy) && d2[tox][toy] > d2[x][y] + 1) {
d2[tox][toy] = d2[x][y] + 1;
que.push({tox, toy});
}
}
}
if (d2[start.ft][start.sd] > mid) {
d1[start.ft][start.sd] = 0;
que.push(start);
}
while (!que.empty()) {
auto cur = que.front();
que.pop();
int x = cur.ft, y = cur.sd;
for (int i = 0; i < 4; i++) {
int tox = x + dx[i], toy = y + dy[i];
if (ok(tox, toy) && d1[tox][toy] > d1[x][y] + 1) {
if ((mid + (d1[x][y] + 1) / s) < d2[tox][toy]) {
d1[tox][toy] = d1[x][y] + 1;
que.push({tox, toy});
}
}
}
}
bool can = false;
for (int i = 0; i < 4; i++) {
int tox = home.ft + dx[i], toy = home.sd + dy[i];
if (ok(tox, toy) && d1[tox][toy] != IINF && (mid + d1[tox][toy] / s) < d2[tox][toy]) {
can = true;
}
}
if (can) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
cout << ans << '\n';
}
int main() {
setIO();
int tt = 1;
#ifdef TEST_CASES
cin >> tt;
#endif
while (tt--)
solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |