Submission #126532

# Submission time Handle Problem Language Result Execution time Memory
126532 2019-07-08T03:25:28 Z khulegub Mecho (IOI09_mecho) C++14
100 / 100
150 ms 4600 KB
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define xx first
#define yy second
using namespace std;
typedef long long i64;
typedef pair<int,int> pii;
int n;
int s;
char gazar[810][810];
bool vis[810][810];
int beed[810][810];
int mei, mej;
bool feasible (int x){
	memset(vis, 0, sizeof vis);
	queue<pii> q;
	int depth = x * s;
	q.push(mp(mei, mej));
	vis[mei][mej] = 1;
	while(!q.empty()){
		int sz = q.size();
		for (int cnt = 0; cnt < sz; cnt++){
			pii nw = q.front();
			if (gazar[nw.xx][nw.yy] == 'D') return 1;
			q.pop();
			if (nw.xx < n && !vis[nw.xx + 1][nw.yy] && (gazar[nw.xx + 1][nw.yy] == 'D' || (gazar[nw.xx + 1][nw.yy] == 'G' && ((depth+1) / s) < beed[nw.xx + 1][nw.yy]) ) ) q.push(mp(nw.xx + 1, nw.yy)), vis[nw.xx + 1][nw.yy] = 1;
			if (nw.xx > 1 && !vis[nw.xx - 1][nw.yy] && (gazar[nw.xx - 1][nw.yy] == 'D' || (gazar[nw.xx - 1][nw.yy] == 'G' && ((depth+1) / s) < beed[nw.xx - 1][nw.yy]) ) ) q.push(mp(nw.xx - 1, nw.yy)), vis[nw.xx - 1][nw.yy] = 1;
			if (nw.yy < n && !vis[nw.xx][nw.yy + 1] && (gazar[nw.xx][nw.yy + 1] == 'D' || (gazar[nw.xx][nw.yy + 1] == 'G' && ((depth+1) / s) < beed[nw.xx][nw.yy + 1]) ) ) q.push(mp(nw.xx, nw.yy + 1)), vis[nw.xx][nw.yy + 1] = 1;
			if (nw.yy > 1 && !vis[nw.xx][nw.yy - 1] && (gazar[nw.xx][nw.yy - 1] == 'D' || (gazar[nw.xx][nw.yy - 1] == 'G' && ((depth+1) / s) < beed[nw.xx][nw.yy - 1]) ) ) q.push(mp(nw.xx, nw.yy - 1)), vis[nw.xx][nw.yy - 1] = 1;
		}
		depth ++;
	}
	return 0;
}
int main (){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	// freopen("in.txt", "r", stdin);
	cin >> n;
	cin >> s;
	for (int i = 1; i <= n; i++){
		string tmp;
		cin >> tmp;
		// cout << tmp;
		for (int j = 1; j <= n; j++){
			gazar[i][j] = tmp[j - 1];
			if (tmp[j - 1] == 'M') mei = i, mej = j;
		}
	}

	queue<pii> q;
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= n; j++){
			if (gazar[i][j] == 'H') q.push( mp(i, j) ), vis[i][j] = 1;
		}
	}
	int depth = 0;
	// cout << "QQQ";
	while (!q.empty()){
		int sz = q.size();
		// cout << sz << ' ';
		for (int cnt = 0; cnt < sz; cnt++){
			pii nw = q.front();
			q.pop();
			beed[nw.xx][nw.yy] = depth;
			if (nw.xx < n && !vis[nw.xx + 1][nw.yy] && (gazar[nw.xx + 1][nw.yy] == 'M' || gazar[nw.xx + 1][nw.yy] == 'G') ) q.push(mp(nw.xx + 1, nw.yy)), vis[nw.xx + 1][nw.yy] = 1;
			if (nw.xx > 1 && !vis[nw.xx - 1][nw.yy] && (gazar[nw.xx - 1][nw.yy] == 'M' || gazar[nw.xx - 1][nw.yy] == 'G') ) q.push(mp(nw.xx - 1, nw.yy)), vis[nw.xx - 1][nw.yy] = 1;
			if (nw.yy < n && !vis[nw.xx][nw.yy + 1] && (gazar[nw.xx][nw.yy + 1] == 'M' || gazar[nw.xx][nw.yy + 1] == 'G') ) q.push(mp(nw.xx, nw.yy + 1)), vis[nw.xx][nw.yy + 1] = 1;
			if (nw.yy > 1 && !vis[nw.xx][nw.yy - 1] && (gazar[nw.xx][nw.yy - 1] == 'M' || gazar[nw.xx][nw.yy - 1] == 'G') ) q.push(mp(nw.xx, nw.yy - 1)), vis[nw.xx][nw.yy - 1] = 1;
		}
		depth++;
	}
	int lo = 0, hi = beed[mei][mej] - 1;
	while (lo != hi){
		int mid = (lo + hi + 1) >> 1;
		if (feasible(mid)) lo = mid;
		else hi = mid - 1;
	}
	if(feasible(lo)) cout << lo;
	else cout << -1;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1016 KB Output is correct
2 Correct 2 ms 1016 KB Output is correct
3 Correct 2 ms 1016 KB Output is correct
4 Correct 3 ms 1016 KB Output is correct
5 Correct 2 ms 1016 KB Output is correct
6 Correct 3 ms 1016 KB Output is correct
7 Correct 72 ms 4472 KB Output is correct
8 Correct 3 ms 1016 KB Output is correct
9 Correct 2 ms 1020 KB Output is correct
10 Correct 2 ms 1016 KB Output is correct
11 Correct 3 ms 1016 KB Output is correct
12 Correct 3 ms 1272 KB Output is correct
13 Correct 3 ms 1144 KB Output is correct
14 Correct 3 ms 1148 KB Output is correct
15 Correct 3 ms 1016 KB Output is correct
16 Correct 3 ms 1144 KB Output is correct
17 Correct 3 ms 1016 KB Output is correct
18 Correct 3 ms 1144 KB Output is correct
19 Correct 3 ms 1144 KB Output is correct
20 Correct 3 ms 1144 KB Output is correct
21 Correct 3 ms 1144 KB Output is correct
22 Correct 3 ms 1144 KB Output is correct
23 Correct 3 ms 1144 KB Output is correct
24 Correct 3 ms 1144 KB Output is correct
25 Correct 3 ms 1272 KB Output is correct
26 Correct 3 ms 1272 KB Output is correct
27 Correct 3 ms 1272 KB Output is correct
28 Correct 3 ms 1272 KB Output is correct
29 Correct 3 ms 1272 KB Output is correct
30 Correct 3 ms 1272 KB Output is correct
31 Correct 3 ms 1272 KB Output is correct
32 Correct 3 ms 1272 KB Output is correct
33 Correct 6 ms 2424 KB Output is correct
34 Correct 6 ms 2424 KB Output is correct
35 Correct 20 ms 2424 KB Output is correct
36 Correct 7 ms 2552 KB Output is correct
37 Correct 6 ms 2552 KB Output is correct
38 Correct 23 ms 2552 KB Output is correct
39 Correct 8 ms 2808 KB Output is correct
40 Correct 7 ms 2808 KB Output is correct
41 Correct 31 ms 2936 KB Output is correct
42 Correct 8 ms 2936 KB Output is correct
43 Correct 8 ms 2936 KB Output is correct
44 Correct 38 ms 3064 KB Output is correct
45 Correct 10 ms 3192 KB Output is correct
46 Correct 9 ms 3196 KB Output is correct
47 Correct 43 ms 3192 KB Output is correct
48 Correct 10 ms 3448 KB Output is correct
49 Correct 10 ms 3320 KB Output is correct
50 Correct 51 ms 3460 KB Output is correct
51 Correct 13 ms 3704 KB Output is correct
52 Correct 11 ms 3576 KB Output is correct
53 Correct 60 ms 3576 KB Output is correct
54 Correct 12 ms 3704 KB Output is correct
55 Correct 12 ms 3832 KB Output is correct
56 Correct 70 ms 3704 KB Output is correct
57 Correct 13 ms 3960 KB Output is correct
58 Correct 13 ms 3960 KB Output is correct
59 Correct 80 ms 3960 KB Output is correct
60 Correct 17 ms 4088 KB Output is correct
61 Correct 15 ms 4216 KB Output is correct
62 Correct 102 ms 4184 KB Output is correct
63 Correct 90 ms 4088 KB Output is correct
64 Correct 149 ms 4208 KB Output is correct
65 Correct 150 ms 4088 KB Output is correct
66 Correct 130 ms 4208 KB Output is correct
67 Correct 108 ms 4088 KB Output is correct
68 Correct 45 ms 4216 KB Output is correct
69 Correct 36 ms 4344 KB Output is correct
70 Correct 36 ms 4236 KB Output is correct
71 Correct 34 ms 4216 KB Output is correct
72 Correct 28 ms 4236 KB Output is correct
73 Correct 32 ms 4472 KB Output is correct
74 Correct 52 ms 4472 KB Output is correct
75 Correct 55 ms 4472 KB Output is correct
76 Correct 55 ms 4600 KB Output is correct
77 Correct 54 ms 4484 KB Output is correct
78 Correct 62 ms 4572 KB Output is correct
79 Correct 46 ms 4344 KB Output is correct
80 Correct 50 ms 4344 KB Output is correct
81 Correct 59 ms 4444 KB Output is correct
82 Correct 52 ms 4344 KB Output is correct
83 Correct 72 ms 4416 KB Output is correct
84 Correct 61 ms 4472 KB Output is correct
85 Correct 58 ms 4412 KB Output is correct
86 Correct 65 ms 4344 KB Output is correct
87 Correct 59 ms 4448 KB Output is correct
88 Correct 65 ms 4344 KB Output is correct
89 Correct 71 ms 4344 KB Output is correct
90 Correct 76 ms 4476 KB Output is correct
91 Correct 78 ms 4344 KB Output is correct
92 Correct 76 ms 4344 KB Output is correct