# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
393076 | ngpin04 | Mecho (IOI09_mecho) | C++14 | 685 ms | 6384 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int N = 805;
const int oo = 1e9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
char a[N][N];
pair <int, int> s,t;
int bee[N][N];
int dis[N][N];
int n,d;
bool ok(int i, int j) {
return a[i][j] != 'T' && min(i, j) > 0 && max(i, j) <= n;
}
bool solve(int x, queue <pair <int, int>> q) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if (a[i][j] != 'H')
bee[i][j] = oo;
if (a[i][j] != 'M')
dis[i][j] = oo;
}
int fir = -1;
while (q.size()) {
int i = q.front().fi;
int j = q.front().se;
if (fir < 0)
fir = bee[i][j];
if (bee[i][j] - fir == x)
break;
q.pop();
for (int dir = 0; dir < 4; dir++) {
int u = i + dx[dir];
int v = j + dy[dir];
if (ok(u, v) && bee[u][v] == oo) {
bee[u][v] = bee[i][j] + 1;
q.push(mp(u, v));
}
}
}
if (bee[s.fi][s.se] < oo)
return false;
queue <pair <int, int>> qq;
qq.push(s);
while (qq.size()) {
fir = -1;
while (qq.size()) {
int i = qq.front().fi;
int j = qq.front().se;
if (fir < 0)
fir = dis[i][j];
if (dis[i][j] - fir == d)
break;
qq.pop();
if (mp(i, j) == t)
return true;
if (bee[i][j] < oo)
continue;
for (int dir = 0; dir < 4; dir++) {
int u = i + dx[dir];
int v = j + dy[dir];
if (ok(u, v) && bee[u][v] == oo && dis[u][v] == oo) {
dis[u][v] = dis[i][j] + 1;
qq.push(mp(u, v));
}
}
}
fir = -1;
while (q.size()) {
int i = q.front().fi;
int j = q.front().se;
if (fir < 0)
fir = bee[i][j];
if (bee[i][j] - fir == 1)
break;
q.pop();
for (int dir = 0; dir < 4; dir++) {
int u = i + dx[dir];
int v = j + dy[dir];
if (ok(u, v) && bee[u][v] == oo) {
bee[u][v] = bee[i][j] + 1;
q.push(mp(u, v));
}
}
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> d;
queue <pair <int, int>> q;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
if (a[i][j] == 'H')
q.push(mp(i, j));
if (a[i][j] == 'M')
s = mp(i, j);
if (a[i][j] == 'D')
t = mp(i, j);
}
int lo = -1;
int hi = 1e9;
while (hi - lo > 1) {
int mid = (lo + hi) >> 1;
if (solve(mid, q))
lo = mid;
else
hi = mid;
}
cout << -1;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |