Submission #817567

# Submission time Handle Problem Language Result Execution time Memory
817567 2023-08-09T13:38:03 Z Asamai Mecho (IOI09_mecho) C++17
100 / 100
140 ms 4436 KB
#include <bits/stdc++.h>
 
using namespace std;
 
template<class A, class B> bool maximize(A& x, B y) {if (x < y) return x = y, true; else return false;}
template<class A, class B> bool minimize(A& x, B y) {if (x > y) return x = y, true; else return false;}
 
#define     all(a)                a.begin(), a.end()
#define     pb                    push_back
#define     pf                    push_front
#define     fi                    first
#define     se                    second
 
typedef     long long             ll;
typedef     unsigned long long    ull;
typedef     double                db;
typedef     long double           ld;
typedef     pair<db, db>          pdb;
typedef     pair<ld, ld>          pld;
typedef     pair<int, int>        pii;
typedef     pair<ll, ll>          pll;
typedef     pair<ll, int>         plli;
typedef     pair<int, ll>         pill;
 
const int MAX_N = 800 + 5;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
 
int n, S;
string a[MAX_N];
pii s, t;
int dist[MAX_N][MAX_N];
bool ok[MAX_N][MAX_N];
 
bool inside(int x, int y) {
	return x > 0 && x <= n && y > 0 && y <= n;
}
 
bool check(int mid) {
	if (mid * S >= dist[s.fi][s.se]) return false;

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			ok[i][j] = false;
		}
	}
 
	queue<pair<pii, int>> q;
	q.push({s, mid * S});
	ok[s.fi][s.se] = true;
	while (!q.empty()) {
		int u = q.front().fi.fi;
		int v = q.front().fi.se;
		int time = q.front().se;
 
		q.pop();
 
		for (int k = 0; k < 4; k++) {
			int x = u + dx[k];
			int y = v + dy[k];
 
			if (inside(x, y) && (a[x][y] == 'G' || a[x][y] == 'D') && !ok[x][y] && dist[x][y] > time + 1) {
				ok[x][y] = true;
				q.push({{x, y}, time + 1});
			}
		}
	}
 
	return ok[t.fi][t.se];
}
	
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
 
	queue<pii> q;
	cin >> n >> S;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		a[i] = ' ' + a[i];
		for (int j = 1; j <= n; j++) {
			if (a[i][j] == 'M') {
				s = {i, j};
			}
			if (a[i][j] == 'D') {
				t = {i, j};
			}
			if (a[i][j] == 'H') {
				q.push({i, j});
				dist[i][j] = 1;
			}
		}
	}
 
	while (!q.empty()) {
		int u = q.front().fi;
		int v = q.front().se;
		q.pop();
		for (int k = 0; k < 4; k++) {
			int x = u + dx[k];
			int y = v + dy[k];
			if (inside(x, y) && (a[x][y] == 'G' || a[x][y] == 'M') && !dist[x][y]) {
				dist[x][y] = dist[u][v] + 1;
				q.push({x, y});
			}
		}
	}
 
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			dist[i][j]--;
			dist[i][j] *= S;
		}
	}
	dist[t.fi][t.se] = 1e9;
 
	int low = 0, high = n * n;
	int ans = -1;
	while (low <= high) {
		int mid = (low + high) >> 1;
		if (check(mid)) {
			ans = mid;
			low = mid + 1;
		}
		else {
			high = mid - 1;
		}
	}
 
	cout << ans;
 
	return 0;
}
 
/*
 
 
*/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 60 ms 4232 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 596 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 412 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 468 KB Output is correct
20 Correct 0 ms 468 KB Output is correct
21 Correct 0 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
26 Correct 1 ms 596 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 1 ms 596 KB Output is correct
31 Correct 1 ms 596 KB Output is correct
32 Correct 1 ms 596 KB Output is correct
33 Correct 3 ms 1748 KB Output is correct
34 Correct 3 ms 1748 KB Output is correct
35 Correct 15 ms 1888 KB Output is correct
36 Correct 4 ms 2020 KB Output is correct
37 Correct 4 ms 2004 KB Output is correct
38 Correct 19 ms 2116 KB Output is correct
39 Correct 4 ms 2260 KB Output is correct
40 Correct 5 ms 2260 KB Output is correct
41 Correct 27 ms 2260 KB Output is correct
42 Correct 5 ms 2516 KB Output is correct
43 Correct 6 ms 2580 KB Output is correct
44 Correct 36 ms 2516 KB Output is correct
45 Correct 6 ms 2844 KB Output is correct
46 Correct 7 ms 2844 KB Output is correct
47 Correct 38 ms 2772 KB Output is correct
48 Correct 10 ms 3028 KB Output is correct
49 Correct 9 ms 3116 KB Output is correct
50 Correct 55 ms 3028 KB Output is correct
51 Correct 9 ms 3284 KB Output is correct
52 Correct 9 ms 3368 KB Output is correct
53 Correct 56 ms 3380 KB Output is correct
54 Correct 9 ms 3540 KB Output is correct
55 Correct 11 ms 3632 KB Output is correct
56 Correct 66 ms 3624 KB Output is correct
57 Correct 13 ms 3796 KB Output is correct
58 Correct 13 ms 3868 KB Output is correct
59 Correct 76 ms 3896 KB Output is correct
60 Correct 14 ms 4052 KB Output is correct
61 Correct 13 ms 4172 KB Output is correct
62 Correct 88 ms 4168 KB Output is correct
63 Correct 76 ms 4168 KB Output is correct
64 Correct 140 ms 4160 KB Output is correct
65 Correct 107 ms 4052 KB Output is correct
66 Correct 91 ms 4156 KB Output is correct
67 Correct 90 ms 4152 KB Output is correct
68 Correct 32 ms 4180 KB Output is correct
69 Correct 32 ms 4196 KB Output is correct
70 Correct 23 ms 4196 KB Output is correct
71 Correct 28 ms 4180 KB Output is correct
72 Correct 19 ms 4180 KB Output is correct
73 Correct 21 ms 4408 KB Output is correct
74 Correct 39 ms 4332 KB Output is correct
75 Correct 50 ms 4436 KB Output is correct
76 Correct 43 ms 4332 KB Output is correct
77 Correct 55 ms 4424 KB Output is correct
78 Correct 53 ms 4308 KB Output is correct
79 Correct 38 ms 4416 KB Output is correct
80 Correct 43 ms 4404 KB Output is correct
81 Correct 51 ms 4408 KB Output is correct
82 Correct 41 ms 4412 KB Output is correct
83 Correct 54 ms 4372 KB Output is correct
84 Correct 52 ms 4308 KB Output is correct
85 Correct 54 ms 4360 KB Output is correct
86 Correct 57 ms 4368 KB Output is correct
87 Correct 53 ms 4364 KB Output is correct
88 Correct 71 ms 4308 KB Output is correct
89 Correct 59 ms 4312 KB Output is correct
90 Correct 59 ms 4268 KB Output is correct
91 Correct 57 ms 4312 KB Output is correct
92 Correct 62 ms 4228 KB Output is correct