Submission #713175

# Submission time Handle Problem Language Result Execution time Memory
713175 2023-03-21T09:23:40 Z ismayil Mecho (IOI09_mecho) C++17
100 / 100
268 ms 21620 KB
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
char grid[801][801];
ll dis_b[801][801], color_b[801][801];
ll color[801][801], dis[801][801];
ll dx[4] = {0, 0, 1, -1};
ll dy[4] = {1, -1, 0, 0};
ll n, s;
pair<ll, ll> finish;
bool valid(ll i, ll j){
	return (0 <= i && i < n && 0 <= j && j < n);
}
void bfs_b(queue<pair<ll, ll>> &q){
	while(!q.empty()){
		ll x = q.front().first, y = q.front().second;
		q.pop();
		for(ll i = 0; i < 4; i++){
			if(valid(x + dx[i], y + dy[i]) && !color_b[x + dx[i]][y + dy[i]] && 
			(grid[x + dx[i]][y + dy[i]] == 'G' || grid[x + dx[i]][y + dy[i]] == 'M')){
				dis_b[x + dx[i]][y + dy[i]] = dis_b[x][y] + 1;
				color_b[x + dx[i]][y + dy[i]] = 1;
				q.push({x + dx[i], y + dy[i]});
			}
		} 
	}
}
bool bfs_m(ll i, ll j, ll t){
	memset(color, 0, sizeof(color));
	memset(dis, 0, sizeof(dis));
	dis[i][j] = t;
	color[i][j] = 1;
	if(dis[i][j] >= dis_b[i][j] * s) return false;
	queue<pair<ll, ll>> q;
	q.push({i, j});
	while(!q.empty()){
		ll x = q.front().first, y = q.front().second;
		q.pop();
		for(ll i = 0; i < 4; i++){
			if(valid(x + dx[i], y + dy[i]) && !color[x + dx[i]][y + dy[i]] &&
			(dis[x][y] + 1 < dis_b[x + dx[i]][y + dy[i]] * s || grid[x + dx[i]][y + dy[i]] == 'D') && 
			(grid[x + dx[i]][y + dy[i]] == 'G' || grid[x + dx[i]][y + dy[i]] == 'D')){
				dis[x + dx[i]][y + dy[i]] = dis[x][y] + 1;
				color[x + dx[i]][y + dy[i]] = 1;
				q.push({x + dx[i], y + dy[i]});
			}
		} 
	}
	return color[finish.first][finish.second];
}

int main() {
	ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
	cin>>n>>s;
	if(n == 1){
		cout<<-1;
		return 0;
	}
	queue<pair<ll, ll>> b, m;
	for(ll i = 0; i < n; i++){
		for(ll j = 0; j < n; j++){
			cin>>grid[i][j];
			if(grid[i][j] == 'H'){
				b.push({i, j});
				color_b[i][j] = 1;
				dis_b[i][j] = 0;
			}else if(grid[i][j] == 'M'){
				m.push({i, j});
			}else if(grid[i][j] == 'D'){
				finish = {i, j};
			}
		}
	}
	bfs_b(b);
	if(!bfs_m(m.front().first, m.front().second, 0)){
		cout<<-1;
		return 0;
	}
	ll l = 0, r = 2e9;
	ll ans = 0;
	while(l <= r){
		ll mid = (l + r) / 2;
		if(bfs_m(m.front().first, m.front().second, s * mid)){
			l = mid + 1;
			ans = max(ans, mid);
		}else r = mid - 1;
	}
	cout<<ans;
}
 
# Verdict Execution time Memory Grader output
1 Correct 35 ms 10404 KB Output is correct
2 Correct 36 ms 10316 KB Output is correct
3 Correct 32 ms 10324 KB Output is correct
4 Correct 30 ms 10324 KB Output is correct
5 Correct 34 ms 10432 KB Output is correct
6 Correct 32 ms 10452 KB Output is correct
7 Correct 140 ms 21244 KB Output is correct
8 Correct 4 ms 10452 KB Output is correct
9 Correct 30 ms 10452 KB Output is correct
10 Correct 32 ms 10500 KB Output is correct
11 Correct 37 ms 10452 KB Output is correct
12 Correct 34 ms 10952 KB Output is correct
13 Correct 31 ms 10836 KB Output is correct
14 Correct 4 ms 10836 KB Output is correct
15 Correct 36 ms 10556 KB Output is correct
16 Correct 39 ms 10452 KB Output is correct
17 Correct 32 ms 10580 KB Output is correct
18 Correct 33 ms 10588 KB Output is correct
19 Correct 36 ms 10580 KB Output is correct
20 Correct 34 ms 10580 KB Output is correct
21 Correct 39 ms 10708 KB Output is correct
22 Correct 40 ms 10708 KB Output is correct
23 Correct 33 ms 10836 KB Output is correct
24 Correct 34 ms 10836 KB Output is correct
25 Correct 32 ms 10916 KB Output is correct
26 Correct 32 ms 10936 KB Output is correct
27 Correct 31 ms 10996 KB Output is correct
28 Correct 34 ms 10964 KB Output is correct
29 Correct 40 ms 10964 KB Output is correct
30 Correct 46 ms 10964 KB Output is correct
31 Correct 41 ms 11072 KB Output is correct
32 Correct 34 ms 11104 KB Output is correct
33 Correct 44 ms 14164 KB Output is correct
34 Correct 39 ms 14676 KB Output is correct
35 Correct 61 ms 14932 KB Output is correct
36 Correct 49 ms 14804 KB Output is correct
37 Correct 45 ms 15432 KB Output is correct
38 Correct 69 ms 15700 KB Output is correct
39 Correct 40 ms 15352 KB Output is correct
40 Correct 45 ms 16188 KB Output is correct
41 Correct 81 ms 16372 KB Output is correct
42 Correct 56 ms 15908 KB Output is correct
43 Correct 47 ms 16852 KB Output is correct
44 Correct 101 ms 16980 KB Output is correct
45 Correct 47 ms 16456 KB Output is correct
46 Correct 54 ms 17568 KB Output is correct
47 Correct 122 ms 17620 KB Output is correct
48 Correct 51 ms 16992 KB Output is correct
49 Correct 61 ms 18156 KB Output is correct
50 Correct 136 ms 18376 KB Output is correct
51 Correct 55 ms 17528 KB Output is correct
52 Correct 74 ms 18572 KB Output is correct
53 Correct 188 ms 19004 KB Output is correct
54 Correct 58 ms 18036 KB Output is correct
55 Correct 60 ms 19168 KB Output is correct
56 Correct 178 ms 19692 KB Output is correct
57 Correct 51 ms 18644 KB Output is correct
58 Correct 65 ms 19668 KB Output is correct
59 Correct 203 ms 20308 KB Output is correct
60 Correct 56 ms 19156 KB Output is correct
61 Correct 57 ms 20044 KB Output is correct
62 Correct 211 ms 21036 KB Output is correct
63 Correct 181 ms 21032 KB Output is correct
64 Correct 268 ms 21024 KB Output is correct
65 Correct 241 ms 21028 KB Output is correct
66 Correct 213 ms 21024 KB Output is correct
67 Correct 42 ms 20908 KB Output is correct
68 Correct 92 ms 21000 KB Output is correct
69 Correct 91 ms 21080 KB Output is correct
70 Correct 81 ms 21088 KB Output is correct
71 Correct 82 ms 21020 KB Output is correct
72 Correct 73 ms 21036 KB Output is correct
73 Correct 87 ms 21552 KB Output is correct
74 Correct 124 ms 21440 KB Output is correct
75 Correct 111 ms 21556 KB Output is correct
76 Correct 102 ms 21512 KB Output is correct
77 Correct 109 ms 21508 KB Output is correct
78 Correct 42 ms 21452 KB Output is correct
79 Correct 130 ms 21620 KB Output is correct
80 Correct 110 ms 21488 KB Output is correct
81 Correct 110 ms 21384 KB Output is correct
82 Correct 117 ms 21500 KB Output is correct
83 Correct 140 ms 21512 KB Output is correct
84 Correct 132 ms 21412 KB Output is correct
85 Correct 125 ms 21412 KB Output is correct
86 Correct 140 ms 21452 KB Output is correct
87 Correct 134 ms 21320 KB Output is correct
88 Correct 139 ms 21212 KB Output is correct
89 Correct 150 ms 21320 KB Output is correct
90 Correct 151 ms 21324 KB Output is correct
91 Correct 155 ms 21316 KB Output is correct
92 Correct 140 ms 21196 KB Output is correct