#include <bits/stdc++.h>
#define inf 1000000007
#define mod 1000000007
#define rnd() rand_num()
#define bigrnd() dis(rand_num)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#define int long long
using namespace std;
unsigned sed=std::chrono::system_clock::now().time_since_epoch().count();
mt19937 rand_num(sed);
uniform_int_distribution<long long> dis(0,inf);
template <typename T> void read(T &x){
x=0;char ch=getchar();int fh=1;
while (ch<'0'||ch>'9'){if (ch=='-')fh=-1;ch=getchar();}
while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
x*=fh;
}
template <typename T> void write(T x) {
if (x<0) x=-x,putchar('-');
if (x>9) write(x/10);
putchar(x%10+'0');
}
template <typename T> void writeln(T x) {
write(x);
puts("");
}
const int dx[]={-1,0,1,0},dy[]={0,-1,0,1};
int n,s;
char mp[805][1605];
int bee[805][805];
int bear[805][805];
bool check(int mid)
{
memset(bear,0x3f,sizeof(bear));
queue<pair<int,int>> q;
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
if(mp[i][j]=='M')
{
if(mid*s>=bee[i][j]) return false;
bear[i][j]=mid*s;
q.push(make_pair(i,j));
}
while(!q.empty())
{
pair<int,int> p=q.front();
q.pop();
if(mp[p.first][p.second]=='D') return true;
for(int d=0;d<4;++d)
{
int xx=p.first+dx[d],yy=p.second+dy[d];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n&&(mp[xx][yy]=='G'||mp[xx][yy]=='D')&&bear[p.first][p.second]+1<bee[xx][yy]&&bear[xx][yy]>bear[p.first][p.second]+1)
{
bear[xx][yy]=bear[p.first][p.second]+1;
q.push(make_pair(xx,yy));
}
}
}
return false;
}
signed main()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
read(n);read(s);
for(int i=1;i<=n;++i)
scanf("%s",mp[i]+1);
memset(bee,0x3f,sizeof(bee));
queue<pair<int,int>> q;
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
if(mp[i][j]=='H')
{
bee[i][j]=0;
q.push(make_pair(i,j));
}
while(!q.empty())
{
pair<int,int> p=q.front();
q.pop();
for(int d=0;d<4;++d)
{
int xx=p.first+dx[d],yy=p.second+dy[d];
if(xx>=1&&xx<=n&&yy>=1&&yy<=n&&(mp[xx][yy]=='G'||mp[xx][yy]=='M')&&bee[xx][yy]>bee[p.first][p.second]+s)
{
bee[xx][yy]=bee[p.first][p.second]+s;
q.push(make_pair(xx,yy));
}
}
}
int l=0,r=inf;
while(l<r)
{
int mid=l+r+1>>1;
if(check(mid)) l=mid;
else r=mid-1;
}
if(check(l)) writeln(l);
else puts("-1");
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:95:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
95 | int mid=l+r+1>>1;
| ~~~^~
mecho.cpp:68:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | scanf("%s",mp[i]+1);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
10484 KB |
Output is correct |
2 |
Correct |
18 ms |
10444 KB |
Output is correct |
3 |
Correct |
14 ms |
10452 KB |
Output is correct |
4 |
Correct |
14 ms |
10372 KB |
Output is correct |
5 |
Correct |
14 ms |
10444 KB |
Output is correct |
6 |
Correct |
14 ms |
10464 KB |
Output is correct |
7 |
Correct |
220 ms |
12452 KB |
Output is correct |
8 |
Correct |
15 ms |
10444 KB |
Output is correct |
9 |
Correct |
15 ms |
10468 KB |
Output is correct |
10 |
Correct |
12 ms |
10444 KB |
Output is correct |
11 |
Correct |
13 ms |
10444 KB |
Output is correct |
12 |
Correct |
14 ms |
10548 KB |
Output is correct |
13 |
Correct |
17 ms |
10536 KB |
Output is correct |
14 |
Correct |
18 ms |
10444 KB |
Output is correct |
15 |
Correct |
13 ms |
10444 KB |
Output is correct |
16 |
Correct |
17 ms |
10480 KB |
Output is correct |
17 |
Correct |
13 ms |
10484 KB |
Output is correct |
18 |
Correct |
15 ms |
10444 KB |
Output is correct |
19 |
Correct |
14 ms |
10444 KB |
Output is correct |
20 |
Correct |
14 ms |
10496 KB |
Output is correct |
21 |
Correct |
13 ms |
10512 KB |
Output is correct |
22 |
Correct |
13 ms |
10444 KB |
Output is correct |
23 |
Correct |
14 ms |
10444 KB |
Output is correct |
24 |
Correct |
14 ms |
10528 KB |
Output is correct |
25 |
Correct |
14 ms |
10544 KB |
Output is correct |
26 |
Correct |
15 ms |
10544 KB |
Output is correct |
27 |
Correct |
13 ms |
10552 KB |
Output is correct |
28 |
Correct |
13 ms |
10444 KB |
Output is correct |
29 |
Correct |
15 ms |
10444 KB |
Output is correct |
30 |
Correct |
13 ms |
10544 KB |
Output is correct |
31 |
Correct |
12 ms |
10444 KB |
Output is correct |
32 |
Correct |
16 ms |
10548 KB |
Output is correct |
33 |
Correct |
24 ms |
11108 KB |
Output is correct |
34 |
Correct |
18 ms |
11084 KB |
Output is correct |
35 |
Correct |
46 ms |
11084 KB |
Output is correct |
36 |
Correct |
18 ms |
11156 KB |
Output is correct |
37 |
Correct |
26 ms |
11212 KB |
Output is correct |
38 |
Correct |
72 ms |
11220 KB |
Output is correct |
39 |
Correct |
22 ms |
11340 KB |
Output is correct |
40 |
Correct |
31 ms |
11272 KB |
Output is correct |
41 |
Correct |
86 ms |
11340 KB |
Output is correct |
42 |
Correct |
25 ms |
11468 KB |
Output is correct |
43 |
Correct |
51 ms |
11424 KB |
Output is correct |
44 |
Correct |
108 ms |
11476 KB |
Output is correct |
45 |
Correct |
24 ms |
11492 KB |
Output is correct |
46 |
Correct |
27 ms |
11596 KB |
Output is correct |
47 |
Correct |
116 ms |
11588 KB |
Output is correct |
48 |
Correct |
25 ms |
11724 KB |
Output is correct |
49 |
Correct |
33 ms |
11724 KB |
Output is correct |
50 |
Correct |
138 ms |
11740 KB |
Output is correct |
51 |
Correct |
25 ms |
11852 KB |
Output is correct |
52 |
Correct |
32 ms |
11872 KB |
Output is correct |
53 |
Correct |
162 ms |
11852 KB |
Output is correct |
54 |
Correct |
29 ms |
11980 KB |
Output is correct |
55 |
Correct |
35 ms |
11948 KB |
Output is correct |
56 |
Correct |
209 ms |
11924 KB |
Output is correct |
57 |
Correct |
28 ms |
12108 KB |
Output is correct |
58 |
Correct |
35 ms |
12108 KB |
Output is correct |
59 |
Correct |
237 ms |
12228 KB |
Output is correct |
60 |
Correct |
35 ms |
12228 KB |
Output is correct |
61 |
Correct |
47 ms |
12252 KB |
Output is correct |
62 |
Correct |
252 ms |
12292 KB |
Output is correct |
63 |
Correct |
154 ms |
12364 KB |
Output is correct |
64 |
Correct |
246 ms |
12316 KB |
Output is correct |
65 |
Correct |
246 ms |
12380 KB |
Output is correct |
66 |
Correct |
188 ms |
12368 KB |
Output is correct |
67 |
Correct |
156 ms |
12316 KB |
Output is correct |
68 |
Correct |
114 ms |
12368 KB |
Output is correct |
69 |
Correct |
82 ms |
12440 KB |
Output is correct |
70 |
Correct |
76 ms |
12484 KB |
Output is correct |
71 |
Correct |
62 ms |
12308 KB |
Output is correct |
72 |
Correct |
58 ms |
12356 KB |
Output is correct |
73 |
Correct |
65 ms |
12748 KB |
Output is correct |
74 |
Correct |
101 ms |
12792 KB |
Output is correct |
75 |
Correct |
114 ms |
12864 KB |
Output is correct |
76 |
Correct |
123 ms |
12880 KB |
Output is correct |
77 |
Correct |
145 ms |
12868 KB |
Output is correct |
78 |
Correct |
133 ms |
12744 KB |
Output is correct |
79 |
Correct |
124 ms |
12808 KB |
Output is correct |
80 |
Correct |
119 ms |
12740 KB |
Output is correct |
81 |
Correct |
165 ms |
12724 KB |
Output is correct |
82 |
Correct |
146 ms |
12816 KB |
Output is correct |
83 |
Correct |
203 ms |
12668 KB |
Output is correct |
84 |
Correct |
170 ms |
12780 KB |
Output is correct |
85 |
Correct |
196 ms |
12740 KB |
Output is correct |
86 |
Correct |
207 ms |
12728 KB |
Output is correct |
87 |
Correct |
214 ms |
12780 KB |
Output is correct |
88 |
Correct |
175 ms |
12560 KB |
Output is correct |
89 |
Correct |
201 ms |
12632 KB |
Output is correct |
90 |
Correct |
231 ms |
12640 KB |
Output is correct |
91 |
Correct |
208 ms |
12632 KB |
Output is correct |
92 |
Correct |
186 ms |
12636 KB |
Output is correct |