#include <bits/stdc++.h>
using namespace std;
#define ll long long
int b[4]={0,0,-1,1};
int c[4]={-1,1,0,0};
char mo[4]={'L','R','U','D'};
int main()
{
/* freopen("piggyback.in","r",stdin);
freopen("piggyback.out","w",stdout);*/
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n,m,k;
cin>>n>>k;
m=n;
ll enx,eny;
char a[n][m];
ll val[n][m];
queue<pair<ll,ll> >q;
ll x,y;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
val[i][j]=1e18;
cin>>a[i][j];
if(a[i][j]=='M')
{
x=i;
y=j;
}
if(a[i][j]=='D')
{
enx=i;
eny=j;
}
if(a[i][j]=='H')
{
q.push({i,j});
val[i][j]=1;
}
}
}
while(!q.empty())
{
int xx=q.front().first;
int yy=q.front().second;
q.pop();
for(int i=0;i<4;i++)
{
int xxx=xx+b[i];
int yyy=yy+c[i];
if(xxx<0||xxx>=n||yyy<0||yyy>=m)
continue;
if(val[xxx][yyy]==1e18&&a[xxx][yyy]!='T'&&a[xxx][yyy]!='D')
{
val[xxx][yyy]=val[xx][yy]+1;
q.push({xxx,yyy});
}
}
}
ll vall[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
vall[i][j]=0;
}
}
/*
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cout<<val[i][j]<<' ';
}
cout<<endl;
}*/
ll l=-1;
ll r=1e18;
ll jj=0;
while((l+1)<r)
{
jj=0;
q.push({x,y});
int ansx=-1,ansy=-1;
ll mi=(l+r)/2;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
vall[i][j]=0;
}
}
vall[x][y]=1;
if(mi>=(val[x][y]-1))
{
//cout<<1<<endl;
r=mi;
continue;
}
while(!q.empty())
{
int xx=q.front().first;
int yy=q.front().second;
/* if(l==0)
{
cout<<xx<<' '<<yy<<endl;
}*/
// cout<<xx<<' '<<yy<<endl;
q.pop();
if(xx==enx&&yy==eny)
{
jj=1;
break;
}
for(int i=0;i<4;i++)
{
int xxx=xx+b[i];
int yyy=yy+c[i];
if(xxx<0||xxx>=n||yyy<0||yyy>=n)
continue;
if(vall[xxx][yyy]==0&&a[xxx][yyy]!='T')
{
vall[xxx][yyy]=vall[xx][yy]+1;
if(xxx==enx&&yyy==eny)
{
jj=1;
break;
}
if((vall[xxx][yyy]-1)/k<val[xxx][yyy]-1-mi)
{
q.push({xxx,yyy});
}
}
}
}
if(jj==1)
{
l=mi;
}else
r=mi;
}
cout<<l<<endl;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:98:8: warning: unused variable 'ansx' [-Wunused-variable]
98 | int ansx=-1,ansy=-1;
| ^~~~
mecho.cpp:98:16: warning: unused variable 'ansy' [-Wunused-variable]
98 | int ansx=-1,ansy=-1;
| ^~~~
mecho.cpp:142:1: warning: 'enx' may be used uninitialized in this function [-Wmaybe-uninitialized]
142 | if(xxx==enx&&yyy==eny)
| ^~
mecho.cpp:108:20: warning: 'y' may be used uninitialized in this function [-Wmaybe-uninitialized]
108 | if(mi>=(val[x][y]-1))
| ~~~~~~~~^
mecho.cpp:142:12: warning: 'eny' may be used uninitialized in this function [-Wmaybe-uninitialized]
142 | if(xxx==enx&&yyy==eny)
| ~~~~~~~~^~~~~~~~~~
mecho.cpp:108:20: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
108 | if(mi>=(val[x][y]-1))
| ~~~~~~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
99 ms |
11168 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
6 ms |
2360 KB |
Output is correct |
34 |
Correct |
5 ms |
2260 KB |
Output is correct |
35 |
Correct |
20 ms |
2320 KB |
Output is correct |
36 |
Correct |
7 ms |
2880 KB |
Output is correct |
37 |
Correct |
7 ms |
2900 KB |
Output is correct |
38 |
Correct |
26 ms |
2900 KB |
Output is correct |
39 |
Correct |
9 ms |
3680 KB |
Output is correct |
40 |
Correct |
11 ms |
3632 KB |
Output is correct |
41 |
Correct |
33 ms |
3668 KB |
Output is correct |
42 |
Correct |
11 ms |
4440 KB |
Output is correct |
43 |
Correct |
11 ms |
4436 KB |
Output is correct |
44 |
Correct |
46 ms |
4436 KB |
Output is correct |
45 |
Correct |
16 ms |
5344 KB |
Output is correct |
46 |
Correct |
13 ms |
5352 KB |
Output is correct |
47 |
Correct |
54 ms |
5332 KB |
Output is correct |
48 |
Correct |
16 ms |
6228 KB |
Output is correct |
49 |
Correct |
16 ms |
6304 KB |
Output is correct |
50 |
Correct |
66 ms |
6300 KB |
Output is correct |
51 |
Correct |
20 ms |
7236 KB |
Output is correct |
52 |
Correct |
23 ms |
7228 KB |
Output is correct |
53 |
Correct |
79 ms |
7340 KB |
Output is correct |
54 |
Correct |
25 ms |
8404 KB |
Output is correct |
55 |
Correct |
23 ms |
8360 KB |
Output is correct |
56 |
Correct |
97 ms |
8404 KB |
Output is correct |
57 |
Correct |
26 ms |
9652 KB |
Output is correct |
58 |
Correct |
30 ms |
9556 KB |
Output is correct |
59 |
Correct |
112 ms |
9664 KB |
Output is correct |
60 |
Correct |
29 ms |
10836 KB |
Output is correct |
61 |
Correct |
29 ms |
10960 KB |
Output is correct |
62 |
Correct |
141 ms |
10968 KB |
Output is correct |
63 |
Correct |
126 ms |
10944 KB |
Output is correct |
64 |
Correct |
196 ms |
10948 KB |
Output is correct |
65 |
Correct |
172 ms |
10956 KB |
Output is correct |
66 |
Correct |
133 ms |
10944 KB |
Output is correct |
67 |
Correct |
118 ms |
10964 KB |
Output is correct |
68 |
Correct |
66 ms |
11004 KB |
Output is correct |
69 |
Correct |
45 ms |
11012 KB |
Output is correct |
70 |
Correct |
56 ms |
10988 KB |
Output is correct |
71 |
Correct |
51 ms |
10936 KB |
Output is correct |
72 |
Correct |
42 ms |
10988 KB |
Output is correct |
73 |
Correct |
58 ms |
11492 KB |
Output is correct |
74 |
Correct |
70 ms |
11444 KB |
Output is correct |
75 |
Correct |
74 ms |
11492 KB |
Output is correct |
76 |
Correct |
74 ms |
11476 KB |
Output is correct |
77 |
Correct |
76 ms |
11444 KB |
Output is correct |
78 |
Correct |
78 ms |
11348 KB |
Output is correct |
79 |
Correct |
72 ms |
11576 KB |
Output is correct |
80 |
Correct |
68 ms |
11320 KB |
Output is correct |
81 |
Correct |
74 ms |
11364 KB |
Output is correct |
82 |
Correct |
68 ms |
11340 KB |
Output is correct |
83 |
Correct |
87 ms |
11368 KB |
Output is correct |
84 |
Correct |
82 ms |
11364 KB |
Output is correct |
85 |
Correct |
84 ms |
11444 KB |
Output is correct |
86 |
Correct |
85 ms |
11364 KB |
Output is correct |
87 |
Correct |
80 ms |
11360 KB |
Output is correct |
88 |
Correct |
87 ms |
11268 KB |
Output is correct |
89 |
Correct |
85 ms |
11220 KB |
Output is correct |
90 |
Correct |
89 ms |
11284 KB |
Output is correct |
91 |
Correct |
86 ms |
11268 KB |
Output is correct |
92 |
Correct |
96 ms |
11212 KB |
Output is correct |