# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
652753 | chanhchuong123 | Mecho (IOI09_mecho) | C++14 | 1093 ms | 65536 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define task "C"
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
const int dx[] = {-1, 0, 0, +1};
const int dy[] = {0, -1, +1, 0};
const int N = 808;
int n, s;
char a[N][N];
pair<int, int> st, en;
pair<int, int> d[N][N];
struct node {
int du, u, v, cnt;
bool operator< (const node &other) const {
return du > other.du;
}
};
bool check(int tim) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
d[i][j] = make_pair(1e9, -1);
priority_queue<node> q;
d[st.fi][st.se] = {tim, s};
q.push({tim, st.fi, st.se, s});
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (a[i][j] == 'H') {
d[i][j] = {0, 0};
q.push({0, i, j, 0});
}
}
}
while (q.size()) {
int u = q.top().u;
int v = q.top().v;
int du = q.top().du;
int cnt = q.top().cnt;
q.pop();
if (d[u][v] != make_pair(du, cnt)) continue;
for (int i = 0; i < 4; i++) {
int x = u + dx[i], y = v + dy[i];
if (u < 1 || u > n) continue;
if (v < 1 || v > n) continue;
if (a[x][y] == 'T') continue;
int w = (cnt == 0 || cnt == s ? 1 : 0);
int _c = (cnt == 0 ? 0 : cnt == s ? 1 : cnt + 1);
if (d[x][y].fi > d[u][v].fi + w) {
d[x][y].fi = d[u][v].fi + w; d[x][y].se = _c;
q.push({d[x][y].fi, x, y, d[x][y].se});
} else if (d[x][y].fi == d[u][v].fi + w) {
if (d[x][y].se == 0) {
if (cnt == s - 1) continue;
d[x][y].fi = d[u][v].fi + w; d[x][y].se = _c;
q.push({d[x][y].fi, x, y, d[x][y].se});
} else if (d[x][y].se == s && cnt == 0) {
d[x][y].fi = d[u][v].fi + w; d[x][y].se = 0;
q.push({d[x][y].fi, x, y, d[x][y].se});
} else if (cnt != 0) {
if (d[x][y].se > _c) {
d[x][y].se = _c;
q.push({d[x][y].fi, x, y, d[x][y].se});
}
}
}
}
}
return d[en.fi][en.se].se > 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp","r")) {
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n >> s;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
if (a[i][j] == 'M')
st = make_pair(i, j);
if (a[i][j] == 'D')
en = make_pair(i, j);
}
}
if (check(0) == false) {
cout << "-1"; return 0;
}
int l = 0, r = n * 2 + 100;
while (r - l > 1) {
int mid = l + r >> 1;
if (check(mid)) l = mid;
else r = mid;
}
cout << l;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |