# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
941728 | RavenHead | Mecho (IOI09_mecho) | C++17 | 199 ms | 11656 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define EB emplace_back
#define all(x) std::begin(x), std::end(x)
#define sz(x) (int)(x).size()
#define rep(i, a, b) for(long long i = a; i < (b); ++i)
#define endl '\n'
#define debarr(a, n) cerr << #a << " : ";for(int i = 0; i < n; i++) cerr << a[i] << " "; cerr << endl;
#define debmat(mat, row, col) cerr << #mat << " :\n";for(int i = 0; i < row; i++) {for(int j = 0; j < col; j++) cerr << mat[i][j] << " ";cerr << endl;}
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class S, class T>ostream& operator <<(ostream& os, const pair<S, T>& p) {return os << "(" << p.first << ", " << p.second << ")";}
template <class T>ostream& operator <<(ostream& os, const vector<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const unordered_set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const unordered_map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const multiset<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T> void dbs(string str, T t) {cerr << str << " : " << t << "\n";}
template <class T, class... S> void dbs(string str, T t, S... s) {int idx = str.find(','); cerr << str.substr(0, idx) << " : " << t << ","; dbs(str.substr(idx + 1), s...);}
template <class T> void prc(T a, T b) {cerr << "["; for (T i = a; i != b; ++i) {if (i != a) cerr << ", "; cerr << *i;} cerr << "]\n";}
typedef long long lli;
typedef pair<lli, lli> ii; typedef vector<lli> vi;
typedef vector<ii> vii; typedef vector<vi> graph;
bool ckmax(auto &a, auto const& b) {return b > a ? a = b, 1 : 0;}
bool ckmin(auto &a, auto const& b) {return b < a ? a = b, 1 : 0;}
int const MOD = 1000000007;
void pre() {
}
void solve() {
lli n, s;
cin >> n >> s;
vector<string> arr(n);
rep(i, 0, n) {
cin >> arr[i];
}
ii mecho, home;
rep(i, 0, n)rep(j, 0, n) {
if (arr[i][j] == 'M') {
mecho = {i, j};
}
else if (arr[i][j] == 'D') {
home = {i, j};
}
}
// checks function
auto checks = [&](lli xx, lli yy)->bool{
if (xx >= 0 && xx < n && yy >= 0 && yy < n && (arr[xx][yy] == 'G' || arr[xx][yy] == 'D'))return true;
return false;
};
int const dx[4] = { -1, 1, 0, 0};
int const dy[4] = {0, 0, -1, 1};
graph dis(n, vi(n, 1e9));
queue<ii> q;
rep(i, 0, n)rep(j, 0, n)if (arr[i][j] == 'H') {
q.push({i, j});
dis[i][j] = 0;
}
while (!q.empty()) {
auto nn = q.front(); q.pop();
rep(dir, 0, 4) {
lli xx = nn.F + dx[dir];
lli yy = nn.S + dy[dir];
if (checks(xx, yy)) {
if (dis[xx][yy] > dis[nn.F][nn.S] + 1) {
dis[xx][yy] = dis[nn.F][nn.S] + 1;
q.push({xx, yy});
}
}
}
}
// check function
auto check = [&](lli mid)->bool{
graph d(n, vi(n, 1e9));
d[mecho.F][mecho.S] = 0;
q.push(mecho);
while (!q.empty()) {
auto nn = q.front(); q.pop();
if (dis[nn.F][nn.S] < mid + (d[nn.F][nn.S] + s - 1) / s) {
continue;
}
rep(dir, 0, 4) {
lli xx = nn.F + dx[dir];
lli yy = nn.S + dy[dir];
if (xx >= 0 && xx < n && yy >= 0 && yy < n && (arr[xx][yy] == 'G' || arr[xx][yy] == 'D')) {
if (d[xx][yy] > d[nn.F][nn.S] + 1) {
d[xx][yy] = d[nn.F][nn.S] + 1;
q.push({xx, yy});
}
}
}
}
return mid + (d[home.F][home.S] + s - 1) / s < dis[home.F][home.S];
};
lli low = 0;
lli high = 1e9;
lli ans = -1;
while (low <= high) {
lli mid = (high + low) / 2;
if (check(mid)) {
ans = mid;
low = mid + 1;
} else high = mid - 1;
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
pre();
int T = 1;
while (T--)
solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |