Submission #605577

# Submission time Handle Problem Language Result Execution time Memory
605577 2022-07-25T18:58:16 Z tonfai_nokhong Mecho (IOI09_mecho) C++17
100 / 100
643 ms 7448 KB
//Mecho
#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;
	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;

		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;
				}
			}
		}
		
		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];
				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;
				}
			}
		}
       
		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:74:23: warning: 'mechox' may be used uninitialized in this function [-Wmaybe-uninitialized]
   74 |   mecho_visited[mechox][mechoy] = true;
      |                       ^
mecho.cpp:96:29: warning: 'home_y' may be used uninitialized in this function [-Wmaybe-uninitialized]
   96 |    int nx = home_x + dx[i], ny = home_y + dy[i];
      |                             ^~
mecho.cpp:96:8: warning: 'home_x' may be used uninitialized in this function [-Wmaybe-uninitialized]
   96 |    int nx = home_x + dx[i], ny = home_y + dy[i];
      |        ^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 542 ms 7128 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 3 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 40 ms 1696 KB Output is correct
34 Correct 39 ms 1684 KB Output is correct
35 Correct 77 ms 1652 KB Output is correct
36 Correct 50 ms 2048 KB Output is correct
37 Correct 53 ms 2064 KB Output is correct
38 Correct 101 ms 2032 KB Output is correct
39 Correct 63 ms 2444 KB Output is correct
40 Correct 65 ms 2456 KB Output is correct
41 Correct 131 ms 2388 KB Output is correct
42 Correct 90 ms 3164 KB Output is correct
43 Correct 83 ms 3176 KB Output is correct
44 Correct 165 ms 3168 KB Output is correct
45 Correct 104 ms 3680 KB Output is correct
46 Correct 101 ms 3684 KB Output is correct
47 Correct 198 ms 3792 KB Output is correct
48 Correct 118 ms 4276 KB Output is correct
49 Correct 124 ms 4300 KB Output is correct
50 Correct 238 ms 4260 KB Output is correct
51 Correct 149 ms 4908 KB Output is correct
52 Correct 144 ms 4872 KB Output is correct
53 Correct 306 ms 4864 KB Output is correct
54 Correct 176 ms 5548 KB Output is correct
55 Correct 174 ms 5564 KB Output is correct
56 Correct 422 ms 5540 KB Output is correct
57 Correct 195 ms 6276 KB Output is correct
58 Correct 204 ms 6384 KB Output is correct
59 Correct 385 ms 6228 KB Output is correct
60 Correct 241 ms 7036 KB Output is correct
61 Correct 224 ms 7164 KB Output is correct
62 Correct 438 ms 7000 KB Output is correct
63 Correct 508 ms 6980 KB Output is correct
64 Correct 643 ms 7028 KB Output is correct
65 Correct 586 ms 7016 KB Output is correct
66 Correct 560 ms 7088 KB Output is correct
67 Correct 540 ms 7148 KB Output is correct
68 Correct 453 ms 7048 KB Output is correct
69 Correct 499 ms 7116 KB Output is correct
70 Correct 454 ms 7100 KB Output is correct
71 Correct 479 ms 7044 KB Output is correct
72 Correct 488 ms 7052 KB Output is correct
73 Correct 452 ms 7320 KB Output is correct
74 Correct 502 ms 7448 KB Output is correct
75 Correct 559 ms 7320 KB Output is correct
76 Correct 507 ms 7244 KB Output is correct
77 Correct 538 ms 7444 KB Output is correct
78 Correct 491 ms 7292 KB Output is correct
79 Correct 480 ms 7288 KB Output is correct
80 Correct 502 ms 7352 KB Output is correct
81 Correct 476 ms 7284 KB Output is correct
82 Correct 483 ms 7280 KB Output is correct
83 Correct 544 ms 7236 KB Output is correct
84 Correct 516 ms 7380 KB Output is correct
85 Correct 538 ms 7252 KB Output is correct
86 Correct 527 ms 7244 KB Output is correct
87 Correct 525 ms 7368 KB Output is correct
88 Correct 518 ms 7184 KB Output is correct
89 Correct 515 ms 7184 KB Output is correct
90 Correct 526 ms 7188 KB Output is correct
91 Correct 528 ms 7188 KB Output is correct
92 Correct 548 ms 7184 KB Output is correct