Submission #777243

# Submission time Handle Problem Language Result Execution time Memory
777243 2023-07-08T20:08:48 Z _ZeyadRafat Mecho (IOI09_mecho) C++14
100 / 100
412 ms 6604 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;
}

Compilation message

mecho.cpp: In function 'bool ok(int)':
mecho.cpp:33:7: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   33 |   auto[r, c] = q.front();
      |       ^
mecho.cpp:58:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   58 |    auto[r, c] = q2.front();
      |        ^
mecho.cpp:79:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   79 |    auto[r, c] = q.front();
      |        ^
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5316 KB Output is correct
2 Correct 4 ms 5332 KB Output is correct
3 Correct 6 ms 5332 KB Output is correct
4 Correct 4 ms 5332 KB Output is correct
5 Correct 4 ms 5332 KB Output is correct
6 Correct 4 ms 5408 KB Output is correct
7 Correct 294 ms 6384 KB Output is correct
8 Correct 4 ms 5332 KB Output is correct
9 Correct 4 ms 5320 KB Output is correct
10 Correct 4 ms 5332 KB Output is correct
11 Correct 4 ms 5332 KB Output is correct
12 Correct 5 ms 5332 KB Output is correct
13 Correct 5 ms 5332 KB Output is correct
14 Correct 5 ms 5456 KB Output is correct
15 Correct 5 ms 5316 KB Output is correct
16 Correct 4 ms 5332 KB Output is correct
17 Correct 4 ms 5320 KB Output is correct
18 Correct 4 ms 5332 KB Output is correct
19 Correct 4 ms 5332 KB Output is correct
20 Correct 4 ms 5332 KB Output is correct
21 Correct 5 ms 5332 KB Output is correct
22 Correct 5 ms 5332 KB Output is correct
23 Correct 5 ms 5316 KB Output is correct
24 Correct 4 ms 5320 KB Output is correct
25 Correct 5 ms 5332 KB Output is correct
26 Correct 5 ms 5332 KB Output is correct
27 Correct 5 ms 5460 KB Output is correct
28 Correct 5 ms 5460 KB Output is correct
29 Correct 5 ms 5460 KB Output is correct
30 Correct 5 ms 5460 KB Output is correct
31 Correct 5 ms 5460 KB Output is correct
32 Correct 5 ms 5460 KB Output is correct
33 Correct 32 ms 5700 KB Output is correct
34 Correct 31 ms 5716 KB Output is correct
35 Correct 38 ms 5716 KB Output is correct
36 Correct 38 ms 5708 KB Output is correct
37 Correct 34 ms 5820 KB Output is correct
38 Correct 48 ms 5832 KB Output is correct
39 Correct 49 ms 5860 KB Output is correct
40 Correct 46 ms 5844 KB Output is correct
41 Correct 59 ms 5868 KB Output is correct
42 Correct 61 ms 5900 KB Output is correct
43 Correct 76 ms 5900 KB Output is correct
44 Correct 75 ms 5912 KB Output is correct
45 Correct 72 ms 5976 KB Output is correct
46 Correct 67 ms 5972 KB Output is correct
47 Correct 90 ms 5988 KB Output is correct
48 Correct 87 ms 5972 KB Output is correct
49 Correct 79 ms 6076 KB Output is correct
50 Correct 117 ms 6088 KB Output is correct
51 Correct 102 ms 6100 KB Output is correct
52 Correct 108 ms 6144 KB Output is correct
53 Correct 124 ms 6100 KB Output is correct
54 Correct 117 ms 6184 KB Output is correct
55 Correct 111 ms 6100 KB Output is correct
56 Correct 143 ms 6188 KB Output is correct
57 Correct 137 ms 6156 KB Output is correct
58 Correct 123 ms 6224 KB Output is correct
59 Correct 196 ms 6228 KB Output is correct
60 Correct 132 ms 6264 KB Output is correct
61 Correct 136 ms 6264 KB Output is correct
62 Correct 188 ms 6268 KB Output is correct
63 Correct 270 ms 6276 KB Output is correct
64 Correct 319 ms 6276 KB Output is correct
65 Correct 309 ms 6196 KB Output is correct
66 Correct 394 ms 6276 KB Output is correct
67 Correct 267 ms 6272 KB Output is correct
68 Correct 285 ms 6228 KB Output is correct
69 Correct 278 ms 6300 KB Output is correct
70 Correct 275 ms 6308 KB Output is correct
71 Correct 272 ms 6332 KB Output is correct
72 Correct 412 ms 6304 KB Output is correct
73 Correct 228 ms 6568 KB Output is correct
74 Correct 309 ms 6604 KB Output is correct
75 Correct 341 ms 6568 KB Output is correct
76 Correct 297 ms 6500 KB Output is correct
77 Correct 314 ms 6484 KB Output is correct
78 Correct 244 ms 6544 KB Output is correct
79 Correct 319 ms 6588 KB Output is correct
80 Correct 311 ms 6584 KB Output is correct
81 Correct 316 ms 6476 KB Output is correct
82 Correct 256 ms 6540 KB Output is correct
83 Correct 254 ms 6500 KB Output is correct
84 Correct 295 ms 6604 KB Output is correct
85 Correct 299 ms 6484 KB Output is correct
86 Correct 296 ms 6504 KB Output is correct
87 Correct 289 ms 6496 KB Output is correct
88 Correct 260 ms 6440 KB Output is correct
89 Correct 295 ms 6440 KB Output is correct
90 Correct 292 ms 6412 KB Output is correct
91 Correct 275 ms 6348 KB Output is correct
92 Correct 290 ms 6444 KB Output is correct