Submission #393074

# Submission time Handle Problem Language Result Execution time Memory
393074 2021-04-22T16:34:24 Z ngpin04 Mecho (IOI09_mecho) C++14
84 / 100
718 ms 6380 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int N = 805; 
const int oo = 1e9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

char a[N][N];

pair <int, int> s,t;

int bee[N][N];
int dis[N][N];
int n,d;

bool ok(int i, int j) {
	return a[i][j] != 'T' && min(i, j) > 0 && max(i, j) <= n;
}

bool solve(int x, queue <pair <int, int>> q) {
	for (int i = 1; i <= n; i++)
	for (int j = 1; j <= n; j++) {
		if (a[i][j] != 'H')
			bee[i][j] = oo;
		if (a[i][j] != 'M')
			dis[i][j] = oo;
	}

	int fir = -1;
	while (q.size()) {
		int i = q.front().fi;
		int j = q.front().se;
		if (fir < 0)
			fir = bee[i][j];
		if (bee[i][j] - fir == x)
			break;
		q.pop();

		for (int dir = 0; dir < 4; dir++) {
			int u = i + dx[dir];
			int v = j + dy[dir];
			if (ok(u, v) && bee[u][v] == oo) {
				bee[u][v] = bee[i][j] + 1;
				q.push(mp(u, v));
			}
		}
	}

	if (bee[s.fi][s.se] < oo)
		return false;

	queue <pair <int, int>> qq;
	qq.push(s);
	while (qq.size()) {
		fir = -1;
		while (qq.size()) {
			int i = qq.front().fi;
			int j = qq.front().se;
			if (fir < 0)
				fir = dis[i][j];
			if (dis[i][j] - fir == d)
				break;
			qq.pop();

			if (bee[i][j] < oo)
				continue;

			if (mp(i, j) == t)
				return true;

			for (int dir = 0; dir < 4; dir++) {
				int u = i + dx[dir];
				int v = j + dy[dir];
				if (ok(u, v) && bee[u][v] == oo && dis[u][v] == oo) {
					dis[u][v] = dis[i][j] + 1;
					qq.push(mp(u, v));
				}
			}
		}

		fir = -1;
		while (q.size()) {
			int i = q.front().fi;
			int j = q.front().se;
			if (fir < 0)
				fir = bee[i][j];
			if (bee[i][j] - fir == 1)
				break;
			q.pop();

			for (int dir = 0; dir < 4; dir++) {
				int u = i + dx[dir];
				int v = j + dy[dir];
				if (ok(u, v) && bee[u][v] == oo) {
					bee[u][v] = bee[i][j] + 1;
					q.push(mp(u, v));
				}
			}
		}
	}
	return false;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> d;

	queue <pair <int, int>> q;
	for (int i = 1; i <= n; i++)
	for (int j = 1; j <= n; j++) {
		cin >> a[i][j];
		if (a[i][j] == 'H') 
			q.push(mp(i, j));
		
		if (a[i][j] == 'M') 
			s = mp(i, j);
	
		if (a[i][j] == 'D')
			t = mp(i, j);
	}
	int lo = -1;
	int hi = 1e9;
	while (hi - lo > 1) {
		int mid = (lo + hi) >> 1;
		if (solve(mid, q))
			lo = mid;
		else 
			hi = mid;
	}
	cout << lo;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Incorrect 1 ms 336 KB Output isn't correct
3 Correct 1 ms 336 KB Output is correct
4 Incorrect 1 ms 332 KB Output isn't correct
5 Correct 1 ms 336 KB Output is correct
6 Incorrect 1 ms 332 KB Output isn't correct
7 Incorrect 648 ms 6096 KB Output isn't correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Incorrect 2 ms 716 KB Output isn't correct
13 Correct 3 ms 588 KB Output is correct
14 Correct 4 ms 756 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 460 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 2 ms 588 KB Output is correct
22 Correct 1 ms 588 KB Output is correct
23 Correct 2 ms 588 KB Output is correct
24 Correct 2 ms 588 KB Output is correct
25 Correct 3 ms 716 KB Output is correct
26 Correct 2 ms 716 KB Output is correct
27 Correct 2 ms 720 KB Output is correct
28 Correct 2 ms 716 KB Output is correct
29 Correct 3 ms 716 KB Output is correct
30 Correct 3 ms 716 KB Output is correct
31 Correct 3 ms 844 KB Output is correct
32 Correct 3 ms 844 KB Output is correct
33 Correct 49 ms 2764 KB Output is correct
34 Correct 46 ms 2764 KB Output is correct
35 Correct 96 ms 2800 KB Output is correct
36 Correct 67 ms 3148 KB Output is correct
37 Correct 62 ms 3152 KB Output is correct
38 Correct 105 ms 3148 KB Output is correct
39 Correct 93 ms 3504 KB Output is correct
40 Correct 79 ms 3480 KB Output is correct
41 Correct 133 ms 3508 KB Output is correct
42 Correct 102 ms 3788 KB Output is correct
43 Correct 96 ms 3852 KB Output is correct
44 Correct 181 ms 3880 KB Output is correct
45 Correct 122 ms 4336 KB Output is correct
46 Correct 118 ms 4216 KB Output is correct
47 Correct 201 ms 4228 KB Output is correct
48 Correct 146 ms 4572 KB Output is correct
49 Correct 134 ms 4556 KB Output is correct
50 Correct 237 ms 4556 KB Output is correct
51 Correct 169 ms 4920 KB Output is correct
52 Correct 163 ms 4920 KB Output is correct
53 Correct 285 ms 4940 KB Output is correct
54 Correct 192 ms 5196 KB Output is correct
55 Correct 187 ms 5316 KB Output is correct
56 Correct 333 ms 5292 KB Output is correct
57 Correct 223 ms 5632 KB Output is correct
58 Correct 236 ms 5752 KB Output is correct
59 Correct 380 ms 5632 KB Output is correct
60 Correct 302 ms 5984 KB Output is correct
61 Correct 243 ms 5980 KB Output is correct
62 Correct 452 ms 6084 KB Output is correct
63 Correct 518 ms 5988 KB Output is correct
64 Correct 556 ms 5984 KB Output is correct
65 Correct 557 ms 5992 KB Output is correct
66 Correct 542 ms 5964 KB Output is correct
67 Correct 542 ms 5988 KB Output is correct
68 Correct 565 ms 5964 KB Output is correct
69 Correct 529 ms 5964 KB Output is correct
70 Correct 525 ms 6012 KB Output is correct
71 Correct 561 ms 6012 KB Output is correct
72 Correct 626 ms 6016 KB Output is correct
73 Correct 580 ms 6284 KB Output is correct
74 Correct 641 ms 6340 KB Output is correct
75 Correct 641 ms 6304 KB Output is correct
76 Correct 718 ms 6340 KB Output is correct
77 Correct 663 ms 6296 KB Output is correct
78 Correct 635 ms 6340 KB Output is correct
79 Correct 639 ms 6252 KB Output is correct
80 Correct 653 ms 6248 KB Output is correct
81 Correct 670 ms 6380 KB Output is correct
82 Correct 656 ms 6256 KB Output is correct
83 Correct 657 ms 6224 KB Output is correct
84 Correct 681 ms 6200 KB Output is correct
85 Correct 657 ms 6220 KB Output is correct
86 Correct 677 ms 6220 KB Output is correct
87 Correct 648 ms 6212 KB Output is correct
88 Correct 656 ms 6156 KB Output is correct
89 Correct 635 ms 6212 KB Output is correct
90 Correct 647 ms 6168 KB Output is correct
91 Correct 648 ms 6176 KB Output is correct
92 Correct 643 ms 6280 KB Output is correct