Submission #629687

# Submission time Handle Problem Language Result Execution time Memory
629687 2022-08-14T22:10:59 Z 54skyxenon Mecho (IOI09_mecho) C++17
89 / 100
443 ms 11756 KB
// https://oj.uz/problem/view/IOI09_mecho
// From https://usaco.guide/problems/ioi-2009mecho/solution

#include <bits/stdc++.h>
using namespace std;

#define pii pair<int, int>
#define MAX_N 800
#define int long long

vector<string> field(MAX_N);
int n, s;
int mechox, mechoy, home_x, home_y;

vector<vector<bool>> bees_visited(MAX_N, vector<bool>(MAX_N, false));
vector<vector<int>> bees_time(MAX_N, vector<int>(MAX_N, 0));

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

bool valid_sq(int x, int y) {
	return x >= 0 && x < n && y >= 0 && y < n && (field[x][y] == 'G' || field[x][y] == 'M');
}

bool mecho_reached(int mecho_dis, int bees_dis){
	return mecho_dis / s < bees_dis;
}

bool ok(int eating_time) {
    vector<vector<bool>> mecho_visited(n, vector<bool>(n, false));
	vector<vector<int>> mecho_time(n, vector<int>(n, 0));
    
    queue<pii> q({make_pair(mechox, mechoy)});

	// move Mecho
	q.push({mechox, mechoy});
	mecho_visited[mechox][mechoy] = true;
	if (bees_time[mechox][mechoy] <= eating_time) {
		q.pop();
	}

	while (!q.empty()) {
		int x = q.front().first, y = q.front().second;
		q.pop();
		for (int i = 0; i < 4; i++) {
			int nx = x + dx[i], ny = y + dy[i];
			/* 
			 * check if mecho reaces node[x][y] before the bees
			 * divide the time mecho takes to reach a node by s, since 
			 * mecho walks s steps at a time.
			 * substract the eating time from the time taken for the 
			 * bees to reach the node, because that time was used by 
			 * mecho for eating
			*/
			if (valid_sq(nx, ny) && !mecho_visited[nx][ny] && mecho_reached(mecho_time[x][y] + 1, bees_time[nx][ny] - eating_time)) {
				mecho_visited[nx][ny] = true;
				q.push({nx, ny});
				mecho_time[nx][ny] = mecho_time[x][y] + 1;
			}
		}
	}
	
	// check if mecho reached a node surrounding his cave before the bees
	for (int i = 0; i < 4; i++) {
		int nx = home_x + dx[i], ny = home_y + dy[i];
		if (valid_sq(nx, ny) && mecho_reached(mecho_time[nx][ny], bees_time[nx][ny] - eating_time) && mecho_visited[nx][ny]) {
			return true;
		}
	}

    return false;
}

int bs(int lo, int hi) {
    if (lo > hi) {
        return -1;
    }

    int mid = (lo + hi) / 2;

    if (!ok(mid)) {
        return bs(lo, mid - 1);
    }
    else if (mid < hi && ok(mid + 1)) {
        return bs(mid + 1, hi);
    }
    else {
        return mid;
    }
}

int32_t main() {
	cin >> n >> s;
	for (int i = 0; i < n; i++) {
		cin >> field[i];
	}
	
	vector<pii> hives;
	// find x and y coordinates for for Mecho, the bees and the cave
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (field[i][j] == 'M') {
				mechox = i;
				mechoy = j;
			} else if (field[i][j] == 'H') {
				hives.push_back(make_pair(i, j));
			} else if (field[i][j] == 'D') {
				home_x = i;
				home_y = j;
			}
		}
	}

    // move bees
    queue<pii> q;
	for (auto [x, y] : hives) {
		q.push(make_pair(x, y));
		bees_visited[x][y] = true;
	}
	
	while (!q.empty()) {
		int x = q.front().first, y = q.front().second;
		q.pop();
		for (int i = 0; i < 4; i++) {
			int nx = x + dx[i], ny = y + dy[i];
			if (valid_sq(nx, ny) && !bees_visited[nx][ny]) {
				bees_time[nx][ny] = bees_time[x][y] + 1;    
				q.push({nx, ny});
				bees_visited[nx][ny] = true;
			}
		}
	}

	cout << bs(0, n * n) << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5460 KB Output is correct
2 Correct 3 ms 5460 KB Output is correct
3 Correct 3 ms 5460 KB Output is correct
4 Correct 3 ms 5460 KB Output is correct
5 Correct 2 ms 5460 KB Output is correct
6 Correct 3 ms 5460 KB Output is correct
7 Correct 166 ms 11456 KB Output is correct
8 Correct 3 ms 5460 KB Output is correct
9 Correct 3 ms 5460 KB Output is correct
10 Correct 3 ms 5460 KB Output is correct
11 Correct 3 ms 5460 KB Output is correct
12 Correct 3 ms 5460 KB Output is correct
13 Incorrect 3 ms 5460 KB Output isn't correct
14 Correct 3 ms 5460 KB Output is correct
15 Correct 3 ms 5460 KB Output is correct
16 Correct 3 ms 5460 KB Output is correct
17 Correct 3 ms 5460 KB Output is correct
18 Correct 3 ms 5460 KB Output is correct
19 Correct 3 ms 5460 KB Output is correct
20 Correct 3 ms 5460 KB Output is correct
21 Correct 3 ms 5460 KB Output is correct
22 Correct 3 ms 5460 KB Output is correct
23 Correct 3 ms 5460 KB Output is correct
24 Correct 3 ms 5460 KB Output is correct
25 Correct 3 ms 5460 KB Output is correct
26 Correct 3 ms 5460 KB Output is correct
27 Correct 3 ms 5460 KB Output is correct
28 Correct 4 ms 5460 KB Output is correct
29 Correct 3 ms 5460 KB Output is correct
30 Correct 3 ms 5460 KB Output is correct
31 Correct 3 ms 5460 KB Output is correct
32 Correct 4 ms 5460 KB Output is correct
33 Correct 18 ms 6760 KB Output is correct
34 Correct 23 ms 6664 KB Output is correct
35 Correct 48 ms 6708 KB Output is correct
36 Correct 25 ms 6980 KB Output is correct
37 Correct 24 ms 6992 KB Output is correct
38 Correct 48 ms 6976 KB Output is correct
39 Correct 24 ms 7356 KB Output is correct
40 Correct 28 ms 7448 KB Output is correct
41 Correct 68 ms 7360 KB Output is correct
42 Correct 39 ms 8064 KB Output is correct
43 Correct 38 ms 7988 KB Output is correct
44 Correct 90 ms 8024 KB Output is correct
45 Correct 42 ms 8536 KB Output is correct
46 Correct 40 ms 8496 KB Output is correct
47 Correct 94 ms 8468 KB Output is correct
48 Correct 48 ms 9128 KB Output is correct
49 Correct 51 ms 9048 KB Output is correct
50 Correct 91 ms 8992 KB Output is correct
51 Correct 55 ms 9692 KB Output is correct
52 Correct 58 ms 9652 KB Output is correct
53 Correct 145 ms 9544 KB Output is correct
54 Correct 70 ms 10148 KB Output is correct
55 Correct 71 ms 10236 KB Output is correct
56 Correct 160 ms 10148 KB Output is correct
57 Correct 81 ms 10888 KB Output is correct
58 Correct 87 ms 10840 KB Output is correct
59 Correct 190 ms 10788 KB Output is correct
60 Correct 90 ms 11756 KB Output is correct
61 Correct 87 ms 11584 KB Output is correct
62 Correct 217 ms 11448 KB Output is correct
63 Correct 163 ms 11452 KB Output is correct
64 Correct 443 ms 11580 KB Output is correct
65 Correct 297 ms 11520 KB Output is correct
66 Correct 241 ms 11500 KB Output is correct
67 Correct 174 ms 11460 KB Output is correct
68 Correct 97 ms 11608 KB Output is correct
69 Correct 124 ms 11488 KB Output is correct
70 Correct 88 ms 11500 KB Output is correct
71 Correct 107 ms 11472 KB Output is correct
72 Incorrect 81 ms 11564 KB Output isn't correct
73 Incorrect 96 ms 11616 KB Output isn't correct
74 Correct 128 ms 11524 KB Output is correct
75 Correct 173 ms 11660 KB Output is correct
76 Correct 122 ms 11544 KB Output is correct
77 Correct 75 ms 11548 KB Output is correct
78 Correct 131 ms 11608 KB Output is correct
79 Correct 118 ms 11524 KB Output is correct
80 Correct 127 ms 11528 KB Output is correct
81 Correct 135 ms 11552 KB Output is correct
82 Correct 138 ms 11524 KB Output is correct
83 Correct 137 ms 11552 KB Output is correct
84 Correct 144 ms 11516 KB Output is correct
85 Correct 174 ms 11524 KB Output is correct
86 Correct 132 ms 11516 KB Output is correct
87 Correct 163 ms 11516 KB Output is correct
88 Correct 151 ms 11480 KB Output is correct
89 Correct 133 ms 11428 KB Output is correct
90 Correct 141 ms 11468 KB Output is correct
91 Correct 174 ms 11504 KB Output is correct
92 Correct 181 ms 11576 KB Output is correct