# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
145922 | popovicirobert | Mecho (IOI09_mecho) | C++14 | 291 ms | 7000 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#if 0
const int MOD = ;
inline int lgput(int a, int b) {
int ans = 1;
while(b > 0) {
if(b & 1) ans = (1LL * ans * a) % MOD;
b >>= 1;
a = (1LL * a * a) % MOD;
}
return ans;
}
inline void mod(int &x) {
if(x >= MOD)
x -= MOD;
}
inline void add(int &x, int y) {
x += y;
mod(x);
}
inline void sub(int &x, int y) {
x += MOD - y;
mod(x);
}
inline void mul(int &x, int y) {
x = (1LL * x * y) % MOD;
}
inline int inv(int x) {
return lgput(x, MOD - 2);
}
#endif
#if 0
int fact[], invfact[];
inline void prec(int n) {
fact[0] = 1;
for(int i = 1; i <= n; i++) {
fact[i] = (1LL * fact[i - 1] * i) % MOD;
}
invfact[n] = lgput(fact[n], MOD - 2);
for(int i = n - 1; i >= 0; i--) {
invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD;
}
}
inline int comb(int n, int k) {
if(n < k) return 0;
return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD;
}
#endif
using namespace std;
const int INF = 1e9;
const int MAXN = 800;
char mat[MAXN + 1][MAXN + 1];
int dl[] = {-1, 0, 1, 0}, dc[] = {0, -1, 0, 1}, n;
bool in(int l, int c) {
return l > 0 && c > 0 && l <= n && c <= n;
}
int main() {
#if 0
ifstream cin("A.in");
ofstream cout("A.out");
#endif
int i, j, s;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> s;
int lm, cm, ld, cd;
for(i = 1; i <= n; i++) {
cin >> mat[i] + 1;
for(j = 1; j <= n; j++) {
if(mat[i][j] == 'M') {
lm = i, cm = j;
}
if(mat[i][j] == 'D') {
ld = i, cd = j;
}
}
}
auto bfs = [&](vector < vector <int> > &dst, char ch) -> void {
queue < pair <int, int> > Q;
int i, j;
for(i = 1; i <= n; i++) {
for(j = 1; j <= n; j++) {
dst[i][j] = INF;
if(mat[i][j] == ch) {
Q.push({i, j});
dst[i][j] = 0;
}
}
}
while(Q.size()) {
auto cur = Q.front();
Q.pop();
for(int i = 0; i < 4; i++) {
int l = cur.first + dl[i], c = cur.second + dc[i];
if(in(l, c) && dst[l][c] == INF && (mat[l][c] == 'G' || mat[l][c] == 'M')) {
dst[l][c] = dst[cur.first][cur.second] + 1;
Q.push({l, c});
}
}
}
};
vector < vector <int> > H(n + 1, vector <int>(n + 1));
vector < vector <int> > M(n + 1, vector <int>(n + 1));
bfs(H, 'H');
auto check = [&](int lim) -> bool {
int i, j;
for(i = 1; i <= n; i++) {
for(j = 1; j <= n; j++) {
M[i][j] = INF;
}
}
if(lim >= H[lm][cm]) {
return 0;
}
queue < pair <int, int> > Q; Q.push({lm, cm});
M[lm][cm] = 0;
while(Q.size()) {
auto cur = Q.front();
Q.pop();
for(int i = 0; i < 4; i++) {
int l = cur.first + dl[i], c = cur.second + dc[i];
int d = (M[cur.first][cur.second] + 1) / s + lim;
if(in(l, c) && M[l][c] == INF && d < H[l][c] && (mat[l][c] == 'G' || mat[l][c] == 'D')) {
Q.push({l, c});
M[l][c] = M[cur.first][cur.second] + 1;
}
}
}
return M[ld][cd] != INF;
};
int res = 0;
for(int step = 1 << 19; step; step >>= 1) {
if(check(res + step)) {
res += step;
}
}
cout << (check(res) ? res : -1);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |