Submission #958034

# Submission time Handle Problem Language Result Execution time Memory
958034 2024-04-04T17:51:53 Z theehann Mecho (IOI09_mecho) C++17
69 / 100
164 ms 11780 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] = {0};
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, len});
	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]){
				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'){
			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 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Incorrect 1 ms 4696 KB Output isn't correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 67 ms 11444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Incorrect 2 ms 6744 KB Output isn't correct
13 Incorrect 2 ms 6748 KB Output isn't correct
14 Correct 2 ms 6748 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 2 ms 4440 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 6748 KB Output is correct
20 Correct 1 ms 6744 KB Output is correct
21 Correct 1 ms 6744 KB Output is correct
22 Correct 1 ms 6748 KB Output is correct
23 Correct 1 ms 6748 KB Output is correct
24 Correct 1 ms 6748 KB Output is correct
25 Correct 1 ms 6748 KB Output is correct
26 Correct 1 ms 6744 KB Output is correct
27 Correct 1 ms 6748 KB Output is correct
28 Correct 2 ms 6904 KB Output is correct
29 Correct 1 ms 6744 KB Output is correct
30 Correct 2 ms 6748 KB Output is correct
31 Correct 1 ms 6748 KB Output is correct
32 Correct 1 ms 6748 KB Output is correct
33 Correct 5 ms 8796 KB Output is correct
34 Correct 5 ms 9048 KB Output is correct
35 Correct 17 ms 8860 KB Output is correct
36 Correct 7 ms 10844 KB Output is correct
37 Correct 8 ms 11040 KB Output is correct
38 Correct 22 ms 10844 KB Output is correct
39 Correct 9 ms 10844 KB Output is correct
40 Correct 9 ms 10972 KB Output is correct
41 Correct 35 ms 11224 KB Output is correct
42 Correct 10 ms 10844 KB Output is correct
43 Correct 17 ms 10844 KB Output is correct
44 Correct 35 ms 10840 KB Output is correct
45 Correct 12 ms 10844 KB Output is correct
46 Correct 12 ms 10844 KB Output is correct
47 Correct 43 ms 11056 KB Output is correct
48 Correct 14 ms 10844 KB Output is correct
49 Correct 14 ms 10840 KB Output is correct
50 Correct 53 ms 11100 KB Output is correct
51 Correct 17 ms 11100 KB Output is correct
52 Correct 17 ms 10976 KB Output is correct
53 Correct 60 ms 11124 KB Output is correct
54 Correct 18 ms 11100 KB Output is correct
55 Correct 18 ms 11168 KB Output is correct
56 Correct 68 ms 11172 KB Output is correct
57 Correct 21 ms 11096 KB Output is correct
58 Correct 21 ms 11096 KB Output is correct
59 Correct 115 ms 11200 KB Output is correct
60 Correct 24 ms 11100 KB Output is correct
61 Correct 25 ms 11100 KB Output is correct
62 Correct 92 ms 11096 KB Output is correct
63 Incorrect 79 ms 11228 KB Output isn't correct
64 Correct 143 ms 11228 KB Output is correct
65 Correct 164 ms 11052 KB Output is correct
66 Correct 106 ms 11224 KB Output is correct
67 Correct 101 ms 11228 KB Output is correct
68 Incorrect 43 ms 11088 KB Output isn't correct
69 Correct 49 ms 11100 KB Output is correct
70 Correct 42 ms 11176 KB Output is correct
71 Correct 35 ms 11096 KB Output is correct
72 Incorrect 48 ms 11352 KB Output isn't correct
73 Incorrect 57 ms 11612 KB Output isn't correct
74 Correct 59 ms 11556 KB Output is correct
75 Correct 60 ms 11776 KB Output is correct
76 Correct 68 ms 11780 KB Output is correct
77 Correct 57 ms 11724 KB Output is correct
78 Correct 59 ms 11604 KB Output is correct
79 Correct 48 ms 11612 KB Output is correct
80 Correct 65 ms 11608 KB Output is correct
81 Correct 57 ms 11612 KB Output is correct
82 Correct 50 ms 11548 KB Output is correct
83 Incorrect 63 ms 11648 KB Output isn't correct
84 Correct 56 ms 11608 KB Output is correct
85 Correct 58 ms 11612 KB Output is correct
86 Correct 81 ms 11612 KB Output is correct
87 Correct 65 ms 11636 KB Output is correct
88 Incorrect 93 ms 11352 KB Output isn't correct
89 Correct 73 ms 11608 KB Output is correct
90 Correct 70 ms 11356 KB Output is correct
91 Correct 65 ms 11360 KB Output is correct
92 Correct 89 ms 11544 KB Output is correct