# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
104094 | Erkhemkhuu | Mecho (IOI09_mecho) | C++17 | 302 ms | 6492 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 ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
int movex[4] = {-1, 0, 1, 0};
int movey[4] = {0, 1, 0, -1};
char maps[805][805];
int beeStep[805][805], meStep[805][805], n, Mx, My, s, Dx, Dy;
queue <pair <int, int> > bee, me;
inline bool check(int x, int y) {
if(x >= 0 && x < n && y >= 0 && y < n) return true;
return false;
}
inline bool can(int mid) {
if(mid >= beeStep[Mx][My]) return false;
int curx, cury, i, j, i1, movexx, moveyy;
me.push(mp(Mx, My));
memset(meStep, -1, sizeof(meStep));
meStep[Mx][My] = 0;
while(!me.empty()) {
curx = me.front().F;
cury = me.front().S;
me.pop();
for(i = 0; i < 4; i++) {
movexx = movex[i] + curx;
moveyy = movey[i] + cury;
if(check(movexx, moveyy) && maps[movexx][moveyy] == 'D') {
while(!me.empty())
me.pop();
return true;
}
if(check(movexx, moveyy) && (maps[movexx][moveyy] == 'G' || maps[movexx][moveyy] == 'D') && meStep[curx][cury] + 1 < s * (beeStep[movexx][moveyy] - mid) && meStep[movexx][moveyy] == -1) {
meStep[movexx][moveyy] = meStep[curx][cury] + 1;
me.push(mp(movexx, moveyy));
}
}
}
return false;
}
int main() {
cin.tie(NULL);
cout.tie(NULL);
ios_base::sync_with_stdio(NULL);
int i, j, l, r, curx, cury, curstep, movexx, moveyy, mid;
cin >> n >> s;
memset(beeStep, -1, sizeof(beeStep));
for(i = 0; i < n; i++) {
cin >> maps[i];
for(j = 0; j < n; j++) {
if(maps[i][j] == 'H') {
bee.push(mp(i, j));
beeStep[i][j] = 0;
}
if(maps[i][j] == 'M') {
Mx = i; My = j;
}
if(maps[i][j] == 'D') {
Dx = i; Dy = j;
}
}
}
while(!bee.empty()) {
curx = bee.front().F;
cury = bee.front().S;
bee.pop();
for(i = 0; i < 4; i++) {
movexx = movex[i] + curx;
moveyy = movey[i] + cury;
if((check(movexx, moveyy) && maps[movexx][moveyy] == 'G' || maps[movexx][moveyy] == 'M') && beeStep[movexx][moveyy] == -1) {
beeStep[movexx][moveyy] = beeStep[curx][cury] + 1;
bee.push(mp(movexx, moveyy));
}
}
}
if(!can(0)) {
cout << "-1\n";
return 0;
}
l = 0; r = n * n;
while(l != r) {
mid = (l + r + 1) / 2;
if(can(mid)) l = mid;
else r = mid - 1;
}
cout << l << "\n";
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |