Submission #943122

# Submission time Handle Problem Language Result Execution time Memory
943122 2024-03-11T08:32:20 Z tkm_algorithms Mecho (IOI09_mecho) C++17
100 / 100
432 ms 7112 KB
#include <bits/stdc++.h>
using namespace std;


#define ff first
#define ss second
using pi = pair<int, int>;
using vpi = vector<pi>;

const int N = 805;
int n, s, b[N][N], m[N][N];
int dr[] = {0, 0, -1, 1};
int dc[] = {-1, 1, 0, 0};
char a[N][N];  // the grid
pi st;  // the starting position for mecho
vpi h;  // locations of all of the bee hives

bool good(int r, int c){  // is cell (r, c) within bounds?
	return 0 <= r && r < n && 0 <= c && c < n;
}

bool ok(int t){  // can the answer be t?
	memset(b, -1, sizeof b);
	memset(m, -1, sizeof m);
	queue<pi> q;  // all of the current bees' possible locations
	for(auto x: h){
		q.push(x);
		b[x.ff][x.ss] = 0;
	}

	// the bees "expand" for t seconds
	while(!q.empty()){  // bfs
		auto[r, c] = q.front();

		if(b[r][c]==t)
			break;
		q.pop();

		for(int i = 0; i < 4; ++i){
			int r2 = r + dr[i];
			int c2 = c + dc[i];
			if(good(r2, c2)&&(a[r2][c2]=='M'||a[r2][c2]=='G')&&b[r2][c2]==-1){
				b[r2][c2] = b[r][c] + 1;
				q.emplace(r2, c2);
			}
		}
	}

	queue<pi> q2;  // mecho's possible locations
	q2.push(st);
	m[st.ff][st.ss] = 0;
	int t2 = t;
	while(!q2.empty()){
		t2++;

		// mecho's possible locations "expand" for 1 second (where he can take up to s steps)
		while(!q2.empty()){ // bfs
			auto[r, c] = q2.front();
			if(m[r][c]==(t2-t)*s)
				break;
			q2.pop();
			if(~b[r][c])
				continue;
			for(int i = 0; i < 4; ++i){
				int r2 = r + dr[i];
				int c2 = c + dc[i];
				if(good(r2, c2)&&b[r2][c2]==-1&&m[r2][c2]==-1&&
						(a[r2][c2]=='G'||a[r2][c2]=='D')){
					if(a[r2][c2]=='D')  // did mecho just find home??
						return true;  // success!
					m[r2][c2] = m[r][c] + 1;
					q2.emplace(r2, c2);
				}
			}
		}

		// the bees' possible locations "expand" by 1 second
		while(!q.empty()){  // bfs
			auto[r, c] = q.front();

			if(b[r][c]==t2)
				break;
			q.pop();
			for(int i = 0; i < 4; ++i){
				int r2 = r + dr[i];
				int c2 = c + dc[i];

				if(good(r2, c2)&&b[r2][c2]==-1&&(a[r2][c2]=='G'||a[r2][c2]=='M')){
					b[r2][c2] = b[r][c] + 1;
					q.emplace(r2, c2);
				}
			}
		}
	}
	return false;  // mecho never found home without encountering bees :(
}

int main() {
	ios::sync_with_stdio(0); cin.tie(0);

	cin >> n >> s;
	for(int i = 0; i < n; ++i)
		for(int j = 0; j < n; ++j){
			cin >> a[i][j];
			if(a[i][j]=='M')
				st = {i, j};
			else if(a[i][j]=='H')
				h.emplace_back(i, j);
		}

	// binary search (yes this binary search might not be commonly seen, but it's shorter to write)
	int ans = -1;
	for(int u = int(1e6); u > 0; u /= 2)
		while(ans + u <= int(1e6) && ok(ans + u))
			ans += u;
	cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5980 KB Output is correct
2 Correct 4 ms 5976 KB Output is correct
3 Correct 4 ms 5980 KB Output is correct
4 Correct 3 ms 5980 KB Output is correct
5 Correct 3 ms 5980 KB Output is correct
6 Correct 4 ms 5980 KB Output is correct
7 Correct 305 ms 6316 KB Output is correct
8 Correct 3 ms 5980 KB Output is correct
9 Correct 3 ms 5980 KB Output is correct
10 Correct 4 ms 5980 KB Output is correct
11 Correct 4 ms 5980 KB Output is correct
12 Correct 4 ms 5980 KB Output is correct
13 Correct 4 ms 6048 KB Output is correct
14 Correct 4 ms 5980 KB Output is correct
15 Correct 4 ms 5980 KB Output is correct
16 Correct 3 ms 5980 KB Output is correct
17 Correct 4 ms 5980 KB Output is correct
18 Correct 3 ms 5980 KB Output is correct
19 Correct 4 ms 5980 KB Output is correct
20 Correct 4 ms 5980 KB Output is correct
21 Correct 4 ms 5980 KB Output is correct
22 Correct 4 ms 5976 KB Output is correct
23 Correct 4 ms 5876 KB Output is correct
24 Correct 4 ms 5980 KB Output is correct
25 Correct 4 ms 5980 KB Output is correct
26 Correct 5 ms 5980 KB Output is correct
27 Correct 6 ms 5980 KB Output is correct
28 Correct 4 ms 6008 KB Output is correct
29 Correct 6 ms 5980 KB Output is correct
30 Correct 5 ms 6056 KB Output is correct
31 Correct 6 ms 5976 KB Output is correct
32 Correct 5 ms 5980 KB Output is correct
33 Correct 31 ms 6248 KB Output is correct
34 Correct 30 ms 6236 KB Output is correct
35 Correct 37 ms 6236 KB Output is correct
36 Correct 38 ms 6236 KB Output is correct
37 Correct 34 ms 6236 KB Output is correct
38 Correct 51 ms 6240 KB Output is correct
39 Correct 49 ms 6236 KB Output is correct
40 Correct 45 ms 6236 KB Output is correct
41 Correct 75 ms 6220 KB Output is correct
42 Correct 68 ms 6312 KB Output is correct
43 Correct 64 ms 6236 KB Output is correct
44 Correct 78 ms 6232 KB Output is correct
45 Correct 62 ms 6232 KB Output is correct
46 Correct 68 ms 6344 KB Output is correct
47 Correct 93 ms 6232 KB Output is correct
48 Correct 86 ms 6236 KB Output is correct
49 Correct 88 ms 6364 KB Output is correct
50 Correct 103 ms 6236 KB Output is correct
51 Correct 108 ms 6232 KB Output is correct
52 Correct 94 ms 6404 KB Output is correct
53 Correct 124 ms 6488 KB Output is correct
54 Correct 118 ms 6232 KB Output is correct
55 Correct 114 ms 6488 KB Output is correct
56 Correct 148 ms 6480 KB Output is correct
57 Correct 123 ms 6224 KB Output is correct
58 Correct 142 ms 6508 KB Output is correct
59 Correct 164 ms 6512 KB Output is correct
60 Correct 131 ms 6488 KB Output is correct
61 Correct 132 ms 6552 KB Output is correct
62 Correct 206 ms 6568 KB Output is correct
63 Correct 293 ms 6548 KB Output is correct
64 Correct 301 ms 6524 KB Output is correct
65 Correct 286 ms 6584 KB Output is correct
66 Correct 403 ms 6416 KB Output is correct
67 Correct 266 ms 6564 KB Output is correct
68 Correct 334 ms 6672 KB Output is correct
69 Correct 275 ms 6560 KB Output is correct
70 Correct 292 ms 6608 KB Output is correct
71 Correct 306 ms 6624 KB Output is correct
72 Correct 432 ms 6604 KB Output is correct
73 Correct 270 ms 6864 KB Output is correct
74 Correct 363 ms 7112 KB Output is correct
75 Correct 351 ms 6904 KB Output is correct
76 Correct 352 ms 6836 KB Output is correct
77 Correct 368 ms 6880 KB Output is correct
78 Correct 280 ms 6820 KB Output is correct
79 Correct 317 ms 6740 KB Output is correct
80 Correct 353 ms 6780 KB Output is correct
81 Correct 311 ms 6788 KB Output is correct
82 Correct 275 ms 6800 KB Output is correct
83 Correct 276 ms 6796 KB Output is correct
84 Correct 332 ms 6624 KB Output is correct
85 Correct 333 ms 6736 KB Output is correct
86 Correct 343 ms 6816 KB Output is correct
87 Correct 333 ms 6632 KB Output is correct
88 Correct 306 ms 6752 KB Output is correct
89 Correct 315 ms 6708 KB Output is correct
90 Correct 338 ms 6748 KB Output is correct
91 Correct 332 ms 6744 KB Output is correct
92 Correct 342 ms 6756 KB Output is correct