답안 #492200

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
492200 2021-12-05T23:38:15 Z Evang Mecho (IOI09_mecho) C++17
100 / 100
454 ms 6452 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5324 KB Output is correct
2 Correct 5 ms 5324 KB Output is correct
3 Correct 5 ms 5324 KB Output is correct
4 Correct 5 ms 5324 KB Output is correct
5 Correct 8 ms 5324 KB Output is correct
6 Correct 7 ms 5324 KB Output is correct
7 Correct 362 ms 6092 KB Output is correct
8 Correct 5 ms 5324 KB Output is correct
9 Correct 5 ms 5404 KB Output is correct
10 Correct 6 ms 5324 KB Output is correct
11 Correct 5 ms 5324 KB Output is correct
12 Correct 7 ms 5324 KB Output is correct
13 Correct 7 ms 5324 KB Output is correct
14 Correct 7 ms 5324 KB Output is correct
15 Correct 7 ms 5396 KB Output is correct
16 Correct 5 ms 5324 KB Output is correct
17 Correct 6 ms 5324 KB Output is correct
18 Correct 5 ms 5324 KB Output is correct
19 Correct 6 ms 5324 KB Output is correct
20 Correct 6 ms 5324 KB Output is correct
21 Correct 6 ms 5416 KB Output is correct
22 Correct 6 ms 5324 KB Output is correct
23 Correct 6 ms 5432 KB Output is correct
24 Correct 7 ms 5324 KB Output is correct
25 Correct 8 ms 5324 KB Output is correct
26 Correct 6 ms 5324 KB Output is correct
27 Correct 7 ms 5324 KB Output is correct
28 Correct 6 ms 5324 KB Output is correct
29 Correct 8 ms 5324 KB Output is correct
30 Correct 7 ms 5440 KB Output is correct
31 Correct 8 ms 5324 KB Output is correct
32 Correct 7 ms 5324 KB Output is correct
33 Correct 44 ms 5648 KB Output is correct
34 Correct 41 ms 5580 KB Output is correct
35 Correct 50 ms 5580 KB Output is correct
36 Correct 50 ms 5580 KB Output is correct
37 Correct 50 ms 5696 KB Output is correct
38 Correct 62 ms 5580 KB Output is correct
39 Correct 75 ms 5708 KB Output is correct
40 Correct 63 ms 5708 KB Output is correct
41 Correct 75 ms 5708 KB Output is correct
42 Correct 80 ms 5708 KB Output is correct
43 Correct 99 ms 5760 KB Output is correct
44 Correct 99 ms 5708 KB Output is correct
45 Correct 84 ms 5804 KB Output is correct
46 Correct 94 ms 5804 KB Output is correct
47 Correct 113 ms 5708 KB Output is correct
48 Correct 117 ms 5844 KB Output is correct
49 Correct 104 ms 5836 KB Output is correct
50 Correct 134 ms 5836 KB Output is correct
51 Correct 125 ms 5836 KB Output is correct
52 Correct 126 ms 5884 KB Output is correct
53 Correct 168 ms 5896 KB Output is correct
54 Correct 154 ms 5916 KB Output is correct
55 Correct 147 ms 5836 KB Output is correct
56 Correct 178 ms 5928 KB Output is correct
57 Correct 169 ms 5964 KB Output is correct
58 Correct 164 ms 5964 KB Output is correct
59 Correct 200 ms 5968 KB Output is correct
60 Correct 172 ms 5964 KB Output is correct
61 Correct 178 ms 6000 KB Output is correct
62 Correct 231 ms 6012 KB Output is correct
63 Correct 306 ms 6020 KB Output is correct
64 Correct 342 ms 6024 KB Output is correct
65 Correct 340 ms 6012 KB Output is correct
66 Correct 448 ms 6008 KB Output is correct
67 Correct 294 ms 5964 KB Output is correct
68 Correct 328 ms 6040 KB Output is correct
69 Correct 303 ms 5964 KB Output is correct
70 Correct 301 ms 6040 KB Output is correct
71 Correct 320 ms 6044 KB Output is correct
72 Correct 454 ms 6060 KB Output is correct
73 Correct 291 ms 6452 KB Output is correct
74 Correct 375 ms 6304 KB Output is correct
75 Correct 389 ms 6436 KB Output is correct
76 Correct 370 ms 6220 KB Output is correct
77 Correct 402 ms 6440 KB Output is correct
78 Correct 302 ms 6276 KB Output is correct
79 Correct 385 ms 6276 KB Output is correct
80 Correct 380 ms 6268 KB Output is correct
81 Correct 338 ms 6276 KB Output is correct
82 Correct 324 ms 6192 KB Output is correct
83 Correct 316 ms 6220 KB Output is correct
84 Correct 365 ms 6236 KB Output is correct
85 Correct 377 ms 6240 KB Output is correct
86 Correct 381 ms 6220 KB Output is correct
87 Correct 358 ms 6240 KB Output is correct
88 Correct 324 ms 6304 KB Output is correct
89 Correct 355 ms 6092 KB Output is correct
90 Correct 360 ms 6172 KB Output is correct
91 Correct 351 ms 6092 KB Output is correct
92 Correct 365 ms 6172 KB Output is correct