Submission #59943

# Submission time Handle Problem Language Result Execution time Memory
59943 2018-07-23T11:04:36 Z Tenuun Mecho (IOI09_mecho) C++17
50 / 100
1000 ms 6908 KB
#include<bits/stdc++.h>

using namespace std;

int n, s, u, V, x, y, z;
int a[803][803], dx[4]={0, 0, 1, -1}, dy[4]={1, -1, 0, 0};
int bear[803][803];

vector<string> v;
vector<pair<int, int> >bee, o;
queue<pair<int, int>>q, b;

bool BFS(){
	queue<pair<pair<int, int>, int> >t;
	t.push({{x, y}, 0});
	bear[x][y]=0;
	while(!t.empty()){
		if(t.front().second>=s) return false;
		x=t.front().first.first, y=t.front().first.second, z=t.front().second;
		t.pop();
		if(a[x][y]!=-1) continue;
		for(int i=0; i<4; i++){
			if(v[x+dx[i]][y+dy[i]]=='D') return true;
			if(v[x+dx[i]][y+dy[i]]=='G' && a[x+dx[i]][y+dy[i]]==-1 && (bear[x+dx[i]][y+dy[i]]>bear[x][y]+1 || bear[x+dx[i]][y+dy[i]]==-1)){
				bear[x+dx[i]][y+dy[i]]=z+1;
				if(z+1>=s) o.push_back({x+dx[i], y+dy[i]});
				else t.push({{x+dx[i], y+dy[i]}, z+1});
			}
		}
	}
	return false;
}

bool check(int m){
	int curr;
	for(int i=0; i<bee.size(); i++){
		x=bee[i].first, y=bee[i].second;
		a[x][y]=0;
		q.push(bee[i]);
	}
	b.push({u, V});
	bear[u][V]=0;
	while(!q.empty()){
		x=q.front().first, y=q.front().second;
		curr=z=a[x][y];
		if(z>=m){
			while(!b.empty()){
				x=b.front().first, y=b.front().second;
				b.pop();
				if(BFS()) return true;
			}
			for(int i=0; i<o.size(); i++) b.push(o[i]);
			o.clear();
		}
		while(!q.empty() && a[q.front().first][q.front().second]==curr){
			x=q.front().first, y=q.front().second;
			q.pop();
			for(int i=0; i<4; i++){
				if((v[x+dx[i]][y+dy[i]]=='G' || v[x+dx[i]][y+dy[i]]=='M')&& a[x+dx[i]][y+dy[i]]==-1){
					a[x+dx[i]][y+dy[i]]=a[x][y]+1;
					q.push({x+dx[i], y+dy[i]});
				}
			}
		}	
	}
	return false;
}

void solve(){
	int l=0, r=(n+1)*n, m=(l+r+1)/2;
	while(l<r){
		memset(a, -1, sizeof a);
		memset(bear, -1, sizeof bear);
		while(!q.empty())q.pop();
		while(!b.empty())b.pop();
		o.clear();
		if(check(m)) l=m;
		else r=m-1;
		m=(l+r+1)/2;
	}
	/*for(int i=1; i<=n; i++){
		for(int j=1; j<=n; j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
	for(int i=1; i<=n; i++){
		for(int j=1; j<=n; j++){
			cout << bear[i][j] << " ";
		}
		cout << endl;
	}*/
	memset(a, -1, sizeof a);
	memset(bear, -1, sizeof bear);
	while(!q.empty())q.pop();
	while(!b.empty())b.pop();
	o.clear();
	if(check(l)==false) cout << -1;
	else cout << l;
}


int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> s;
	string tmp, k;
	for(int i=0; i<n+2; i++) tmp+='T';
	v.push_back(tmp);
	for(int i=1; i<=n; i++){
		cin >> k;
		k='T'+k+'T';
		for(int j=1; j<=n; j++){
			if(k[j]=='M'){
				u=i, V=j;
			}
			if(k[j]=='H'){
				bee.push_back({i, j});
			}
		}
		v.push_back(k);
	}
	v.push_back(tmp);
	solve();
	return 0;
}

Compilation message

mecho.cpp: In function 'bool check(int)':
mecho.cpp:36:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<bee.size(); i++){
               ~^~~~~~~~~~~
mecho.cpp:52:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int i=0; i<o.size(); i++) b.push(o[i]);
                 ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5368 KB Output is correct
2 Correct 8 ms 5556 KB Output is correct
3 Correct 9 ms 5556 KB Output is correct
4 Correct 6 ms 5556 KB Output is correct
5 Correct 10 ms 5568 KB Output is correct
6 Correct 11 ms 5568 KB Output is correct
7 Execution timed out 1087 ms 6608 KB Time limit exceeded
8 Correct 10 ms 6608 KB Output is correct
9 Correct 12 ms 6608 KB Output is correct
10 Correct 10 ms 6608 KB Output is correct
11 Correct 12 ms 6608 KB Output is correct
12 Correct 11 ms 6608 KB Output is correct
13 Correct 15 ms 6608 KB Output is correct
14 Correct 14 ms 6608 KB Output is correct
15 Correct 9 ms 6608 KB Output is correct
16 Correct 10 ms 6608 KB Output is correct
17 Correct 10 ms 6608 KB Output is correct
18 Correct 11 ms 6608 KB Output is correct
19 Correct 12 ms 6608 KB Output is correct
20 Correct 11 ms 6608 KB Output is correct
21 Correct 10 ms 6608 KB Output is correct
22 Correct 13 ms 6608 KB Output is correct
23 Correct 12 ms 6608 KB Output is correct
24 Correct 10 ms 6608 KB Output is correct
25 Correct 11 ms 6608 KB Output is correct
26 Correct 11 ms 6608 KB Output is correct
27 Correct 15 ms 6608 KB Output is correct
28 Correct 10 ms 6608 KB Output is correct
29 Correct 12 ms 6608 KB Output is correct
30 Correct 12 ms 6608 KB Output is correct
31 Correct 10 ms 6608 KB Output is correct
32 Correct 11 ms 6608 KB Output is correct
33 Correct 49 ms 6608 KB Output is correct
34 Correct 40 ms 6608 KB Output is correct
35 Correct 436 ms 6608 KB Output is correct
36 Correct 90 ms 6608 KB Output is correct
37 Correct 78 ms 6608 KB Output is correct
38 Correct 884 ms 6608 KB Output is correct
39 Correct 66 ms 6608 KB Output is correct
40 Correct 66 ms 6608 KB Output is correct
41 Execution timed out 1089 ms 6608 KB Time limit exceeded
42 Correct 81 ms 6608 KB Output is correct
43 Correct 110 ms 6608 KB Output is correct
44 Execution timed out 1079 ms 6608 KB Time limit exceeded
45 Correct 106 ms 6608 KB Output is correct
46 Correct 98 ms 6608 KB Output is correct
47 Execution timed out 1091 ms 6608 KB Time limit exceeded
48 Correct 125 ms 6608 KB Output is correct
49 Correct 169 ms 6608 KB Output is correct
50 Execution timed out 1066 ms 6608 KB Time limit exceeded
51 Correct 157 ms 6608 KB Output is correct
52 Correct 168 ms 6608 KB Output is correct
53 Execution timed out 1091 ms 6608 KB Time limit exceeded
54 Correct 177 ms 6608 KB Output is correct
55 Correct 169 ms 6608 KB Output is correct
56 Execution timed out 1085 ms 6608 KB Time limit exceeded
57 Correct 276 ms 6608 KB Output is correct
58 Correct 230 ms 6608 KB Output is correct
59 Execution timed out 1075 ms 6608 KB Time limit exceeded
60 Correct 211 ms 6608 KB Output is correct
61 Correct 200 ms 6608 KB Output is correct
62 Execution timed out 1062 ms 6608 KB Time limit exceeded
63 Correct 814 ms 6608 KB Output is correct
64 Execution timed out 1069 ms 6608 KB Time limit exceeded
65 Execution timed out 1065 ms 6608 KB Time limit exceeded
66 Execution timed out 1012 ms 6608 KB Time limit exceeded
67 Correct 923 ms 6608 KB Output is correct
68 Execution timed out 1080 ms 6652 KB Time limit exceeded
69 Execution timed out 1083 ms 6652 KB Time limit exceeded
70 Correct 745 ms 6652 KB Output is correct
71 Correct 713 ms 6652 KB Output is correct
72 Correct 737 ms 6652 KB Output is correct
73 Correct 641 ms 6884 KB Output is correct
74 Execution timed out 1075 ms 6884 KB Time limit exceeded
75 Correct 984 ms 6884 KB Output is correct
76 Execution timed out 1082 ms 6908 KB Time limit exceeded
77 Execution timed out 1081 ms 6908 KB Time limit exceeded
78 Execution timed out 1087 ms 6908 KB Time limit exceeded
79 Execution timed out 1098 ms 6908 KB Time limit exceeded
80 Execution timed out 1078 ms 6908 KB Time limit exceeded
81 Execution timed out 1073 ms 6908 KB Time limit exceeded
82 Execution timed out 1090 ms 6908 KB Time limit exceeded
83 Execution timed out 1079 ms 6908 KB Time limit exceeded
84 Execution timed out 1090 ms 6908 KB Time limit exceeded
85 Execution timed out 1068 ms 6908 KB Time limit exceeded
86 Execution timed out 1079 ms 6908 KB Time limit exceeded
87 Execution timed out 1081 ms 6908 KB Time limit exceeded
88 Execution timed out 1086 ms 6908 KB Time limit exceeded
89 Execution timed out 1087 ms 6908 KB Time limit exceeded
90 Execution timed out 1081 ms 6908 KB Time limit exceeded
91 Execution timed out 1089 ms 6908 KB Time limit exceeded
92 Execution timed out 1071 ms 6908 KB Time limit exceeded