Submission #393323

# Submission time Handle Problem Language Result Execution time Memory
393323 2021-04-23T07:54:40 Z ngpin04 Mecho (IOI09_mecho) C++14
100 / 100
704 ms 6404 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 && a[u][v] != 'D') {
				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 (mp(i, j) == t)
				return true;
 
			if (bee[i][j] < oo)
				continue;
 
			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 && a[u][v] != 'D') {
					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 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 685 ms 6224 KB Output is 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 Correct 2 ms 716 KB Output is correct
13 Correct 3 ms 588 KB Output is correct
14 Correct 4 ms 716 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 1 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 2 ms 716 KB Output is correct
26 Correct 2 ms 716 KB Output is correct
27 Correct 2 ms 716 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 4 ms 844 KB Output is correct
33 Correct 56 ms 2800 KB Output is correct
34 Correct 47 ms 2884 KB Output is correct
35 Correct 83 ms 2764 KB Output is correct
36 Correct 65 ms 3148 KB Output is correct
37 Correct 61 ms 3168 KB Output is correct
38 Correct 105 ms 3148 KB Output is correct
39 Correct 81 ms 3504 KB Output is correct
40 Correct 77 ms 3404 KB Output is correct
41 Correct 135 ms 3552 KB Output is correct
42 Correct 111 ms 3788 KB Output is correct
43 Correct 94 ms 3788 KB Output is correct
44 Correct 177 ms 3916 KB Output is correct
45 Correct 123 ms 4292 KB Output is correct
46 Correct 111 ms 4172 KB Output is correct
47 Correct 204 ms 4172 KB Output is correct
48 Correct 145 ms 4564 KB Output is correct
49 Correct 135 ms 4576 KB Output is correct
50 Correct 241 ms 4572 KB Output is correct
51 Correct 169 ms 4812 KB Output is correct
52 Correct 156 ms 4920 KB Output is correct
53 Correct 281 ms 4940 KB Output is correct
54 Correct 231 ms 5196 KB Output is correct
55 Correct 178 ms 5196 KB Output is correct
56 Correct 370 ms 5196 KB Output is correct
57 Correct 226 ms 5584 KB Output is correct
58 Correct 241 ms 5624 KB Output is correct
59 Correct 382 ms 5704 KB Output is correct
60 Correct 253 ms 5976 KB Output is correct
61 Correct 237 ms 5980 KB Output is correct
62 Correct 439 ms 5988 KB Output is correct
63 Correct 527 ms 6084 KB Output is correct
64 Correct 539 ms 5992 KB Output is correct
65 Correct 548 ms 5964 KB Output is correct
66 Correct 562 ms 5964 KB Output is correct
67 Correct 534 ms 5984 KB Output is correct
68 Correct 593 ms 6012 KB Output is correct
69 Correct 584 ms 5976 KB Output is correct
70 Correct 541 ms 6024 KB Output is correct
71 Correct 555 ms 6016 KB Output is correct
72 Correct 632 ms 6016 KB Output is correct
73 Correct 605 ms 6404 KB Output is correct
74 Correct 655 ms 6284 KB Output is correct
75 Correct 661 ms 6288 KB Output is correct
76 Correct 667 ms 6340 KB Output is correct
77 Correct 666 ms 6284 KB Output is correct
78 Correct 655 ms 6252 KB Output is correct
79 Correct 671 ms 6276 KB Output is correct
80 Correct 665 ms 6256 KB Output is correct
81 Correct 673 ms 6256 KB Output is correct
82 Correct 661 ms 6248 KB Output is correct
83 Correct 704 ms 6220 KB Output is correct
84 Correct 656 ms 6220 KB Output is correct
85 Correct 655 ms 6220 KB Output is correct
86 Correct 683 ms 6220 KB Output is correct
87 Correct 665 ms 6224 KB Output is correct
88 Correct 676 ms 6212 KB Output is correct
89 Correct 640 ms 6156 KB Output is correct
90 Correct 643 ms 6156 KB Output is correct
91 Correct 676 ms 6156 KB Output is correct
92 Correct 655 ms 6172 KB Output is correct