Submission #712197

# Submission time Handle Problem Language Result Execution time Memory
712197 2023-03-18T11:06:55 Z dubabuba Mecho (IOI09_mecho) C++14
100 / 100
519 ms 5320 KB
#include <bits/stdc++.h>
using namespace std;
 
int di[] = {1, -1, 0, 0};
int dj[] = {0, 0, -1, 1};
 
typedef pair<int, int> pii;
#define ff first
#define ss second
 
const int mxn = 808;
char mp[mxn][mxn];
bool vs[mxn][mxn];
int bee[mxn][mxn];
int n, s, Si, Sj, Di, Dj;
 
bool can(int i, int j) {
	if(mp[i][j] == 'T') return false;
	if(mp[i][j] == 'H') return false;
	return true;
}
 
bool bfs(int M) {
	priority_queue<pair<int, pii>> pq;
	pq.push({0, {Si, Sj}});
 
	memset(vs, 0, sizeof vs);
	vs[Si][Sj] = true;
 
	while(pq.size()) {
		int k = -pq.top().ff;
		int i = pq.top().ss.ff;
		int j = pq.top().ss.ss;
		pq.pop();
 
		if(i == Di && j == Dj) return true;
 
		int minute = k / s;
		if(minute + M >= bee[i][j]) continue;
 
		// cout << minute << ": (" << k << ") " << i << ' ' << j << " - " << bee[i][j] << '\n';
 
		for(int t = 0; t < 4; t++) {
			int I = i + di[t];
			int J = j + dj[t];
 
			if(!vs[I][J] && can(I, J)) {
				vs[I][J] = 1;
				pq.push({-(k + 1), {I, J}});
				// cout << " > " << I << ' ' << J << '\n';
			}
		}
	}
	return false;
}
 
priority_queue<pair<int, pii>> pq;
void beefs() {
	memset(bee, -1, sizeof bee);
 
	while(pq.size()) {
		int k = -pq.top().ff;
		int i = pq.top().ss.ff;
		int j = pq.top().ss.ss;
		pq.pop();
 
		bee[i][j] = k;
 
		for(int t = 0; t < 4; t++) {
			int I = i + di[t];
			int J = j + dj[t];
 
			if(!vs[I][J] && mp[I][J] != 'T' && mp[I][J] != 'D') {
				vs[I][J] = true;
				pq.push({-(k + 1), {I, J}});
			}
		}
	}
}
 
int main() {
	cin >> n >> s;
	for(int i = 1; i <= n; i++)
	for(int j = 1; j <= n; j++) {
		cin >> mp[i][j];
 
		if(mp[i][j] == 'H') {
			pq.push({0, {i, j}});
			vs[i][j] = true;
		}
 
		if(mp[i][j] == 'M') {
			Si = i;
			Sj = j;
		}
 
		if(mp[i][j] == 'D') {
			Di = i;
			Dj = j;
		}
	}
 
	for(int i = 0; i <= n + 1; i++)
	mp[i][0] = mp[i][n + 1] = mp[0][i] = mp[n + 1][i] = 'T';
 
	beefs();
 
	// for(int i = 1; i <= n; i++) {
	// 	for(int j = 1; j <= n; j++)
	// 		cout << mp[i][j] << ' ';
	// 	cout << '\n';
	// }
 
	if(!bfs(0)) {
		cout << -1 << '\n';
		return 0;
	}
 
	int L = 0, R = n * n + 1;
	while(R - L > 1) {
		int M = (L + R) / 2;
		// cout << L << ' ' << R << ": " << M << '\n';
		if(bfs(M)) L = M;
		else R = M;
	}
 
	cout << L << '\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 3 ms 3412 KB Output is correct
3 Correct 2 ms 3412 KB Output is correct
4 Correct 2 ms 3512 KB Output is correct
5 Correct 2 ms 3412 KB Output is correct
6 Correct 2 ms 3512 KB Output is correct
7 Correct 360 ms 4664 KB Output is correct
8 Correct 2 ms 3412 KB Output is correct
9 Correct 2 ms 3412 KB Output is correct
10 Correct 2 ms 3516 KB Output is correct
11 Correct 2 ms 3516 KB Output is correct
12 Correct 3 ms 3432 KB Output is correct
13 Correct 3 ms 3464 KB Output is correct
14 Correct 2 ms 3508 KB Output is correct
15 Correct 2 ms 3512 KB Output is correct
16 Correct 2 ms 3412 KB Output is correct
17 Correct 2 ms 3412 KB Output is correct
18 Correct 3 ms 3512 KB Output is correct
19 Correct 2 ms 3412 KB Output is correct
20 Correct 2 ms 3412 KB Output is correct
21 Correct 2 ms 3540 KB Output is correct
22 Correct 2 ms 3512 KB Output is correct
23 Correct 2 ms 3540 KB Output is correct
24 Correct 2 ms 3508 KB Output is correct
25 Correct 2 ms 3540 KB Output is correct
26 Correct 3 ms 3540 KB Output is correct
27 Correct 3 ms 3540 KB Output is correct
28 Correct 3 ms 3508 KB Output is correct
29 Correct 2 ms 3528 KB Output is correct
30 Correct 3 ms 3540 KB Output is correct
31 Correct 4 ms 3516 KB Output is correct
32 Correct 3 ms 3564 KB Output is correct
33 Correct 11 ms 3776 KB Output is correct
34 Correct 11 ms 3800 KB Output is correct
35 Correct 75 ms 3892 KB Output is correct
36 Correct 12 ms 3924 KB Output is correct
37 Correct 14 ms 3924 KB Output is correct
38 Correct 103 ms 3976 KB Output is correct
39 Correct 16 ms 4032 KB Output is correct
40 Correct 17 ms 4052 KB Output is correct
41 Correct 133 ms 4180 KB Output is correct
42 Correct 21 ms 4164 KB Output is correct
43 Correct 21 ms 3980 KB Output is correct
44 Correct 159 ms 4136 KB Output is correct
45 Correct 22 ms 4108 KB Output is correct
46 Correct 24 ms 4076 KB Output is correct
47 Correct 196 ms 4148 KB Output is correct
48 Correct 27 ms 4060 KB Output is correct
49 Correct 30 ms 4156 KB Output is correct
50 Correct 231 ms 4184 KB Output is correct
51 Correct 30 ms 4196 KB Output is correct
52 Correct 33 ms 4184 KB Output is correct
53 Correct 303 ms 4228 KB Output is correct
54 Correct 36 ms 4292 KB Output is correct
55 Correct 37 ms 4300 KB Output is correct
56 Correct 337 ms 4368 KB Output is correct
57 Correct 42 ms 4288 KB Output is correct
58 Correct 45 ms 4420 KB Output is correct
59 Correct 405 ms 4472 KB Output is correct
60 Correct 45 ms 4428 KB Output is correct
61 Correct 48 ms 4384 KB Output is correct
62 Correct 458 ms 4476 KB Output is correct
63 Correct 299 ms 4460 KB Output is correct
64 Correct 519 ms 4460 KB Output is correct
65 Correct 421 ms 4556 KB Output is correct
66 Correct 365 ms 4460 KB Output is correct
67 Correct 115 ms 4460 KB Output is correct
68 Correct 145 ms 4508 KB Output is correct
69 Correct 143 ms 4512 KB Output is correct
70 Correct 134 ms 4540 KB Output is correct
71 Correct 118 ms 4540 KB Output is correct
72 Correct 99 ms 4428 KB Output is correct
73 Correct 184 ms 5236 KB Output is correct
74 Correct 272 ms 5320 KB Output is correct
75 Correct 315 ms 5304 KB Output is correct
76 Correct 305 ms 5296 KB Output is correct
77 Correct 312 ms 5300 KB Output is correct
78 Correct 161 ms 4868 KB Output is correct
79 Correct 249 ms 4972 KB Output is correct
80 Correct 289 ms 5008 KB Output is correct
81 Correct 288 ms 4808 KB Output is correct
82 Correct 266 ms 4844 KB Output is correct
83 Correct 319 ms 4804 KB Output is correct
84 Correct 345 ms 4812 KB Output is correct
85 Correct 346 ms 4808 KB Output is correct
86 Correct 343 ms 4860 KB Output is correct
87 Correct 342 ms 4808 KB Output is correct
88 Correct 342 ms 4764 KB Output is correct
89 Correct 407 ms 4876 KB Output is correct
90 Correct 416 ms 4844 KB Output is correct
91 Correct 360 ms 4696 KB Output is correct
92 Correct 367 ms 4712 KB Output is correct