# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
687222 | stevancv | Mecho (IOI09_mecho) | C++14 | 206 ms | 6944 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 800 + 2;
const int inf = 1e9;
string s[N];
int b[N][N], a[N][N], n, k;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
bool Can(int x, int y) {
if (x < 0 || x >= n) return 0;
if (y < 0 || y >= n) return 0;
if (s[x][y] == 'T') return 0;
return 1;
}
bool Ok(int x, int y, int val, int m) {
return (b[x][y] - val / k - m) > 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
int ii, jj, iii, jjj;
ii = jj = iii = jjj = -1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (s[i][j] == 'M') {
ii = i;
jj = j;
}
if (s[i][j] == 'D') {
iii = i;
jjj = j;
}
}
}
queue<pair<int, int>> q;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (s[i][j] == 'H') {
q.push({i, j});
b[i][j] = 0;
}
else b[i][j] = inf;
}
}
while (!q.empty()) {
auto u = q.front(); q.pop();
for (int i = 0; i < 4; i++) {
int nx = u.first + dx[i];
int ny = u.second + dy[i];
if (!Can(nx, ny) || (nx == iii && ny == jjj)) continue;
if (b[nx][ny] > b[u.first][u.second] + 1) {
q.push({nx, ny});
b[nx][ny] = b[u.first][u.second] + 1;
}
}
}
auto Moze = [&] (int mid) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = inf;
}
}
if (mid >= b[iii][jjj]) return false;
q.push({ii, jj});
a[ii][jj] = 0;
while (!q.empty()) {
auto u = q.front(); q.pop();
for (int i = 0; i < 4; i++) {
int nx = u.first + dx[i];
int ny = u.second + dy[i];
if (!Can(nx, ny)) continue;
if (Ok(nx, ny, a[u.first][u.second] + 1, mid) && a[nx][ny] > a[u.first][u.second] + 1) {
q.push({nx, ny});
a[nx][ny] = a[u.first][u.second] + 1;
}
}
}
return a[iii][jjj] != inf;
};
int l = 0, r = b[ii][jj] - 1, ans = -1;
while (l <= r) {
int mid = l + r >> 1;
if (Moze(mid)) {
l = mid + 1;
ans = mid;
}
else r = mid - 1;
}
cout << ans << en;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |