# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
759138 | Blagoj | Mecho (IOI09_mecho) | C++17 | 143 ms | 4444 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define all(x) x.begin(), x.end()
int n, s, times[802][802];
vector<string> v(802);
pair<int, int> st, en;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
inline bool valid(int x, int y) {
return (x >= 0 && x < n && y >= 0 && y < n && v[x][y] != 'T');
}
bool check(int m) {
if (m >= times[st.first][st.second]) return 0;
queue<pair<int, int>> q;
q.push(st);
bool vis[n + 2][n + 2];
memset(vis, 0, sizeof(vis));
vis[st.first][st.second] = 1;
int moves = 0;
do {
moves++;
if (moves >= s) {
moves = 0;
m++;
}
int sz = q.size();
while (sz--) {
int x = q.front().first, y = q.front().second;
q.pop();
for (int d = 0; d < 4; d++) {
int nx = x + dx[d], ny = y + dy[d];
if (valid(nx, ny) && m < times[nx][ny] && !vis[nx][ny]) {
vis[nx][ny] = 1;
q.push({nx, ny});
if (make_pair(nx, ny) == en) return 1;
}
}
}
}
while (q.size() > 0);
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> s;
queue<pair<int, int>> h;
for (int i = 0; i < n; i++) {
cin >> v[i];
for (int j = 0; j < n; j++) {
times[i][j] = n * n + 1000;
if (v[i][j] == 'M') st = {i, j};
if (v[i][j] == 'H') {
h.push({i, j});
times[i][j] = 0;
}
if (v[i][j] == 'D') en = {i, j};
}
}
while (h.size() > 0) {
int x = h.front().first, y = h.front().second;
h.pop();
for (int d = 0; d < 4; d++) {
int nx = x + dx[d], ny = y + dy[d];
if (valid(nx, ny) && times[x][y] + 1 < times[nx][ny]) {
times[nx][ny] = times[x][y] + 1;
h.push({nx, ny});
}
}
}
int l = 0, r = n * n + 100, ans;
while (l <= r) {
int m = (l + r) / 2;
if (check(m)) {
ans = m;
l = m + 1;
}
else r = m - 1;
}
cout << ans;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |