답안 #393073

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
393073 2021-04-22T16:33:17 Z ngpin04 Mecho (IOI09_mecho) C++14
84 / 100
720 ms 6408 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;

			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 dis[t.fi][t.se] < oo;
}

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;
	}
	assert(lo < (int) 1e9 - 1);
	cout << lo;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Incorrect 1 ms 332 KB Output isn't correct
3 Correct 1 ms 332 KB Output is correct
4 Incorrect 1 ms 336 KB Output isn't correct
5 Correct 1 ms 332 KB Output is correct
6 Incorrect 1 ms 332 KB Output isn't correct
7 Incorrect 674 ms 6228 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 6 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 464 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 3 ms 716 KB Output is correct
28 Correct 2 ms 716 KB Output is correct
29 Correct 4 ms 716 KB Output is correct
30 Correct 4 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 52 ms 2788 KB Output is correct
34 Correct 47 ms 2692 KB Output is correct
35 Correct 90 ms 2792 KB Output is correct
36 Correct 67 ms 3164 KB Output is correct
37 Correct 65 ms 3148 KB Output is correct
38 Correct 111 ms 3164 KB Output is correct
39 Correct 84 ms 3404 KB Output is correct
40 Correct 77 ms 3500 KB Output is correct
41 Correct 135 ms 3592 KB Output is correct
42 Correct 118 ms 3788 KB Output is correct
43 Correct 100 ms 3864 KB Output is correct
44 Correct 169 ms 3788 KB Output is correct
45 Correct 128 ms 4172 KB Output is correct
46 Correct 113 ms 4172 KB Output is correct
47 Correct 210 ms 4172 KB Output is correct
48 Correct 147 ms 4676 KB Output is correct
49 Correct 134 ms 4568 KB Output is correct
50 Correct 272 ms 4552 KB Output is correct
51 Correct 173 ms 4812 KB Output is correct
52 Correct 190 ms 4920 KB Output is correct
53 Correct 286 ms 4940 KB Output is correct
54 Correct 202 ms 5272 KB Output is correct
55 Correct 183 ms 5196 KB Output is correct
56 Correct 338 ms 5284 KB Output is correct
57 Correct 265 ms 5628 KB Output is correct
58 Correct 201 ms 5540 KB Output is correct
59 Correct 397 ms 5648 KB Output is correct
60 Correct 267 ms 5964 KB Output is correct
61 Correct 241 ms 5984 KB Output is correct
62 Correct 432 ms 5992 KB Output is correct
63 Correct 557 ms 5992 KB Output is correct
64 Correct 549 ms 5988 KB Output is correct
65 Correct 535 ms 6084 KB Output is correct
66 Correct 545 ms 5984 KB Output is correct
67 Correct 504 ms 5964 KB Output is correct
68 Correct 577 ms 6020 KB Output is correct
69 Correct 529 ms 6044 KB Output is correct
70 Correct 533 ms 6024 KB Output is correct
71 Correct 552 ms 6016 KB Output is correct
72 Correct 630 ms 6016 KB Output is correct
73 Correct 579 ms 6288 KB Output is correct
74 Correct 651 ms 6408 KB Output is correct
75 Correct 662 ms 6408 KB Output is correct
76 Correct 667 ms 6284 KB Output is correct
77 Correct 720 ms 6288 KB Output is correct
78 Correct 704 ms 6148 KB Output is correct
79 Correct 673 ms 6248 KB Output is correct
80 Correct 650 ms 6252 KB Output is correct
81 Correct 659 ms 6264 KB Output is correct
82 Correct 666 ms 6260 KB Output is correct
83 Correct 648 ms 6220 KB Output is correct
84 Correct 685 ms 6268 KB Output is correct
85 Correct 660 ms 6200 KB Output is correct
86 Correct 658 ms 6220 KB Output is correct
87 Correct 653 ms 6220 KB Output is correct
88 Correct 671 ms 6160 KB Output is correct
89 Correct 656 ms 6156 KB Output is correct
90 Correct 671 ms 6160 KB Output is correct
91 Correct 658 ms 6156 KB Output is correct
92 Correct 655 ms 6092 KB Output is correct