Submission #629689

# Submission time Handle Problem Language Result Execution time Memory
629689 2022-08-14T22:16:17 Z 54skyxenon Mecho (IOI09_mecho) C++17
100 / 100
245 ms 6580 KB
// From https://usaco.guide/problems/ioi-2009mecho/solution
 
#include <bits/stdc++.h>
using namespace std;

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

vector<string> field(MAX_N);
int n, s;

vector<vector<bool>> bees_visited(MAX_N, vector<bool>(MAX_N));
vector<vector<int>> bees_time(MAX_N, vector<int>(MAX_N));
 
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;
}
 
int main() {
	cin >> n >> s;
	for (int i = 0; i < n; i++) {
		cin >> field[i];
	}
	
	vector<pii> hives;
	int mechox, mechoy, home_x, home_y;
	// 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({i, j});
			} else if (field[i][j] == 'D') {
				home_x = i;
				home_y = j;
			}
		}
	}
 
	int dx[] = {-1, 0, 1, 0};
	int dy[] = {0, -1, 0, 1};

    // move bees
    queue<pii> q;
	for (auto i : hives) {
		q.push({i.first, i.second});
		bees_visited[i.first][i.second] = 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;
			}
		}
	}
 
	// binary search on waiting time
	int l = 0;
	int r = n * n;
	while (l <= r) {
		vector<vector<bool>> mecho_visited(n, vector<bool>(n));
		vector<vector<int>> mecho_time(n, vector<int>(n));
		
		int eating_time = (l + r) / 2;
		
		// 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
		bool reached = false;
		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]) {
					reached = true;
				}
		}
		if (reached) {
			l = eating_time + 1;
		} else {
			r = eating_time - 1;
		}
	}
 
	cout << l - 1 << '\n';
}

Compilation message

mecho.cpp: In function 'int main()':
mecho.cpp:80:23: warning: 'mechox' may be used uninitialized in this function [-Wmaybe-uninitialized]
   80 |   mecho_visited[mechox][mechoy] = true;
      |                       ^
mecho.cpp:111:29: warning: 'home_y' may be used uninitialized in this function [-Wmaybe-uninitialized]
  111 |    int nx = home_x + dx[i], ny = home_y + dy[i];
      |                             ^~
mecho.cpp:111:8: warning: 'home_x' may be used uninitialized in this function [-Wmaybe-uninitialized]
  111 |    int nx = home_x + dx[i], ny = home_y + dy[i];
      |        ^~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2900 KB Output is correct
2 Correct 2 ms 2900 KB Output is correct
3 Correct 2 ms 2900 KB Output is correct
4 Correct 2 ms 2900 KB Output is correct
5 Correct 2 ms 2900 KB Output is correct
6 Correct 2 ms 2900 KB Output is correct
7 Correct 125 ms 6448 KB Output is correct
8 Correct 2 ms 2900 KB Output is correct
9 Correct 2 ms 2900 KB Output is correct
10 Correct 2 ms 2900 KB Output is correct
11 Correct 2 ms 2900 KB Output is correct
12 Correct 2 ms 2900 KB Output is correct
13 Correct 2 ms 2900 KB Output is correct
14 Correct 2 ms 2900 KB Output is correct
15 Correct 2 ms 2900 KB Output is correct
16 Correct 2 ms 2900 KB Output is correct
17 Correct 2 ms 2900 KB Output is correct
18 Correct 2 ms 2900 KB Output is correct
19 Correct 2 ms 2900 KB Output is correct
20 Correct 2 ms 2900 KB Output is correct
21 Correct 2 ms 2900 KB Output is correct
22 Correct 2 ms 2996 KB Output is correct
23 Correct 3 ms 2900 KB Output is correct
24 Correct 2 ms 2900 KB Output is correct
25 Correct 2 ms 2900 KB Output is correct
26 Correct 2 ms 2900 KB Output is correct
27 Correct 2 ms 2900 KB Output is correct
28 Correct 2 ms 2900 KB Output is correct
29 Correct 2 ms 2900 KB Output is correct
30 Correct 3 ms 2900 KB Output is correct
31 Correct 2 ms 2900 KB Output is correct
32 Correct 2 ms 2900 KB Output is correct
33 Correct 10 ms 3732 KB Output is correct
34 Correct 12 ms 3664 KB Output is correct
35 Correct 31 ms 3728 KB Output is correct
36 Correct 13 ms 3796 KB Output is correct
37 Correct 14 ms 3832 KB Output is correct
38 Correct 42 ms 3848 KB Output is correct
39 Correct 15 ms 4052 KB Output is correct
40 Correct 16 ms 4052 KB Output is correct
41 Correct 58 ms 4088 KB Output is correct
42 Correct 22 ms 4544 KB Output is correct
43 Correct 21 ms 4504 KB Output is correct
44 Correct 68 ms 4500 KB Output is correct
45 Correct 23 ms 4756 KB Output is correct
46 Correct 24 ms 4796 KB Output is correct
47 Correct 78 ms 4812 KB Output is correct
48 Correct 27 ms 5092 KB Output is correct
49 Correct 28 ms 5104 KB Output is correct
50 Correct 94 ms 5060 KB Output is correct
51 Correct 31 ms 5388 KB Output is correct
52 Correct 33 ms 5388 KB Output is correct
53 Correct 118 ms 5396 KB Output is correct
54 Correct 37 ms 5716 KB Output is correct
55 Correct 39 ms 5708 KB Output is correct
56 Correct 134 ms 5672 KB Output is correct
57 Correct 44 ms 6068 KB Output is correct
58 Correct 45 ms 6064 KB Output is correct
59 Correct 152 ms 6040 KB Output is correct
60 Correct 47 ms 6428 KB Output is correct
61 Correct 50 ms 6448 KB Output is correct
62 Correct 174 ms 6392 KB Output is correct
63 Correct 140 ms 6408 KB Output is correct
64 Correct 245 ms 6428 KB Output is correct
65 Correct 193 ms 6404 KB Output is correct
66 Correct 172 ms 6436 KB Output is correct
67 Correct 154 ms 6400 KB Output is correct
68 Correct 78 ms 6448 KB Output is correct
69 Correct 87 ms 6432 KB Output is correct
70 Correct 62 ms 6436 KB Output is correct
71 Correct 69 ms 6516 KB Output is correct
72 Correct 56 ms 6496 KB Output is correct
73 Correct 64 ms 6460 KB Output is correct
74 Correct 106 ms 6516 KB Output is correct
75 Correct 113 ms 6508 KB Output is correct
76 Correct 103 ms 6552 KB Output is correct
77 Correct 101 ms 6484 KB Output is correct
78 Correct 112 ms 6428 KB Output is correct
79 Correct 82 ms 6376 KB Output is correct
80 Correct 96 ms 6432 KB Output is correct
81 Correct 105 ms 6432 KB Output is correct
82 Correct 98 ms 6460 KB Output is correct
83 Correct 116 ms 6484 KB Output is correct
84 Correct 109 ms 6420 KB Output is correct
85 Correct 118 ms 6452 KB Output is correct
86 Correct 119 ms 6580 KB Output is correct
87 Correct 114 ms 6428 KB Output is correct
88 Correct 121 ms 6452 KB Output is correct
89 Correct 120 ms 6444 KB Output is correct
90 Correct 129 ms 6420 KB Output is correct
91 Correct 121 ms 6452 KB Output is correct
92 Correct 132 ms 6448 KB Output is correct