#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;
if(grid[t1][t2]=='D')return true;
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=1e6,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;
| ^
# |
결과 |
실행 시간 |
메모리 |
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 |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
7 |
Incorrect |
464 ms |
6408 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 |
3 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
320 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 |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
320 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
324 KB |
Output is correct |
29 |
Correct |
2 ms |
328 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
3 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
340 KB |
Output is correct |
33 |
Correct |
42 ms |
1492 KB |
Output is correct |
34 |
Correct |
50 ms |
1492 KB |
Output is correct |
35 |
Correct |
41 ms |
1540 KB |
Output is correct |
36 |
Correct |
57 ms |
1876 KB |
Output is correct |
37 |
Correct |
56 ms |
1916 KB |
Output is correct |
38 |
Correct |
53 ms |
1916 KB |
Output is correct |
39 |
Correct |
71 ms |
2348 KB |
Output is correct |
40 |
Correct |
70 ms |
2264 KB |
Output is correct |
41 |
Correct |
66 ms |
2344 KB |
Output is correct |
42 |
Correct |
93 ms |
2812 KB |
Output is correct |
43 |
Correct |
86 ms |
2812 KB |
Output is correct |
44 |
Correct |
90 ms |
2812 KB |
Output is correct |
45 |
Correct |
114 ms |
3292 KB |
Output is correct |
46 |
Correct |
106 ms |
3292 KB |
Output is correct |
47 |
Correct |
108 ms |
3300 KB |
Output is correct |
48 |
Correct |
126 ms |
3816 KB |
Output is correct |
49 |
Correct |
127 ms |
3916 KB |
Output is correct |
50 |
Correct |
125 ms |
3832 KB |
Output is correct |
51 |
Correct |
149 ms |
4368 KB |
Output is correct |
52 |
Correct |
145 ms |
4360 KB |
Output is correct |
53 |
Correct |
154 ms |
4308 KB |
Output is correct |
54 |
Correct |
203 ms |
4964 KB |
Output is correct |
55 |
Correct |
167 ms |
4964 KB |
Output is correct |
56 |
Correct |
177 ms |
4972 KB |
Output is correct |
57 |
Correct |
202 ms |
5608 KB |
Output is correct |
58 |
Correct |
198 ms |
5588 KB |
Output is correct |
59 |
Correct |
200 ms |
5628 KB |
Output is correct |
60 |
Correct |
215 ms |
6200 KB |
Output is correct |
61 |
Correct |
231 ms |
6292 KB |
Output is correct |
62 |
Correct |
236 ms |
6392 KB |
Output is correct |
63 |
Correct |
408 ms |
6228 KB |
Output is correct |
64 |
Correct |
470 ms |
6228 KB |
Output is correct |
65 |
Correct |
418 ms |
6296 KB |
Output is correct |
66 |
Correct |
437 ms |
6296 KB |
Output is correct |
67 |
Correct |
404 ms |
6300 KB |
Output is correct |
68 |
Correct |
443 ms |
6316 KB |
Output is correct |
69 |
Correct |
408 ms |
6440 KB |
Output is correct |
70 |
Correct |
444 ms |
6324 KB |
Output is correct |
71 |
Correct |
450 ms |
6324 KB |
Output is correct |
72 |
Correct |
471 ms |
6324 KB |
Output is correct |
73 |
Correct |
73 ms |
6576 KB |
Output is correct |
74 |
Correct |
492 ms |
6568 KB |
Output is correct |
75 |
Correct |
505 ms |
6604 KB |
Output is correct |
76 |
Correct |
473 ms |
6572 KB |
Output is correct |
77 |
Correct |
476 ms |
6576 KB |
Output is correct |
78 |
Correct |
443 ms |
6552 KB |
Output is correct |
79 |
Correct |
459 ms |
6484 KB |
Output is correct |
80 |
Correct |
471 ms |
6548 KB |
Output is correct |
81 |
Correct |
480 ms |
6552 KB |
Output is correct |
82 |
Correct |
491 ms |
6548 KB |
Output is correct |
83 |
Correct |
504 ms |
6504 KB |
Output is correct |
84 |
Correct |
470 ms |
6512 KB |
Output is correct |
85 |
Correct |
440 ms |
6508 KB |
Output is correct |
86 |
Correct |
482 ms |
6504 KB |
Output is correct |
87 |
Correct |
480 ms |
6504 KB |
Output is correct |
88 |
Correct |
468 ms |
6584 KB |
Output is correct |
89 |
Correct |
452 ms |
6460 KB |
Output is correct |
90 |
Correct |
469 ms |
6464 KB |
Output is correct |
91 |
Correct |
462 ms |
6412 KB |
Output is correct |
92 |
Correct |
475 ms |
6464 KB |
Output is correct |