Submission #958047

# Submission time Handle Problem Language Result Execution time Memory
958047 2024-04-04T18:13:44 Z theehann Mecho (IOI09_mecho) C++17
69 / 100
150 ms 11712 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' || a[u][v] == 'M') && 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 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Incorrect 1 ms 2396 KB Output isn't correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 76 ms 11348 KB Output is correct
8 Correct 1 ms 2648 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Incorrect 1 ms 3160 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 2648 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 2652 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2648 KB Output is correct
23 Correct 1 ms 2920 KB Output is correct
24 Correct 1 ms 2908 KB Output is correct
25 Correct 1 ms 2908 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 1 ms 3164 KB Output is correct
30 Correct 2 ms 3164 KB Output is correct
31 Correct 1 ms 3160 KB Output is correct
32 Correct 1 ms 3164 KB Output is correct
33 Correct 7 ms 8028 KB Output is correct
34 Correct 6 ms 8028 KB Output is correct
35 Correct 19 ms 8028 KB Output is correct
36 Correct 8 ms 8284 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 8540 KB Output is correct
41 Correct 30 ms 8720 KB Output is correct
42 Correct 12 ms 9052 KB Output is correct
43 Correct 11 ms 9052 KB Output is correct
44 Correct 39 ms 9052 KB Output is correct
45 Correct 19 ms 9692 KB Output is correct
46 Correct 18 ms 9308 KB Output is correct
47 Correct 62 ms 9436 KB Output is correct
48 Correct 16 ms 9560 KB Output is correct
49 Correct 16 ms 9560 KB Output is correct
50 Correct 60 ms 9820 KB Output is correct
51 Correct 19 ms 10076 KB Output is correct
52 Correct 20 ms 10076 KB Output is correct
53 Correct 94 ms 10152 KB Output is correct
54 Correct 20 ms 10332 KB Output is correct
55 Correct 20 ms 10332 KB Output is correct
56 Correct 81 ms 10332 KB Output is correct
57 Correct 22 ms 10844 KB Output is correct
58 Correct 23 ms 10844 KB Output is correct
59 Correct 121 ms 10808 KB Output is correct
60 Correct 26 ms 11356 KB Output is correct
61 Correct 26 ms 11224 KB Output is correct
62 Correct 108 ms 11220 KB Output is correct
63 Incorrect 88 ms 11100 KB Output isn't correct
64 Correct 150 ms 11460 KB Output is correct
65 Correct 145 ms 11216 KB Output is correct
66 Correct 107 ms 11100 KB Output is correct
67 Correct 94 ms 11204 KB Output is correct
68 Incorrect 57 ms 11092 KB Output isn't correct
69 Correct 49 ms 11084 KB Output is correct
70 Correct 44 ms 11096 KB Output is correct
71 Correct 37 ms 11100 KB Output is correct
72 Incorrect 37 ms 11100 KB Output isn't correct
73 Incorrect 48 ms 11592 KB Output isn't correct
74 Correct 56 ms 11712 KB Output is correct
75 Correct 64 ms 11612 KB Output is correct
76 Correct 55 ms 11612 KB Output is correct
77 Correct 56 ms 11560 KB Output is correct
78 Correct 62 ms 11612 KB Output is correct
79 Correct 53 ms 11608 KB Output is correct
80 Correct 63 ms 11612 KB Output is correct
81 Correct 63 ms 11612 KB Output is correct
82 Correct 52 ms 11612 KB Output is correct
83 Incorrect 75 ms 11612 KB Output isn't correct
84 Correct 58 ms 11612 KB Output is correct
85 Correct 62 ms 11608 KB Output is correct
86 Correct 75 ms 11620 KB Output is correct
87 Correct 83 ms 11620 KB Output is correct
88 Incorrect 63 ms 11528 KB Output isn't correct
89 Correct 82 ms 11352 KB Output is correct
90 Correct 70 ms 11356 KB Output is correct
91 Correct 67 ms 11344 KB Output is correct
92 Correct 71 ms 11356 KB Output is correct