Submission #958043

# Submission time Handle Problem Language Result Execution time Memory
958043 2024-04-04T18:08:42 Z theehann Mecho (IOI09_mecho) C++17
69 / 100
155 ms 11964 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define ft first
#define se second
#define NAME "A"
#define file freopen(NAME".INP","r",stdin); freopen(NAME".OUT","w",stdout);
#define sdf ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const int MOD = 1e9 + 7;
const int N = 800 + 5, INF = 1e15;

int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
char a[N][N];
int dis[N][N], vis[N][N] = {INF};
bool f = false;
int n, step, homex, homey, mx, my;

bool bfs(int len, int x, int y){
	vis[x][y] = len;
	queue<tuple<int, int, int>> q;
	q.push({x, y, 0});
	while(!q.empty()){
		const auto [sx, sy, cur] = q.front(); q.pop();
		int dist = (cur + 1) / step;

		for(int i = 0;i < 4;++i){
			int u = sx + dx[i], v = sy + dy[i];
			if(u < 1 || v < 1 || v > n || u > n) continue;
			if(a[u][v] == 'G' && (cur + 1) < vis[u][v] && dist < dis[u][v] - (len / step)){
				vis[u][v] = cur + 1;
				q.push({u, v, cur + 1});
			}
		}
	}

	for(int i = 0;i < 4;++i){
		int u = homex + dx[i], v = homey + dy[i];
		if(vis[u][v] != INF && a[u][v] == 'G' && vis[u][v] / step < dis[u][v] - (len / step)){
			return true;
		}
	}
	return false;
}

int32_t main(){
	sdf

	cin >> n >> step;
	queue<pair<int, int>> q;
	for(int i = 1;i <= n;++i){
		for(int j = 1;j <= n;++j){
			cin >> a[i][j];
			dis[i][j] = INF;
			if(a[i][j] == 'H'){
				q.push({i, j});
				dis[i][j] = 0;
			}
			else if(a[i][j] == 'D'){
				homex = i; homey = j;
			}
			else if(a[i][j] == 'M'){
				mx = i; my = j;
			}
		}
	}

	while(!q.empty()){
		const auto [x, y] = q.front(); q.pop();

		for(int i  = 0;i < 4;++i){
			int u = x + dx[i];
			int v = y + dy[i];
			if(u < 1 || v < 1 || u > n || v > n) continue;
			if(dis[u][v] == INF && (a[u][v] == 'G' || a[u][v] == 'M')){
				dis[u][v] = dis[x][y] + 1;
				q.push({u, v});
			}
		}
	}

	int l = 1, r = INF;
	int ans =0;
	while(l <= r){
		int mid = (l + r) / 2;
		for(int i = 1;i <= n;++i){
			for(int j = 1;j <= n;++j){
				vis[i][j] = INF;
			}
		}
		if(bfs(mid * step, mx, my)){
			l = mid + 1;
			ans = mid;
		}
		else{
			r = mid - 1;
		}
	}
	cout << ((ans >= 1) ? ans : -1) ;
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Incorrect 1 ms 2392 KB Output isn't correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 74 ms 11424 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Incorrect 1 ms 2904 KB Output isn't correct
13 Incorrect 1 ms 2908 KB Output isn't correct
14 Correct 1 ms 2908 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2904 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2904 KB Output is correct
23 Correct 1 ms 2904 KB Output is correct
24 Correct 1 ms 2904 KB Output is correct
25 Correct 1 ms 2904 KB Output is correct
26 Correct 1 ms 2908 KB Output is correct
27 Correct 1 ms 2908 KB Output is correct
28 Correct 1 ms 2908 KB Output is correct
29 Correct 2 ms 3164 KB Output is correct
30 Correct 1 ms 3164 KB Output is correct
31 Correct 1 ms 3164 KB Output is correct
32 Correct 1 ms 3164 KB Output is correct
33 Correct 6 ms 8028 KB Output is correct
34 Correct 7 ms 8028 KB Output is correct
35 Correct 20 ms 8024 KB Output is correct
36 Correct 11 ms 8280 KB Output is correct
37 Correct 8 ms 8284 KB Output is correct
38 Correct 25 ms 8404 KB Output is correct
39 Correct 10 ms 8540 KB Output is correct
40 Correct 10 ms 8536 KB Output is correct
41 Correct 32 ms 8752 KB Output is correct
42 Correct 11 ms 9048 KB Output is correct
43 Correct 11 ms 9052 KB Output is correct
44 Correct 40 ms 9052 KB Output is correct
45 Correct 13 ms 9308 KB Output is correct
46 Correct 14 ms 9388 KB Output is correct
47 Correct 49 ms 9308 KB Output is correct
48 Correct 15 ms 9564 KB Output is correct
49 Correct 16 ms 9676 KB Output is correct
50 Correct 54 ms 9816 KB Output is correct
51 Correct 18 ms 10076 KB Output is correct
52 Correct 20 ms 10164 KB Output is correct
53 Correct 70 ms 10076 KB Output is correct
54 Correct 19 ms 10328 KB Output is correct
55 Correct 20 ms 10524 KB Output is correct
56 Correct 78 ms 10520 KB Output is correct
57 Correct 22 ms 10844 KB Output is correct
58 Correct 23 ms 10844 KB Output is correct
59 Correct 86 ms 10868 KB Output is correct
60 Correct 26 ms 11200 KB Output is correct
61 Correct 26 ms 11100 KB Output is correct
62 Correct 97 ms 11220 KB Output is correct
63 Incorrect 84 ms 11204 KB Output isn't correct
64 Correct 155 ms 11204 KB Output is correct
65 Correct 154 ms 11600 KB Output is correct
66 Correct 112 ms 11212 KB Output is correct
67 Correct 101 ms 11208 KB Output is correct
68 Incorrect 50 ms 11264 KB Output isn't correct
69 Correct 53 ms 11092 KB Output is correct
70 Correct 45 ms 11100 KB Output is correct
71 Correct 47 ms 11100 KB Output is correct
72 Incorrect 38 ms 11100 KB Output isn't correct
73 Incorrect 41 ms 11608 KB Output isn't correct
74 Correct 57 ms 11560 KB Output is correct
75 Correct 76 ms 11764 KB Output is correct
76 Correct 58 ms 11756 KB Output is correct
77 Correct 58 ms 11556 KB Output is correct
78 Correct 60 ms 11608 KB Output is correct
79 Correct 52 ms 11612 KB Output is correct
80 Correct 56 ms 11612 KB Output is correct
81 Correct 60 ms 11608 KB Output is correct
82 Correct 52 ms 11700 KB Output is correct
83 Incorrect 64 ms 11620 KB Output isn't correct
84 Correct 72 ms 11616 KB Output is correct
85 Correct 75 ms 11964 KB Output is correct
86 Correct 67 ms 11604 KB Output is correct
87 Correct 66 ms 11612 KB Output is correct
88 Incorrect 71 ms 11352 KB Output isn't correct
89 Correct 70 ms 11352 KB Output is correct
90 Correct 74 ms 11524 KB Output is correct
91 Correct 65 ms 11356 KB Output is correct
92 Correct 71 ms 11368 KB Output is correct