#include <bits/stdc++.h>
#define inf 100000000000000007
#define mod 1000000007
#define rnd() dist(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 seed=std::chrono::system_clock::now().time_since_epoch().count();
mt19937 rand_num(seed);
uniform_int_distribution<int> dist(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][1005];
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]!='T'&&mp[xx][yy]!='H'&&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=5000000;
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:94:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
94 | int mid=l+r+1>>1;
| ~~~^~
mecho.cpp:67:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | scanf("%s",mp[i]+1);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
10448 KB |
Output is correct |
2 |
Correct |
12 ms |
10448 KB |
Output is correct |
3 |
Correct |
12 ms |
10332 KB |
Output is correct |
4 |
Correct |
12 ms |
10444 KB |
Output is correct |
5 |
Correct |
11 ms |
10444 KB |
Output is correct |
6 |
Correct |
12 ms |
10444 KB |
Output is correct |
7 |
Correct |
233 ms |
11436 KB |
Output is correct |
8 |
Correct |
12 ms |
10444 KB |
Output is correct |
9 |
Correct |
13 ms |
10456 KB |
Output is correct |
10 |
Correct |
13 ms |
10444 KB |
Output is correct |
11 |
Correct |
13 ms |
10464 KB |
Output is correct |
12 |
Correct |
20 ms |
10444 KB |
Output is correct |
13 |
Correct |
12 ms |
10444 KB |
Output is correct |
14 |
Correct |
14 ms |
10508 KB |
Output is correct |
15 |
Correct |
20 ms |
10460 KB |
Output is correct |
16 |
Correct |
13 ms |
10444 KB |
Output is correct |
17 |
Correct |
13 ms |
10472 KB |
Output is correct |
18 |
Correct |
13 ms |
10572 KB |
Output is correct |
19 |
Correct |
11 ms |
10444 KB |
Output is correct |
20 |
Correct |
12 ms |
10444 KB |
Output is correct |
21 |
Correct |
11 ms |
10444 KB |
Output is correct |
22 |
Correct |
12 ms |
10480 KB |
Output is correct |
23 |
Correct |
13 ms |
10444 KB |
Output is correct |
24 |
Correct |
14 ms |
10444 KB |
Output is correct |
25 |
Correct |
12 ms |
10444 KB |
Output is correct |
26 |
Correct |
11 ms |
10508 KB |
Output is correct |
27 |
Correct |
13 ms |
10508 KB |
Output is correct |
28 |
Correct |
12 ms |
10512 KB |
Output is correct |
29 |
Correct |
12 ms |
10512 KB |
Output is correct |
30 |
Correct |
14 ms |
10444 KB |
Output is correct |
31 |
Correct |
13 ms |
10460 KB |
Output is correct |
32 |
Correct |
13 ms |
10444 KB |
Output is correct |
33 |
Correct |
18 ms |
10700 KB |
Output is correct |
34 |
Correct |
17 ms |
10792 KB |
Output is correct |
35 |
Correct |
46 ms |
10700 KB |
Output is correct |
36 |
Correct |
18 ms |
10832 KB |
Output is correct |
37 |
Correct |
22 ms |
10828 KB |
Output is correct |
38 |
Correct |
64 ms |
10820 KB |
Output is correct |
39 |
Correct |
18 ms |
10888 KB |
Output is correct |
40 |
Correct |
25 ms |
10828 KB |
Output is correct |
41 |
Correct |
80 ms |
10892 KB |
Output is correct |
42 |
Correct |
21 ms |
10828 KB |
Output is correct |
43 |
Correct |
25 ms |
10936 KB |
Output is correct |
44 |
Correct |
97 ms |
10940 KB |
Output is correct |
45 |
Correct |
26 ms |
10984 KB |
Output is correct |
46 |
Correct |
31 ms |
10956 KB |
Output is correct |
47 |
Correct |
118 ms |
10980 KB |
Output is correct |
48 |
Correct |
22 ms |
10964 KB |
Output is correct |
49 |
Correct |
25 ms |
10956 KB |
Output is correct |
50 |
Correct |
170 ms |
10980 KB |
Output is correct |
51 |
Correct |
23 ms |
11084 KB |
Output is correct |
52 |
Correct |
27 ms |
11084 KB |
Output is correct |
53 |
Correct |
152 ms |
11084 KB |
Output is correct |
54 |
Correct |
35 ms |
11116 KB |
Output is correct |
55 |
Correct |
39 ms |
11084 KB |
Output is correct |
56 |
Correct |
200 ms |
11204 KB |
Output is correct |
57 |
Correct |
28 ms |
11176 KB |
Output is correct |
58 |
Correct |
32 ms |
11160 KB |
Output is correct |
59 |
Correct |
235 ms |
11240 KB |
Output is correct |
60 |
Correct |
28 ms |
11232 KB |
Output is correct |
61 |
Correct |
33 ms |
11232 KB |
Output is correct |
62 |
Correct |
238 ms |
11192 KB |
Output is correct |
63 |
Correct |
169 ms |
11220 KB |
Output is correct |
64 |
Correct |
221 ms |
11212 KB |
Output is correct |
65 |
Correct |
249 ms |
11168 KB |
Output is correct |
66 |
Correct |
177 ms |
11204 KB |
Output is correct |
67 |
Correct |
162 ms |
11160 KB |
Output is correct |
68 |
Correct |
78 ms |
11280 KB |
Output is correct |
69 |
Correct |
74 ms |
11196 KB |
Output is correct |
70 |
Correct |
74 ms |
11220 KB |
Output is correct |
71 |
Correct |
79 ms |
11260 KB |
Output is correct |
72 |
Correct |
52 ms |
11156 KB |
Output is correct |
73 |
Correct |
64 ms |
11716 KB |
Output is correct |
74 |
Correct |
92 ms |
11708 KB |
Output is correct |
75 |
Correct |
110 ms |
11712 KB |
Output is correct |
76 |
Correct |
133 ms |
11776 KB |
Output is correct |
77 |
Correct |
96 ms |
11780 KB |
Output is correct |
78 |
Correct |
142 ms |
11716 KB |
Output is correct |
79 |
Correct |
92 ms |
11712 KB |
Output is correct |
80 |
Correct |
108 ms |
11648 KB |
Output is correct |
81 |
Correct |
125 ms |
11704 KB |
Output is correct |
82 |
Correct |
110 ms |
11716 KB |
Output is correct |
83 |
Correct |
188 ms |
11632 KB |
Output is correct |
84 |
Correct |
140 ms |
11588 KB |
Output is correct |
85 |
Correct |
166 ms |
11636 KB |
Output is correct |
86 |
Correct |
162 ms |
11572 KB |
Output is correct |
87 |
Correct |
197 ms |
11636 KB |
Output is correct |
88 |
Correct |
179 ms |
11540 KB |
Output is correct |
89 |
Correct |
255 ms |
11484 KB |
Output is correct |
90 |
Correct |
171 ms |
11460 KB |
Output is correct |
91 |
Correct |
191 ms |
11544 KB |
Output is correct |
92 |
Correct |
186 ms |
11600 KB |
Output is correct |