# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
245580 | tatyam | Mecho (IOI09_mecho) | C++17 | 218 ms | 6908 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int INF = 0x3fffffff;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
#define name2(a,b,c,...) c
#define rep1(b) for(int i = 0; i < b; i++)
#define rep2(i,b) for(int i = 0; i < b; i++)
#define rep(...) name2(__VA_ARGS__,rep2,rep1)(__VA_ARGS__)
#define each(i,a) for(auto&& i : a)
template<class T, class U> bool chmin(T& a, const U& b){ if(a > b){ a = b; return 1; } return 0; }
template<class T, class U> bool chmax(T& a, const U& b){ if(a < b){ a = b; return 1; } return 0; }
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, s;
cin >> n >> s;
vector<string> a(n);
each(i, a) cin >> i;
vector<vector<int>> bee(n, vector<int>(n, INF));
queue<pii> q;
rep(n) rep(j, n) if(a[i][j] == 'H'){
bee[i][j] = 0;
q.emplace(i, j);
}
while(q.size()){
auto [x, y] = q.front();
q.pop();
rep(4){
int x2 = x + dx[i], y2 = y + dy[i];
if(x2 < 0 || y2 < 0 || x2 >= n || y2 >= n || a[x2][y2] != 'G' && a[x2][y2] != 'M') continue;
if(chmin(bee[x2][y2], bee[x][y] + 1)) q.emplace(x2, y2);
}
}
pii start, goal;
rep(n) rep(j, n) if(a[i][j] == 'M') start = {i, j};
rep(n) rep(j, n) if(a[i][j] == 'D') goal = {i, j};
auto check = [&](int v) -> bool {
if(bee[start.first][start.second] <= v) return false;
vector<vector<int>> m(n, vector<int>(n, INF));
m[start.first][start.second] = 0;
q.push(start);
while(q.size()){
auto [x, y] = q.front();
q.pop();
int next = m[x][y] + 1;
rep(4){
int x2 = x + dx[i], y2 = y + dy[i];
if(x2 < 0 || y2 < 0 || x2 >= n || y2 >= n || a[x2][y2] == 'T') continue;
if(next / s + v < bee[x2][y2] && chmin(m[x2][y2], next)) q.emplace(x2, y2);
}
}
return m[goal.first][goal.second] != INF;
};
int ok = -1, ng = 0x1fffff;
while(ng - ok > 1){
int cen = (ok + ng) / 2;
if(check(cen)) ok = cen;
else ng = cen;
}
cout << ok << endl;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |