#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
// #define int long long int
#define pb push_back
#define mid (l+(r-l)/2)
#define f first
#define s second
static const int N=805;
static const long long int MOD=(1LL<<61)-1;
static const long long int inf=INT64_MAX;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
void io(string name = ""){
freopen((name+".in").c_str(), "r", stdin);
freopen((name+".out").c_str(), "w", stdout);
}
int n,x,mx,my,hx,hy;
vector<vector<char>>v(N,vector<char>(N));
vector<vector<int>>bee(N,vector<int>(N,-1));
int mec[N][N];
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
bool inside(int a,int b){return ((a<=n && a>0)&&(b<=n && b>0)&&(v[a][b]=='G'||v[a][b]=='M'));}
void calcbee(){
queue<array<int,3>>q;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(v[i][j]=='H'){q.push({i,j,0});}
}
}
while(q.size()){
array<int,3>curr=q.front();
q.pop();
if(bee[curr[0]][curr[1]]!=-1){continue;}
bee[curr[0]][curr[1]]=curr[2];
for(int i=0;i<4;i++){
if(inside(curr[0]+dx[i],curr[1]+dy[i])){
q.push({curr[0]+dx[i],curr[1]+dy[i],curr[2]+1});
}
}
}
}
bool mecho(int k){
queue<array<int,3>>q;
q.push({mx,my,k});
while(q.size()){
array<int,3>curr=q.front();
q.pop();
if(mec[curr[0]][curr[1]]!=-1){continue;}
mec[curr[0]][curr[1]]=curr[2];
for(int i=0;i<4;i++){
if(inside(curr[0]+dx[i],curr[1]+dy[i]) && bee[curr[0]+dx[i]][curr[1]+dy[i]]>(curr[2]+1)/x){
q.push({curr[0]+dx[i],curr[1]+dy[i],curr[2]+1});
}
}
}
mec[mx][my]=-1;
for(int i=0;i<4;i++){
if(inside(hx+dx[i],hy+dy[i]) && mec[hx+dx[i]][hy+dy[i]]!=-1){return 1;}
}
return 0;
}
int32_t main(){
//io("local");
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>x;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>v[i][j];
if(v[i][j]=='M'){mx=i,my=j;}
if(v[i][j]=='D'){hx=i,hy=j;}
}
}
calcbee();
// for(int i=1;i<=n;i++){
// for(int j=1;j<=n;j++){cout<<bee[i][j]<<" ";}
// cout<<endl;
// }
int l=0,r=n*n;
bool chc=0,achc=0;
while(l<=r){
memset(mec,-1,sizeof(mec));
chc=mecho(mid*(x));
// cout<<mid<<" "<<chc<<endl;
if(chc){l=mid+1;achc=1;}
else{r=mid-1;}
// for(int i=1;i<=n;i++){
// for(int j=1;j<=n;j++){cout<<mec[i][j]<<" ";}
// cout<<endl;
// }
// cout<<endl;
}
// memset(mec,-1,sizeof(mec));
// if(!achc && mecho(0)){cout<<0;}
if(!achc){cout<<-1;}
else{cout<<l-1;}
return 0;
}
Compilation message
mecho.cpp: In function 'void io(std::string)':
mecho.cpp:18:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen((name+".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen((name+".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
6228 KB |
Output is correct |
2 |
Correct |
4 ms |
6016 KB |
Output is correct |
3 |
Correct |
4 ms |
6228 KB |
Output is correct |
4 |
Correct |
4 ms |
6228 KB |
Output is correct |
5 |
Correct |
4 ms |
6104 KB |
Output is correct |
6 |
Correct |
5 ms |
6240 KB |
Output is correct |
7 |
Correct |
171 ms |
7280 KB |
Output is correct |
8 |
Correct |
4 ms |
6228 KB |
Output is correct |
9 |
Correct |
5 ms |
6228 KB |
Output is correct |
10 |
Correct |
5 ms |
6228 KB |
Output is correct |
11 |
Correct |
5 ms |
6228 KB |
Output is correct |
12 |
Incorrect |
5 ms |
6228 KB |
Output isn't correct |
13 |
Incorrect |
7 ms |
6228 KB |
Output isn't correct |
14 |
Correct |
5 ms |
6228 KB |
Output is correct |
15 |
Correct |
5 ms |
6228 KB |
Output is correct |
16 |
Correct |
5 ms |
6228 KB |
Output is correct |
17 |
Correct |
4 ms |
6000 KB |
Output is correct |
18 |
Correct |
5 ms |
6148 KB |
Output is correct |
19 |
Correct |
5 ms |
6228 KB |
Output is correct |
20 |
Correct |
5 ms |
6228 KB |
Output is correct |
21 |
Correct |
5 ms |
6008 KB |
Output is correct |
22 |
Correct |
4 ms |
6228 KB |
Output is correct |
23 |
Correct |
6 ms |
6228 KB |
Output is correct |
24 |
Correct |
6 ms |
6228 KB |
Output is correct |
25 |
Correct |
5 ms |
6228 KB |
Output is correct |
26 |
Correct |
5 ms |
6228 KB |
Output is correct |
27 |
Correct |
5 ms |
6228 KB |
Output is correct |
28 |
Correct |
5 ms |
6228 KB |
Output is correct |
29 |
Correct |
5 ms |
6228 KB |
Output is correct |
30 |
Correct |
5 ms |
6228 KB |
Output is correct |
31 |
Correct |
6 ms |
6228 KB |
Output is correct |
32 |
Correct |
5 ms |
6228 KB |
Output is correct |
33 |
Correct |
11 ms |
6248 KB |
Output is correct |
34 |
Correct |
11 ms |
6228 KB |
Output is correct |
35 |
Correct |
38 ms |
6392 KB |
Output is correct |
36 |
Correct |
11 ms |
6228 KB |
Output is correct |
37 |
Correct |
16 ms |
6396 KB |
Output is correct |
38 |
Correct |
46 ms |
6228 KB |
Output is correct |
39 |
Correct |
11 ms |
6228 KB |
Output is correct |
40 |
Correct |
13 ms |
6228 KB |
Output is correct |
41 |
Correct |
67 ms |
6436 KB |
Output is correct |
42 |
Correct |
18 ms |
6484 KB |
Output is correct |
43 |
Correct |
22 ms |
6484 KB |
Output is correct |
44 |
Correct |
86 ms |
6276 KB |
Output is correct |
45 |
Correct |
16 ms |
6484 KB |
Output is correct |
46 |
Correct |
19 ms |
6476 KB |
Output is correct |
47 |
Correct |
97 ms |
6552 KB |
Output is correct |
48 |
Correct |
17 ms |
6484 KB |
Output is correct |
49 |
Correct |
19 ms |
6360 KB |
Output is correct |
50 |
Correct |
119 ms |
6364 KB |
Output is correct |
51 |
Correct |
20 ms |
6416 KB |
Output is correct |
52 |
Correct |
28 ms |
6484 KB |
Output is correct |
53 |
Correct |
151 ms |
6500 KB |
Output is correct |
54 |
Correct |
30 ms |
6484 KB |
Output is correct |
55 |
Correct |
34 ms |
6500 KB |
Output is correct |
56 |
Correct |
197 ms |
6748 KB |
Output is correct |
57 |
Correct |
24 ms |
6740 KB |
Output is correct |
58 |
Correct |
41 ms |
6748 KB |
Output is correct |
59 |
Correct |
213 ms |
6808 KB |
Output is correct |
60 |
Correct |
27 ms |
6632 KB |
Output is correct |
61 |
Correct |
28 ms |
6628 KB |
Output is correct |
62 |
Correct |
232 ms |
6732 KB |
Output is correct |
63 |
Correct |
128 ms |
6624 KB |
Output is correct |
64 |
Correct |
246 ms |
6740 KB |
Output is correct |
65 |
Correct |
201 ms |
6852 KB |
Output is correct |
66 |
Correct |
150 ms |
6740 KB |
Output is correct |
67 |
Correct |
131 ms |
6636 KB |
Output is correct |
68 |
Correct |
62 ms |
6732 KB |
Output is correct |
69 |
Correct |
58 ms |
6872 KB |
Output is correct |
70 |
Correct |
52 ms |
6716 KB |
Output is correct |
71 |
Correct |
55 ms |
6756 KB |
Output is correct |
72 |
Incorrect |
38 ms |
6732 KB |
Output isn't correct |
73 |
Incorrect |
76 ms |
8308 KB |
Output isn't correct |
74 |
Correct |
116 ms |
8368 KB |
Output is correct |
75 |
Correct |
152 ms |
7708 KB |
Output is correct |
76 |
Correct |
108 ms |
8268 KB |
Output is correct |
77 |
Correct |
133 ms |
8420 KB |
Output is correct |
78 |
Correct |
171 ms |
8192 KB |
Output is correct |
79 |
Correct |
98 ms |
8264 KB |
Output is correct |
80 |
Correct |
111 ms |
7832 KB |
Output is correct |
81 |
Correct |
164 ms |
8524 KB |
Output is correct |
82 |
Correct |
105 ms |
8280 KB |
Output is correct |
83 |
Correct |
157 ms |
7860 KB |
Output is correct |
84 |
Correct |
119 ms |
7676 KB |
Output is correct |
85 |
Correct |
170 ms |
7852 KB |
Output is correct |
86 |
Correct |
143 ms |
8012 KB |
Output is correct |
87 |
Correct |
144 ms |
7980 KB |
Output is correct |
88 |
Correct |
180 ms |
7604 KB |
Output is correct |
89 |
Correct |
164 ms |
7580 KB |
Output is correct |
90 |
Correct |
170 ms |
7732 KB |
Output is correct |
91 |
Correct |
179 ms |
7604 KB |
Output is correct |
92 |
Correct |
172 ms |
7396 KB |
Output is correct |