Submission #958039

# Submission time Handle Problem Language Result Execution time Memory
958039 2024-04-04T18:02:56 Z theehann Mecho (IOI09_mecho) C++17
69 / 100
144 ms 11708 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'){
				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 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 2392 KB Output isn't correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 71 ms 11356 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 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Incorrect 1 ms 2908 KB Output isn't correct
13 Incorrect 1 ms 2908 KB Output isn't correct
14 Correct 1 ms 2904 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 2652 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2908 KB Output is correct
23 Correct 2 ms 2904 KB Output is correct
24 Correct 1 ms 2904 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 2904 KB Output is correct
28 Correct 1 ms 2904 KB Output is correct
29 Correct 1 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 8 ms 8028 KB Output is correct
34 Correct 7 ms 8024 KB Output is correct
35 Correct 18 ms 8028 KB Output is correct
36 Correct 8 ms 8284 KB Output is correct
37 Correct 9 ms 8284 KB Output is correct
38 Correct 24 ms 8392 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 8540 KB Output is correct
42 Correct 11 ms 9052 KB Output is correct
43 Correct 11 ms 9052 KB Output is correct
44 Correct 38 ms 9048 KB Output is correct
45 Correct 13 ms 9308 KB Output is correct
46 Correct 14 ms 9308 KB Output is correct
47 Correct 45 ms 9560 KB Output is correct
48 Correct 16 ms 9720 KB Output is correct
49 Correct 16 ms 9560 KB Output is correct
50 Correct 65 ms 9820 KB Output is correct
51 Correct 19 ms 10072 KB Output is correct
52 Correct 19 ms 10156 KB Output is correct
53 Correct 66 ms 10328 KB Output is correct
54 Correct 20 ms 10328 KB Output is correct
55 Correct 19 ms 10328 KB Output is correct
56 Correct 80 ms 10328 KB Output is correct
57 Correct 22 ms 10844 KB Output is correct
58 Correct 23 ms 10844 KB Output is correct
59 Correct 87 ms 10844 KB Output is correct
60 Correct 26 ms 11096 KB Output is correct
61 Correct 26 ms 11100 KB Output is correct
62 Correct 96 ms 11100 KB Output is correct
63 Incorrect 82 ms 11088 KB Output isn't correct
64 Correct 144 ms 11200 KB Output is correct
65 Correct 140 ms 11088 KB Output is correct
66 Correct 106 ms 11096 KB Output is correct
67 Correct 94 ms 11096 KB Output is correct
68 Incorrect 52 ms 11264 KB Output isn't correct
69 Correct 48 ms 11100 KB Output is correct
70 Correct 44 ms 11096 KB Output is correct
71 Correct 38 ms 11092 KB Output is correct
72 Incorrect 44 ms 11084 KB Output isn't correct
73 Incorrect 60 ms 11608 KB Output isn't correct
74 Correct 55 ms 11708 KB Output is correct
75 Correct 59 ms 11600 KB Output is correct
76 Correct 56 ms 11592 KB Output is correct
77 Correct 55 ms 11548 KB Output is correct
78 Correct 60 ms 11696 KB Output is correct
79 Correct 52 ms 11608 KB Output is correct
80 Correct 54 ms 11612 KB Output is correct
81 Correct 62 ms 11476 KB Output is correct
82 Correct 52 ms 11696 KB Output is correct
83 Incorrect 76 ms 11604 KB Output isn't correct
84 Correct 58 ms 11600 KB Output is correct
85 Correct 65 ms 11608 KB Output is correct
86 Correct 68 ms 11620 KB Output is correct
87 Correct 65 ms 11600 KB Output is correct
88 Incorrect 77 ms 11352 KB Output isn't correct
89 Correct 79 ms 11356 KB Output is correct
90 Correct 86 ms 11356 KB Output is correct
91 Correct 66 ms 11624 KB Output is correct
92 Correct 77 ms 11344 KB Output is correct