Submission #1037042

# Submission time Handle Problem Language Result Execution time Memory
1037042 2024-07-27T23:32:49 Z hippo123 Mecho (IOI09_mecho) C++17
64 / 100
146 ms 7252 KB
#include <bits/stdc++.h>
 
using namespace std;
#define ll long long
#define f first
#define s second
#define pr pair<int, int>
#define pb push_back
const int ndim=800;
const int dmax=1e9; 

vector<string> mp(ndim);
vector<int> level(ndim*ndim, -1);
vector<int> level2(ndim*ndim, -1);

int dx[4]={1, -1, 0, 0};
int dy[4]={0, 0, 1, -1};
int n, s; 

bool dmcheck(int x, int y){
	if(x>=0 && x<n && y>=0 && y<n && mp[x][y]!='T' && mp[x][y]!='D' && level[x*n+y]==-1 ) return true;
	else return false;
}

bool bearcheck(int x, int y){
	if(x>=0 && x<n && y>=0 && y<n && mp[x][y]!='T' && mp[x][y]!='H' && level2[x*n+y]==-1 ) return true;
	else return false;

}

bool check(int mid, pr M, pr hs){
	
	for (int i=0; i<n*n; i++) level2[i]=-1;
	
	queue<pr> q; 
	q.push({M.f*n+M.s, 0});
	level2[M.f*n+M.s]=0;
	bool judge=false; 
	while(!q.empty()){
		pr n1=q.front(); q.pop();
		int nd=n1.f; int lev=n1.s; 
		int x=nd/n; int y=nd%n;
		//cout<< "x/y= "<<x<<" "<<y<<endl;
		//cout<<" level[x*n+y]-mid= "<<level[x*n+y]-mid<<endl;
		if(lev>=(level[x*n+y]-mid)*s) continue; 
		
		
		for (int i=0; i<4; i++){
			int x2=x+dx[i]; int y2=y+dy[i];
			
			if( bearcheck(x2, y2)){
				//cout<<" x2/y2= "<<x2<<" "<<y2<< "hs= "<<hs.f<<" "<<hs.s<<endl;
				if(x2==hs.f && y2==hs.s ){ judge=true; break;}
				//cout<<" lev+1 / (level[x2*n+y2]-mid)*s= "<<lev+1 <<" "<< (level[x2*n+y2]-mid)*s<<endl;
				if(lev+1 <= (level[x2*n+y2]-mid)*s){ // level is bee
					q.push({x2*n+y2, lev+1});
					level2[x2*n+y2]= lev+1;
				}
				
				
			}
			if(judge) break; 
		}	
		if(judge) break; 
	}

	//for (int i=0; i<n; i++){
	//	for (int j=0; j<n; j++) cout<<level2[i*n+j]<<" ";
	//	cout<<endl;
	//}	
	
	
	//cout<<" judge= "<<judge<<endl;
	return judge;
	
}


int main(){
	cin>>n>>s; 
	vector<pr> hv;
	pr M, hs;	
	for (int i=0; i<n; i++) {
		cin>>mp[i]; 
		for (int j=0; j<n; j++){
			if(mp[i][j]=='H') hv.pb({i, j});
			if(mp[i][j]=='M') M={i, j};
			if(mp[i][j]=='D') hs={i, j};
		}
	}
	//cout<<" hs= "<<hs.f<<" "<<hs.s<<endl;
	queue<pr> q; 

	for (auto elem: hv) {
		q.push({elem.f*n+elem.s, 0});
		level[elem.f*n+elem.s]=0;
	}
	while (!q.empty()){
		pr n1=q.front(); q.pop();
		
		int nd=n1.f; int lev=n1.s;
		int x=nd/n; int y=nd%n;
		
		for (int i=0; i<4; i++){
			int x2=x+dx[i]; int y2=y+dy[i];
			if(dmcheck(x2, y2)){
				q.push({x2*n+y2, lev+1}); level[x2*n+y2]=lev+1; 
			}
		}		
	}
	//for (int i=0; i<n; i++){
	//	for (int j=0; j<n; j++) cout<<level[i*n+j]<<" ";
	//	cout<<endl;
	//}
	
	int lft=0; int rht=100000;
	while(lft<rht ){
		
		int mid=(rht+lft+1)/2;
	//	cout<<" lft/rht/mid= "<<lft<<" "<<rht<<" "<<mid<<endl;
		if(check(mid, M, hs)) lft=mid;
		else rht=mid-1; 
	}
	cout<<lft<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5468 KB Output is correct
2 Correct 2 ms 5468 KB Output is correct
3 Correct 2 ms 5468 KB Output is correct
4 Correct 2 ms 5468 KB Output is correct
5 Correct 1 ms 5468 KB Output is correct
6 Correct 2 ms 5464 KB Output is correct
7 Correct 100 ms 6824 KB Output is correct
8 Incorrect 2 ms 5484 KB Output isn't correct
9 Correct 2 ms 5468 KB Output is correct
10 Correct 2 ms 5468 KB Output is correct
11 Correct 2 ms 5468 KB Output is correct
12 Correct 2 ms 5468 KB Output is correct
13 Correct 2 ms 5492 KB Output is correct
14 Incorrect 2 ms 5480 KB Output isn't correct
15 Correct 2 ms 5468 KB Output is correct
16 Correct 1 ms 5468 KB Output is correct
17 Correct 2 ms 5468 KB Output is correct
18 Correct 2 ms 5488 KB Output is correct
19 Correct 2 ms 5468 KB Output is correct
20 Correct 1 ms 5468 KB Output is correct
21 Correct 2 ms 5468 KB Output is correct
22 Correct 2 ms 5468 KB Output is correct
23 Correct 2 ms 5468 KB Output is correct
24 Correct 2 ms 5468 KB Output is correct
25 Correct 2 ms 5468 KB Output is correct
26 Correct 2 ms 5468 KB Output is correct
27 Correct 2 ms 5468 KB Output is correct
28 Correct 2 ms 5468 KB Output is correct
29 Correct 2 ms 5464 KB Output is correct
30 Correct 2 ms 5468 KB Output is correct
31 Correct 2 ms 5468 KB Output is correct
32 Correct 2 ms 5468 KB Output is correct
33 Correct 6 ms 5756 KB Output is correct
34 Correct 5 ms 5724 KB Output is correct
35 Correct 20 ms 5776 KB Output is correct
36 Correct 7 ms 5720 KB Output is correct
37 Correct 7 ms 5724 KB Output is correct
38 Correct 26 ms 5844 KB Output is correct
39 Incorrect 8 ms 5876 KB Output isn't correct
40 Correct 8 ms 5724 KB Output is correct
41 Correct 33 ms 5724 KB Output is correct
42 Incorrect 9 ms 5980 KB Output isn't correct
43 Correct 9 ms 5980 KB Output is correct
44 Correct 41 ms 6100 KB Output is correct
45 Incorrect 11 ms 6232 KB Output isn't correct
46 Correct 11 ms 6232 KB Output is correct
47 Correct 55 ms 6236 KB Output is correct
48 Incorrect 13 ms 6236 KB Output isn't correct
49 Correct 13 ms 6420 KB Output is correct
50 Correct 60 ms 6236 KB Output is correct
51 Incorrect 14 ms 6488 KB Output isn't correct
52 Incorrect 15 ms 6492 KB Output isn't correct
53 Correct 70 ms 6744 KB Output is correct
54 Incorrect 16 ms 6492 KB Output isn't correct
55 Incorrect 16 ms 6492 KB Output isn't correct
56 Correct 86 ms 6492 KB Output is correct
57 Incorrect 19 ms 6748 KB Output isn't correct
58 Incorrect 21 ms 6748 KB Output isn't correct
59 Correct 93 ms 6744 KB Output is correct
60 Incorrect 21 ms 6784 KB Output isn't correct
61 Incorrect 20 ms 6748 KB Output isn't correct
62 Correct 112 ms 6872 KB Output is correct
63 Correct 86 ms 6748 KB Output is correct
64 Correct 146 ms 6744 KB Output is correct
65 Correct 125 ms 6744 KB Output is correct
66 Correct 124 ms 6744 KB Output is correct
67 Incorrect 109 ms 6748 KB Output isn't correct
68 Correct 44 ms 6744 KB Output is correct
69 Correct 42 ms 6748 KB Output is correct
70 Correct 37 ms 6744 KB Output is correct
71 Correct 36 ms 6748 KB Output is correct
72 Correct 32 ms 6744 KB Output is correct
73 Correct 34 ms 7004 KB Output is correct
74 Correct 49 ms 7168 KB Output is correct
75 Correct 63 ms 7004 KB Output is correct
76 Correct 59 ms 7004 KB Output is correct
77 Correct 58 ms 7152 KB Output is correct
78 Incorrect 68 ms 7136 KB Output isn't correct
79 Correct 59 ms 7132 KB Output is correct
80 Correct 60 ms 7004 KB Output is correct
81 Correct 67 ms 7000 KB Output is correct
82 Correct 56 ms 7096 KB Output is correct
83 Correct 73 ms 7000 KB Output is correct
84 Correct 80 ms 7252 KB Output is correct
85 Correct 72 ms 7004 KB Output is correct
86 Correct 73 ms 7040 KB Output is correct
87 Correct 77 ms 7092 KB Output is correct
88 Correct 73 ms 7004 KB Output is correct
89 Correct 79 ms 7032 KB Output is correct
90 Correct 82 ms 7004 KB Output is correct
91 Correct 82 ms 7036 KB Output is correct
92 Correct 80 ms 7004 KB Output is correct