Submission #103689

# Submission time Handle Problem Language Result Execution time Memory
103689 2019-04-02T04:32:45 Z turbat Mecho (IOI09_mecho) C++14
0 / 100
1000 ms 3560 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
using pii = pair<int, int>;
using vi = vector <int>;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define ll long long
#define ook order_of_key
#define fbo find_by_order
#define sq(x) (x) * (x)
#define N 805

int x, y, posx, posy, n, s;
int ax[4] = {0, 0, 1, -1};
int ay[4] = {1, -1, 0, 0}; 
string st[N];
queue <pii> que, q, bfs, mecho;
bool used[N][N], vis[N][N], pass[N][N];

bool check(int curx, int cury){
	if (curx < n && cury < n && curx >= 0 && cury >= 0 && !used[curx][cury]) return true;
	return false;
}

bool can(int time){;
	que = q;
	mecho.push(mp(x, y));
	memset(pass, 0, sizeof pass);
	pass[x][y] = 1;
	for (int i = 0;i < n;i++)
		for (int j = 0;j < n;j++)
			used[i][j] = vis[i][j];
	while (time--){
		bfs = que;
		while(!que.empty()) que.pop();
		while (!bfs.empty()){
			pii pos = bfs.front();
			bfs.pop();
			for (int i = 0;i < 4;i++)
				if (check(pos.F + ax[i], pos.S + ay[i]) && st[pos.F + ax[i]][pos.S + ay[i]] != 'T'
					&& st[pos.F + ax[i]][pos.S + ay[i]] != 'D')
					que.push(mp(pos.F + ax[i], pos.S + ay[i])), used[pos.F + ax[i]][pos.S + ay[i]] = 1;
		} 
	}
	while (!used[posx][posy]){
		int tmp = s;
		while (tmp--){
			bfs = mecho;
			while (!mecho.empty()) mecho.pop();
			while (!bfs.empty()){
				pii pos = bfs.front();
				bfs.pop();
				if (pos.F == posx && pos.S == posy) return true;			
				if (!used[pos.F][pos.S])
					for (int i = 0;i < 4;i++)
						if (check(pos.F + ax[i], pos.S + ay[i]) && st[pos.F + ax[i]][pos.S + ay[i]] != 'T'
							&& !pass[pos.F + ax[i]][pos.S + ay[i]])
							mecho.push(mp(pos.F + ax[i], pos.S + ay[i])), pass[pos.F + ax[i]][pos.S + ay[i]] = 1;
			}
		}
		bfs = que;
		while (!que.empty()) que.pop();
		while (!bfs.empty()){
			pii pos = bfs.front();
			bfs.pop();
			for (int i = 0;i < 4;i++)
				if (check(pos.F + ax[i], pos.S + ay[i]) && st[pos.F + ax[i]][pos.S + ay[i]] != 'T'
					&& st[pos.F + ax[i]][pos.S + ay[i]] != 'D')
					que.push(mp(pos.F + ax[i], pos.S + ay[i])), used[pos.F + ax[i]][pos.S + ay[i]] = 1;
		} 
	}
	return false;
}

int main (){
	cin >> n>> s;
	for (int i = 0;i < n;i++)
		cin >> st[i];
	for (int i = 0;i < n;i++)
		for (int j = 0;j < n;j++){
			if (st[i][j] == 'M') x = i, y = j;
			if (st[i][j] == 'D') posx = i, posy = j;
			if (st[i][j] == 'H') q.push(mp(i, j)), vis[i][j] = 1;
		}
	int l = 0, r = n * n;
	while (l != r){
		int mid = (l + r + 1) / 2;
		if (can(mid)) l = mid;
		else r = mid - 1;
	}
	if (can(l)) cout << l<< endl;
	else cout << -1 << endl;
}
# Verdict Execution time Memory Grader output
1 Execution timed out 1067 ms 1024 KB Time limit exceeded
2 Execution timed out 1069 ms 1024 KB Time limit exceeded
3 Execution timed out 1085 ms 1024 KB Time limit exceeded
4 Execution timed out 1072 ms 1024 KB Time limit exceeded
5 Execution timed out 1066 ms 1024 KB Time limit exceeded
6 Execution timed out 1081 ms 1024 KB Time limit exceeded
7 Execution timed out 1063 ms 2908 KB Time limit exceeded
8 Execution timed out 1060 ms 1024 KB Time limit exceeded
9 Execution timed out 1070 ms 1024 KB Time limit exceeded
10 Execution timed out 1058 ms 1024 KB Time limit exceeded
11 Execution timed out 1070 ms 1024 KB Time limit exceeded
12 Execution timed out 1082 ms 1024 KB Time limit exceeded
13 Execution timed out 1077 ms 1024 KB Time limit exceeded
14 Execution timed out 1068 ms 1280 KB Time limit exceeded
15 Execution timed out 1058 ms 1024 KB Time limit exceeded
16 Execution timed out 1063 ms 1024 KB Time limit exceeded
17 Execution timed out 1068 ms 1024 KB Time limit exceeded
18 Execution timed out 1081 ms 1024 KB Time limit exceeded
19 Execution timed out 1073 ms 1024 KB Time limit exceeded
20 Execution timed out 1077 ms 1024 KB Time limit exceeded
21 Execution timed out 1077 ms 1024 KB Time limit exceeded
22 Execution timed out 1081 ms 1024 KB Time limit exceeded
23 Execution timed out 1084 ms 1024 KB Time limit exceeded
24 Execution timed out 1067 ms 1024 KB Time limit exceeded
25 Execution timed out 1078 ms 1024 KB Time limit exceeded
26 Execution timed out 1074 ms 1024 KB Time limit exceeded
27 Execution timed out 1071 ms 1024 KB Time limit exceeded
28 Execution timed out 1063 ms 1024 KB Time limit exceeded
29 Execution timed out 1069 ms 1024 KB Time limit exceeded
30 Execution timed out 1067 ms 1024 KB Time limit exceeded
31 Execution timed out 1079 ms 1024 KB Time limit exceeded
32 Execution timed out 1061 ms 1024 KB Time limit exceeded
33 Execution timed out 1052 ms 1408 KB Time limit exceeded
34 Execution timed out 1075 ms 1408 KB Time limit exceeded
35 Execution timed out 1083 ms 1484 KB Time limit exceeded
36 Execution timed out 1079 ms 1508 KB Time limit exceeded
37 Execution timed out 1063 ms 1536 KB Time limit exceeded
38 Execution timed out 1077 ms 1536 KB Time limit exceeded
39 Execution timed out 1048 ms 1584 KB Time limit exceeded
40 Execution timed out 1064 ms 1536 KB Time limit exceeded
41 Execution timed out 1072 ms 1536 KB Time limit exceeded
42 Execution timed out 1074 ms 1792 KB Time limit exceeded
43 Execution timed out 1064 ms 1920 KB Time limit exceeded
44 Execution timed out 1048 ms 1920 KB Time limit exceeded
45 Execution timed out 1070 ms 1920 KB Time limit exceeded
46 Execution timed out 1080 ms 1920 KB Time limit exceeded
47 Execution timed out 1077 ms 1920 KB Time limit exceeded
48 Execution timed out 1062 ms 2040 KB Time limit exceeded
49 Execution timed out 1076 ms 2048 KB Time limit exceeded
50 Execution timed out 1044 ms 2044 KB Time limit exceeded
51 Execution timed out 1061 ms 2168 KB Time limit exceeded
52 Execution timed out 1072 ms 2176 KB Time limit exceeded
53 Execution timed out 1063 ms 2176 KB Time limit exceeded
54 Execution timed out 1061 ms 2176 KB Time limit exceeded
55 Execution timed out 1069 ms 2176 KB Time limit exceeded
56 Execution timed out 1069 ms 2168 KB Time limit exceeded
57 Execution timed out 1077 ms 2432 KB Time limit exceeded
58 Execution timed out 1075 ms 2304 KB Time limit exceeded
59 Execution timed out 1064 ms 2296 KB Time limit exceeded
60 Execution timed out 1060 ms 2424 KB Time limit exceeded
61 Execution timed out 1056 ms 2432 KB Time limit exceeded
62 Execution timed out 1067 ms 2432 KB Time limit exceeded
63 Execution timed out 1062 ms 2424 KB Time limit exceeded
64 Execution timed out 1081 ms 2432 KB Time limit exceeded
65 Execution timed out 1068 ms 2424 KB Time limit exceeded
66 Execution timed out 1076 ms 2424 KB Time limit exceeded
67 Execution timed out 1082 ms 2424 KB Time limit exceeded
68 Execution timed out 1075 ms 3036 KB Time limit exceeded
69 Execution timed out 1056 ms 3068 KB Time limit exceeded
70 Execution timed out 1043 ms 3064 KB Time limit exceeded
71 Execution timed out 1067 ms 3064 KB Time limit exceeded
72 Execution timed out 1077 ms 3072 KB Time limit exceeded
73 Execution timed out 1082 ms 3560 KB Time limit exceeded
74 Execution timed out 1068 ms 3556 KB Time limit exceeded
75 Execution timed out 1075 ms 3560 KB Time limit exceeded
76 Execution timed out 1073 ms 3560 KB Time limit exceeded
77 Execution timed out 1056 ms 3560 KB Time limit exceeded
78 Execution timed out 1070 ms 3260 KB Time limit exceeded
79 Execution timed out 1071 ms 3200 KB Time limit exceeded
80 Execution timed out 1068 ms 3192 KB Time limit exceeded
81 Execution timed out 1077 ms 3256 KB Time limit exceeded
82 Execution timed out 1071 ms 3296 KB Time limit exceeded
83 Execution timed out 1076 ms 3164 KB Time limit exceeded
84 Execution timed out 1078 ms 3164 KB Time limit exceeded
85 Execution timed out 1065 ms 3164 KB Time limit exceeded
86 Execution timed out 1066 ms 3164 KB Time limit exceeded
87 Execution timed out 1066 ms 3164 KB Time limit exceeded
88 Execution timed out 1067 ms 3040 KB Time limit exceeded
89 Execution timed out 1067 ms 3040 KB Time limit exceeded
90 Execution timed out 1044 ms 3056 KB Time limit exceeded
91 Execution timed out 1068 ms 3080 KB Time limit exceeded
92 Execution timed out 1065 ms 3080 KB Time limit exceeded