답안 #516166

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
516166 2022-01-20T14:23:06 Z Tizariox Mecho (IOI09_mecho) C++14
65 / 100
1000 ms 47932 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int, int>;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define F0R1(i, a) for (int i = 1; i <= (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define F0Rd1(i, a) for (int i = a; i > 0; i--)
#define SORT(vec) sort(vec.begin(), vec.end())
#define S0RT(arr, n) sort(arr, arr + n)

#define pA first
#define pB second
#define MOD 1000000007
#define nl "\n"
#define pb push_back

int n, steps, rootX, rootY, homeX, homeY;

vector<pii> adj[800][800];
bool vis[800][800];
bool visBee[800][800];
set<pii> updateQueue;
bool blocked[800][800];
vector<pii> hives;
queue<pii> beeQueue;
int initialDist = 0;

void moveBees() {
	int sz = beeQueue.size();
	FOR(i, 0, sz) {
		pii node = beeQueue.front();
		beeQueue.pop();
		for (pii neighbor : adj[node.pA][node.pB]) {
			if (!visBee[neighbor.pA][neighbor.pB] && !(neighbor.pA == homeX && neighbor.pB == homeY)) {
				visBee[neighbor.pA][neighbor.pB] = true;
				// updateQueue.insert(neighbor);
				beeQueue.push(neighbor);
			}
		}
	}
}

bool bfs(int eatHoney) {
	FOR(i, 0, n) {
		FOR(j, 0, n) {
			vis[i][j] = false;
			visBee[i][j] = false;
		}
	}
	queue<pii> bfsQueue;
	beeQueue = queue<pii>();
	vis[rootX][rootY] = true;
	bfsQueue.push(pii(rootX, rootY));
	for (pii i : hives) {
		visBee[i.pA][i.pB] = true;
		beeQueue.push(i);
	}
	FOR(i, 0, eatHoney) { moveBees(); }
	// somehow avoid having to replace the queue with every step
	while (!bfsQueue.empty()) {
		FOR(i, 0, steps) {
			if (i == steps - 1) {
				moveBees();
			}
			int sz = bfsQueue.size();
			FOR(j, 0, sz) {
				pii node = bfsQueue.front();
				bfsQueue.pop();
				for (pii neighbor : adj[node.pA][node.pB]) {
					if (!vis[neighbor.pA][neighbor.pB] && (!visBee[neighbor.pA][neighbor.pB])) { // make sure the visbee was done not in the previous round or that i == sz - 1
						vis[neighbor.pA][neighbor.pB] = true;
						if (neighbor.pA == homeX && neighbor.pB == homeY) {
							return true;
						}
						bfsQueue.push(neighbor);
					}
				}
			}
		}
	}
	return false;
}

void gridAdj() {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (!blocked[i][j]) {
				if (i > 0 && !blocked[i - 1][j]) {
					adj[i][j].pb(pii(i - 1, j));
				}
				if (i < n - 1 && !blocked[i + 1][j]) {
					adj[i][j].pb(pii(i + 1, j));
				}
				if (j > 0 && !blocked[i][j - 1]) {
					adj[i][j].pb(pii(i, j - 1));
				}
				if (j < n - 1 && !blocked[i][j + 1]) {
					adj[i][j].pb(pii(i, j + 1));
				}
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> steps;
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		FOR(j, 0, n) {
			if (s[j] == 'T') {
				blocked[i][j] = true;
			} else if (s[j] == 'M') {
				rootX = i;
				rootY = j;
			} else if (s[j] == 'D') {
				homeX = i;
				homeY = j;
			} else if (s[j] == 'H') {
				hives.pb(pii(i, j));
			}
		}
	}
	gridAdj();
	int l = 0;
	int r = 1e6;
	int ans = l - 1;
	while (l <= r) {
		ll mid = (r + l) / 2;
		if (bfs(mid)) {
			ans = mid;
			l = mid + 1;
		} else {
			r = mid - 1;
		}
	}
	cout << ans << nl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 15308 KB Output is correct
2 Correct 13 ms 15356 KB Output is correct
3 Correct 11 ms 15352 KB Output is correct
4 Correct 12 ms 15308 KB Output is correct
5 Correct 12 ms 15356 KB Output is correct
6 Correct 12 ms 15368 KB Output is correct
7 Execution timed out 1057 ms 47924 KB Time limit exceeded
8 Correct 17 ms 15308 KB Output is correct
9 Correct 12 ms 15356 KB Output is correct
10 Correct 12 ms 15352 KB Output is correct
11 Correct 12 ms 15300 KB Output is correct
12 Incorrect 111 ms 15484 KB Output isn't correct
13 Incorrect 15 ms 15516 KB Output isn't correct
14 Correct 15 ms 15612 KB Output is correct
15 Correct 14 ms 15416 KB Output is correct
16 Correct 14 ms 15416 KB Output is correct
17 Correct 13 ms 15356 KB Output is correct
18 Correct 14 ms 15356 KB Output is correct
19 Correct 12 ms 15352 KB Output is correct
20 Correct 13 ms 15344 KB Output is correct
21 Correct 15 ms 15388 KB Output is correct
22 Correct 16 ms 15436 KB Output is correct
23 Correct 15 ms 15516 KB Output is correct
24 Correct 15 ms 15484 KB Output is correct
25 Correct 13 ms 15488 KB Output is correct
26 Correct 17 ms 15484 KB Output is correct
27 Correct 15 ms 15588 KB Output is correct
28 Correct 14 ms 15484 KB Output is correct
29 Correct 13 ms 15564 KB Output is correct
30 Correct 15 ms 15616 KB Output is correct
31 Correct 18 ms 15612 KB Output is correct
32 Correct 13 ms 15564 KB Output is correct
33 Correct 41 ms 18204 KB Output is correct
34 Correct 40 ms 18204 KB Output is correct
35 Correct 116 ms 20084 KB Output is correct
36 Correct 49 ms 18908 KB Output is correct
37 Correct 48 ms 18944 KB Output is correct
38 Correct 209 ms 21416 KB Output is correct
39 Correct 58 ms 19792 KB Output is correct
40 Correct 58 ms 19776 KB Output is correct
41 Correct 256 ms 22896 KB Output is correct
42 Correct 66 ms 20600 KB Output is correct
43 Correct 74 ms 20692 KB Output is correct
44 Correct 345 ms 24508 KB Output is correct
45 Correct 81 ms 21692 KB Output is correct
46 Correct 82 ms 21640 KB Output is correct
47 Correct 405 ms 26368 KB Output is correct
48 Correct 91 ms 22732 KB Output is correct
49 Correct 95 ms 22752 KB Output is correct
50 Correct 521 ms 28344 KB Output is correct
51 Correct 107 ms 23932 KB Output is correct
52 Correct 102 ms 23800 KB Output is correct
53 Correct 605 ms 30468 KB Output is correct
54 Correct 118 ms 25156 KB Output is correct
55 Correct 120 ms 25084 KB Output is correct
56 Correct 717 ms 32760 KB Output is correct
57 Correct 142 ms 26448 KB Output is correct
58 Correct 143 ms 26416 KB Output is correct
59 Correct 842 ms 35216 KB Output is correct
60 Correct 152 ms 27776 KB Output is correct
61 Correct 166 ms 27876 KB Output is correct
62 Correct 956 ms 37820 KB Output is correct
63 Correct 573 ms 31520 KB Output is correct
64 Correct 554 ms 31324 KB Output is correct
65 Correct 559 ms 31388 KB Output is correct
66 Correct 650 ms 31392 KB Output is correct
67 Correct 561 ms 31392 KB Output is correct
68 Correct 709 ms 31428 KB Output is correct
69 Correct 681 ms 31424 KB Output is correct
70 Correct 690 ms 31428 KB Output is correct
71 Correct 676 ms 31428 KB Output is correct
72 Incorrect 793 ms 31428 KB Output isn't correct
73 Execution timed out 1058 ms 47920 KB Time limit exceeded
74 Execution timed out 1018 ms 47796 KB Time limit exceeded
75 Execution timed out 1053 ms 47812 KB Time limit exceeded
76 Execution timed out 1074 ms 47812 KB Time limit exceeded
77 Execution timed out 1057 ms 47904 KB Time limit exceeded
78 Execution timed out 1061 ms 47844 KB Time limit exceeded
79 Execution timed out 1057 ms 47900 KB Time limit exceeded
80 Execution timed out 1036 ms 47896 KB Time limit exceeded
81 Execution timed out 1069 ms 47932 KB Time limit exceeded
82 Execution timed out 1047 ms 47892 KB Time limit exceeded
83 Execution timed out 1047 ms 47716 KB Time limit exceeded
84 Execution timed out 1026 ms 47684 KB Time limit exceeded
85 Execution timed out 1087 ms 47684 KB Time limit exceeded
86 Execution timed out 1041 ms 47684 KB Time limit exceeded
87 Execution timed out 1073 ms 47708 KB Time limit exceeded
88 Execution timed out 1065 ms 47808 KB Time limit exceeded
89 Execution timed out 1090 ms 47728 KB Time limit exceeded
90 Execution timed out 1060 ms 47700 KB Time limit exceeded
91 Execution timed out 1089 ms 47804 KB Time limit exceeded
92 Execution timed out 1070 ms 47828 KB Time limit exceeded