Submission #777230

# Submission time Handle Problem Language Result Execution time Memory
777230 2023-07-08T19:43:41 Z _ZeyadRafat Mecho (IOI09_mecho) C++14
89 / 100
743 ms 7004 KB
#include <bits/stdc++.h>
using namespace std;

long long  gcd(long long a, long long b){
    if(b == 0) return a;
    return gcd(b, a % b);
}
long long _gcd(long long n1,long long n2)
{  while (n2!=0){
        long long temp=n1;
        n1=n2;
        n2=temp%n1;
    }
    return n1;
}
long long _lcm(long long n1,long long n2){
    return (n1/ _gcd(n1,n2))*n2;
}

bool prime(long long n){
    if(n==2)return true;
    if(n<2||n%2==0)return false;
    int d= sqrtl(n);
    for(int i=3;i<= d;i+=2)
        if(n%i==0)return false;
    return true;
}

string to_binary(long long x){
    string s="";
    while(x){
        int d=x%2;
        char c=d+'0';
        s+=c;
        x/=2;
    }
    return s;
}
long long to_dec(string s){
    long long su=0;


    int d=0;

    for(long long  i=0;i<(s.size());i++){
        if(s[i]=='0')continue;
        long long q= round(pow(2LL,i));
        su+=q;
    }
    return su;
}
bool com(pair<long long ,long long >&x,pair<long long ,long long >&y){
	if(x.first!=y.first)return x.first>y.first;
	return x.second<y.second;
}
void Nozarashi(){
	long long n,k;
	cin>>n>>k;
	vector<string>grid;
	for(int i=0;i<n;i++){
		string s;
		cin>>s;
		grid.push_back(s);
	}
	long long s1,s2;
	for(int i=0;i<int(grid.size());i++){
		for(int j=0;j<int(grid[i].size());j++){
			if(grid[i][j]=='M'){
             s1=i;
             s2=j;
			}

		}
	}
	function<bool(int ,int,int,int)>in_range=[&](int i,int j,int n,int m){
		if(i>=0&&i<n&&j>=0&&j<m)return true;
		return false;
	};
   function<bool(long long ) >bfs=[&](long long mi){
   long long visted[n+3][n+3];
   memset(visted,0,sizeof(visted));
   queue<pair<int,int> >qe;
   for(int i=0;i<n;i++){
   	for(int j=0;j<n;j++){
   		if(grid[i][j]=='H'){qe.push({i,j});
   		   		visted[i][j]=true;}
   	}
   }
   int level=0;
   while(!qe.empty()){
   	if(level==mi)break;
   	long long sz=qe.size();
   	for(int d=0;d<sz;d++){
   		long long i=qe.front().first;
   		long long j=qe.front().second;

   		qe.pop();
   		for(int I=-1;I<=1;I++){
   			for(int J=-1;J<=1;J++){
   				if(abs(I)==abs(J))continue;
   				long long t1=i+I;
   				long long t2=j+J;
   				if(!in_range(t1,t2,n,n))continue;
   				if(visted[t1][t2])continue;
   				if(grid[t1][t2]=='M'){
   					return false;
   				}
   				if(grid[t1][t2]=='D')continue;
   				if(grid[t1][t2]=='T')continue;
   				qe.push({t1,t2});
   				visted[t1][t2]=true;
   			}
   		}
   	}
   	level++;
   }
   level=0;
   queue<pair<int,int > >qem;
   qem.push({s1,s2});
   visted[s1][s2]=true;

   while(!qem.empty()||!qe.empty()){

   	long long sz=qe.size();
   	if((level+1)%k==0){
   	for(int d=0;d<sz;d++){
   		long long i=qe.front().first;
   		long long j=qe.front().second;
   		qe.pop();

   		for(int I=-1;I<=1;I++){
   			for(int J=-1;J<=1;J++){
   				if(abs(I)==abs(J))continue;
   				long long t1=i+I;
   				long long t2=j+J;
   				if(!in_range(t1,t2,n,n))continue;
   				if(visted[t1][t2])continue;

   				if(grid[t1][t2]=='T')continue;

   				qe.push({t1,t2});
   				visted[t1][t2]=true;

   		}
   	}
   	}
   	sz=qem.size();
   		for(int d=0;d<sz;d++){
   		long long i=qem.front().first;
   		long long j=qem.front().second;
   		qem.pop();

   		for(int I=-1;I<=1;I++){
   			for(int J=-1;J<=1;J++){
   				if(abs(I)==abs(J))continue;
   				long long t1=i+I;
   				long long t2=j+J;
   				if(!in_range(t1,t2,n,n))continue;
   				if(visted[t1][t2])continue;

   				if(grid[t1][t2]=='D')return true;

   				if(grid[t1][t2]=='T')continue;
   				qem.push({t1,t2});
   				visted[t1][t2]=true;

   		}
   	}
   	}
   	}
  else {
     sz=qem.size();
   		for(int d=0;d<sz;d++){
   		long long i=qem.front().first;
   		long long j=qem.front().second;
   		qem.pop();

   		for(int I=-1;I<=1;I++){
   			for(int J=-1;J<=1;J++){
   				if(abs(I)==abs(J))continue;
   				long long t1=i+I;
   				long long t2=j+J;
   				if(!in_range(t1,t2,n,n))continue;
   				if(visted[t1][t2])continue;
   				if(grid[t1][t2]=='T')continue;
   				if(grid[t1][t2]=='D')return true;

   				qem.push({t1,t2});
   				visted[t1][t2]=true;

   		}
   	}
   	}
  }
  level++;
   }
  return false;
   };
long long l=0,r=1e9,ans=-1;
while(l<=r){
	long long mid=l+(r-l)/2;

	if(bfs(mid)){
		l=mid+1;
		ans=mid;
	}
	else r=mid-1;
}
cout<<ans<<endl;
}


int main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0);


    //// freopen("lasers.in", "r", stdin);
    // freopen("lasers.out", "w", stdout);

    int t=1;
    while (t--){

        Nozarashi();
    }
    return 0;
}

Compilation message

mecho.cpp: In function 'long long int to_dec(std::string)':
mecho.cpp:45:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |     for(long long  i=0;i<(s.size());i++){
      |                        ~^~~~~~~~~~~
mecho.cpp:43:9: warning: unused variable 'd' [-Wunused-variable]
   43 |     int d=0;
      |         ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Correct 1 ms 316 KB Output is correct
4 Incorrect 1 ms 320 KB Output isn't correct
5 Correct 1 ms 320 KB Output is correct
6 Incorrect 1 ms 212 KB Output isn't correct
7 Incorrect 691 ms 6776 KB Output isn't correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 4 ms 316 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 316 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 316 KB Output is correct
27 Correct 3 ms 292 KB Output is correct
28 Correct 3 ms 340 KB Output is correct
29 Correct 3 ms 340 KB Output is correct
30 Correct 3 ms 324 KB Output is correct
31 Correct 4 ms 340 KB Output is correct
32 Correct 3 ms 340 KB Output is correct
33 Correct 66 ms 1492 KB Output is correct
34 Correct 63 ms 1540 KB Output is correct
35 Correct 51 ms 1544 KB Output is correct
36 Correct 88 ms 1920 KB Output is correct
37 Correct 85 ms 1920 KB Output is correct
38 Correct 81 ms 1920 KB Output is correct
39 Correct 107 ms 2260 KB Output is correct
40 Correct 128 ms 2260 KB Output is correct
41 Correct 85 ms 2356 KB Output is correct
42 Correct 133 ms 2772 KB Output is correct
43 Correct 131 ms 2772 KB Output is correct
44 Correct 109 ms 2764 KB Output is correct
45 Correct 160 ms 3284 KB Output is correct
46 Correct 151 ms 3336 KB Output is correct
47 Correct 133 ms 3340 KB Output is correct
48 Correct 190 ms 3924 KB Output is correct
49 Correct 185 ms 3924 KB Output is correct
50 Correct 162 ms 3916 KB Output is correct
51 Correct 221 ms 4532 KB Output is correct
52 Correct 217 ms 4524 KB Output is correct
53 Correct 186 ms 4556 KB Output is correct
54 Correct 253 ms 5204 KB Output is correct
55 Correct 253 ms 5204 KB Output is correct
56 Correct 220 ms 5204 KB Output is correct
57 Correct 299 ms 5844 KB Output is correct
58 Correct 283 ms 5904 KB Output is correct
59 Correct 264 ms 5920 KB Output is correct
60 Correct 341 ms 6664 KB Output is correct
61 Correct 355 ms 6664 KB Output is correct
62 Correct 306 ms 6672 KB Output is correct
63 Correct 607 ms 6668 KB Output is correct
64 Correct 681 ms 6668 KB Output is correct
65 Correct 659 ms 6668 KB Output is correct
66 Correct 622 ms 6684 KB Output is correct
67 Correct 611 ms 6732 KB Output is correct
68 Correct 663 ms 6716 KB Output is correct
69 Correct 609 ms 6700 KB Output is correct
70 Correct 638 ms 6612 KB Output is correct
71 Correct 654 ms 6824 KB Output is correct
72 Correct 666 ms 6700 KB Output is correct
73 Correct 88 ms 6948 KB Output is correct
74 Correct 720 ms 6948 KB Output is correct
75 Correct 722 ms 6948 KB Output is correct
76 Correct 718 ms 6988 KB Output is correct
77 Correct 743 ms 6948 KB Output is correct
78 Correct 676 ms 6920 KB Output is correct
79 Correct 692 ms 6920 KB Output is correct
80 Correct 686 ms 6988 KB Output is correct
81 Correct 690 ms 6920 KB Output is correct
82 Correct 719 ms 6916 KB Output is correct
83 Correct 707 ms 6876 KB Output is correct
84 Correct 735 ms 6880 KB Output is correct
85 Correct 615 ms 6988 KB Output is correct
86 Correct 726 ms 6880 KB Output is correct
87 Correct 704 ms 7004 KB Output is correct
88 Correct 717 ms 6832 KB Output is correct
89 Correct 669 ms 6832 KB Output is correct
90 Correct 723 ms 6828 KB Output is correct
91 Correct 712 ms 6828 KB Output is correct
92 Correct 701 ms 6832 KB Output is correct