Submission #492145

# Submission time Handle Problem Language Result Execution time Memory
492145 2021-12-05T16:10:17 Z nehasane Mecho (IOI09_mecho) C++14
5 / 100
562 ms 7380 KB
#include <bits/stdc++.h>
using namespace std;

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

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<pair<int, int>> 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};

	// binary search on waiting time
	int l = 0;
	int r = n * n;
	while (l <= r) {
		vector<vector<bool>> bees_visited(n, vector<bool>(n));
		vector<vector<bool>> mecho_visited(n, vector<bool>(n));
		vector<vector<int>> bees_time(n, vector<int>(n));
		vector<vector<int>> mecho_time(n, vector<int>(n));
		queue<pair<int, int>> q;
		
		int eating_time = (l + r) / 2;

		// move bees
		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;
				}
			}
		}
		
		
		// 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)) {
					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:77:31: warning: 'mechoy' may be used uninitialized in this function [-Wmaybe-uninitialized]
   77 |   mecho_visited[mechox][mechoy] = true;
      |                               ^
mecho.cpp:77:23: warning: 'mechox' may be used uninitialized in this function [-Wmaybe-uninitialized]
   77 |   mecho_visited[mechox][mechoy] = true;
      |                       ^
mecho.cpp:109:29: warning: 'home_y' may be used uninitialized in this function [-Wmaybe-uninitialized]
  109 |    int nx = home_x + dx[i], ny = home_y + dy[i];
      |                             ^~
mecho.cpp:109:8: warning: 'home_x' may be used uninitialized in this function [-Wmaybe-uninitialized]
  109 |    int nx = home_x + dx[i], ny = home_y + dy[i];
      |        ^~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Incorrect 1 ms 204 KB Output isn't correct
3 Incorrect 0 ms 332 KB Output isn't correct
4 Incorrect 0 ms 204 KB Output isn't correct
5 Incorrect 0 ms 204 KB Output isn't correct
6 Incorrect 0 ms 204 KB Output isn't correct
7 Incorrect 520 ms 7116 KB Output isn't correct
8 Incorrect 1 ms 204 KB Output isn't correct
9 Incorrect 1 ms 316 KB Output isn't correct
10 Incorrect 1 ms 332 KB Output isn't correct
11 Incorrect 1 ms 312 KB Output isn't correct
12 Correct 2 ms 332 KB Output is correct
13 Incorrect 2 ms 332 KB Output isn't correct
14 Incorrect 2 ms 332 KB Output isn't correct
15 Incorrect 1 ms 332 KB Output isn't correct
16 Incorrect 1 ms 332 KB Output isn't correct
17 Incorrect 1 ms 332 KB Output isn't correct
18 Incorrect 1 ms 332 KB Output isn't correct
19 Incorrect 1 ms 236 KB Output isn't correct
20 Incorrect 1 ms 332 KB Output isn't correct
21 Incorrect 1 ms 332 KB Output isn't correct
22 Incorrect 1 ms 340 KB Output isn't correct
23 Incorrect 1 ms 320 KB Output isn't correct
24 Incorrect 1 ms 332 KB Output isn't correct
25 Incorrect 2 ms 332 KB Output isn't correct
26 Incorrect 2 ms 332 KB Output isn't correct
27 Incorrect 3 ms 332 KB Output isn't correct
28 Incorrect 1 ms 332 KB Output isn't correct
29 Incorrect 2 ms 332 KB Output isn't correct
30 Incorrect 1 ms 332 KB Output isn't correct
31 Incorrect 2 ms 332 KB Output isn't correct
32 Incorrect 2 ms 332 KB Output isn't correct
33 Incorrect 41 ms 1660 KB Output isn't correct
34 Incorrect 43 ms 1664 KB Output isn't correct
35 Incorrect 72 ms 1656 KB Output isn't correct
36 Incorrect 55 ms 2032 KB Output isn't correct
37 Incorrect 53 ms 1996 KB Output isn't correct
38 Incorrect 106 ms 2116 KB Output isn't correct
39 Incorrect 67 ms 2464 KB Output isn't correct
40 Incorrect 64 ms 2472 KB Output isn't correct
41 Incorrect 132 ms 2436 KB Output isn't correct
42 Incorrect 90 ms 3156 KB Output isn't correct
43 Incorrect 103 ms 3152 KB Output isn't correct
44 Incorrect 169 ms 3148 KB Output isn't correct
45 Incorrect 110 ms 3664 KB Output isn't correct
46 Incorrect 102 ms 3660 KB Output isn't correct
47 Incorrect 223 ms 3660 KB Output isn't correct
48 Incorrect 130 ms 4420 KB Output isn't correct
49 Incorrect 134 ms 4260 KB Output isn't correct
50 Incorrect 245 ms 4264 KB Output isn't correct
51 Incorrect 195 ms 4916 KB Output isn't correct
52 Incorrect 153 ms 4936 KB Output isn't correct
53 Incorrect 288 ms 4812 KB Output isn't correct
54 Incorrect 192 ms 5604 KB Output isn't correct
55 Incorrect 175 ms 5560 KB Output isn't correct
56 Incorrect 330 ms 5528 KB Output isn't correct
57 Incorrect 203 ms 6260 KB Output isn't correct
58 Incorrect 197 ms 6260 KB Output isn't correct
59 Incorrect 405 ms 6340 KB Output isn't correct
60 Incorrect 239 ms 7012 KB Output isn't correct
61 Incorrect 225 ms 7012 KB Output isn't correct
62 Incorrect 485 ms 7008 KB Output isn't correct
63 Incorrect 435 ms 7016 KB Output isn't correct
64 Incorrect 455 ms 7072 KB Output isn't correct
65 Incorrect 477 ms 7212 KB Output isn't correct
66 Incorrect 462 ms 7020 KB Output isn't correct
67 Incorrect 417 ms 7136 KB Output isn't correct
68 Incorrect 452 ms 7032 KB Output isn't correct
69 Incorrect 481 ms 7040 KB Output isn't correct
70 Incorrect 456 ms 7036 KB Output isn't correct
71 Incorrect 460 ms 7052 KB Output isn't correct
72 Incorrect 464 ms 7036 KB Output isn't correct
73 Incorrect 492 ms 7316 KB Output isn't correct
74 Incorrect 515 ms 7364 KB Output isn't correct
75 Incorrect 498 ms 7360 KB Output isn't correct
76 Incorrect 489 ms 7320 KB Output isn't correct
77 Incorrect 538 ms 7380 KB Output isn't correct
78 Incorrect 495 ms 7276 KB Output isn't correct
79 Incorrect 476 ms 7276 KB Output isn't correct
80 Incorrect 448 ms 7276 KB Output isn't correct
81 Incorrect 478 ms 7364 KB Output isn't correct
82 Incorrect 464 ms 7188 KB Output isn't correct
83 Incorrect 507 ms 7244 KB Output isn't correct
84 Incorrect 524 ms 7288 KB Output isn't correct
85 Incorrect 505 ms 7232 KB Output isn't correct
86 Incorrect 532 ms 7236 KB Output isn't correct
87 Incorrect 526 ms 7244 KB Output isn't correct
88 Incorrect 526 ms 7184 KB Output isn't correct
89 Incorrect 558 ms 7296 KB Output isn't correct
90 Incorrect 512 ms 7176 KB Output isn't correct
91 Incorrect 531 ms 7304 KB Output isn't correct
92 Incorrect 562 ms 7192 KB Output isn't correct