#include <bits/stdc++.h>
using namespace std;
#define MAXN 810
long long n;
long long s;
char matrica[MAXN][MAXN];
long long dist1[MAXN][MAXN];///bee bfs
long long dist2[MAXN][MAXN];///mecho bfs
pair<long long,long long> parent[MAXN][MAXN];
queue<pair<long long,long long>> bfsq1;
queue<pair<long long,long long>> bfsq2;
bool check(long long x,long long y)
{
if (x>=1 and x<=n and y>=1 and y<=n and matrica[x][y]!='T') return true;
else return false;
}
void bfs1()
{
while (bfsq1.empty()==false)
{
long long x=bfsq1.front().first;
long long y=bfsq1.front().second;
bfsq1.pop();
if (check(x+1,y) and dist1[x+1][y]==LLONG_MAX)
{
dist1[x+1][y]=dist1[x][y]+1;
bfsq1.push({x+1,y});
}
if (check(x-1,y) and dist1[x-1][y]==LLONG_MAX)
{
dist1[x-1][y]=dist1[x][y]+1;
bfsq1.push({x-1,y});
}
if (check(x,y+1) and dist1[x][y+1]==LLONG_MAX)
{
dist1[x][y+1]=dist1[x][y]+1;
bfsq1.push({x,y+1});
}
if (check(x,y-1) and dist1[x][y-1]==LLONG_MAX)
{
dist1[x][y-1]=dist1[x][y]+1;
bfsq1.push({x,y-1});
}
}
}
void bfs2(long long a,long long b,long long mid)
{
for (long long i=1;i<=n;i++)
{
for (long long j=1;j<=n;j++) dist2[i][j]=LLONG_MAX;
}
dist2[a][b]=mid*s; bfsq2.push({a,b});
while (bfsq2.empty()==false)
{
long long x=bfsq2.front().first;
long long y=bfsq2.front().second;
bfsq2.pop();
if (check(x+1,y) and dist2[x+1][y]==LLONG_MAX)
{
long long d=dist2[x][y]+1;
if (d<dist1[x+1][y]*s) dist2[x+1][y]=d;
else dist2[x+1][y]=-1;
if (dist2[x+1][y]!=-1) parent[x+1][y]={x,y};
if (dist2[x+1][y]!=-1) bfsq2.push({x+1,y});
}
if (check(x-1,y) and dist2[x-1][y]==LLONG_MAX)
{
long long d=dist2[x][y]+1;
if (d<dist1[x-1][y]*s) dist2[x-1][y]=d;
else dist2[x-1][y]=-1;
if (dist2[x-1][y]!=-1) parent[x-1][y]={x,y};
if (dist2[x-1][y]!=-1) bfsq2.push({x-1,y});
}
if (check(x,y+1) and dist2[x][y+1]==LLONG_MAX)
{
long long d=dist2[x][y]+1;
if (d<dist1[x][y+1]*s) dist2[x][y+1]=d;
else dist2[x][y+1]=-1;
if (dist2[x][y+1]!=-1) parent[x][y+1]={x,y};
if (dist2[x][y+1]!=-1) bfsq2.push({x,y+1});
}
if (check(x,y-1) and dist2[x][y-1]==LLONG_MAX)
{
long long d=dist2[x][y]+1;
if (d<dist1[x][y-1]*s) dist2[x][y-1]=d;
else dist2[x][y-1]=-1;
if (dist2[x][y-1]!=-1) parent[x][y-1]={x,y};
if (dist2[x][y-1]!=-1) bfsq2.push({x,y-1});
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>s;
for (long long i=1;i<=n;i++)
{
for (long long j=1;j<=n;j++) dist1[i][j]=LLONG_MAX;
}
long long a,b;
long long c,d;
for (long long i=1;i<=n;i++)
{
for (long long j=1;j<=n;j++)
{
cin>>matrica[i][j];
if (matrica[i][j]=='H')
{
dist1[i][j]=0;
bfsq1.push({i,j});
}
if (matrica[i][j]=='M') {a=i;b=j;}
if (matrica[i][j]=='D') {c=i;d=j;}
}
}
bfs1();
long long l=0;
long long r=dist1[a][b];
long long rez=-1;
while (l<=r)
{
long long mid=(l+r)/2;
bfs2(a,b,mid);
if (dist2[c][d]!=LLONG_MAX and dist2[c][d]!=-1)
{
rez=mid;
l=mid+1;
}
else r=mid-1;
}
cout<<rez<<endl;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:134:23: warning: 'd' may be used uninitialized in this function [-Wmaybe-uninitialized]
134 | if (dist2[c][d]!=LLONG_MAX and dist2[c][d]!=-1)
| ~~~~~~~~~~^
mecho.cpp:134:23: warning: 'c' may be used uninitialized in this function [-Wmaybe-uninitialized]
mecho.cpp:128:15: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
128 | long long r=dist1[a][b];
| ^
mecho.cpp:128:15: warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
3 |
Correct |
1 ms |
6620 KB |
Output is correct |
4 |
Incorrect |
1 ms |
6488 KB |
Output isn't correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
7 |
Incorrect |
49 ms |
20240 KB |
Output isn't correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8664 KB |
Output is correct |
12 |
Incorrect |
1 ms |
10588 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
10588 KB |
Output isn't correct |
14 |
Correct |
1 ms |
10704 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
19 |
Correct |
1 ms |
8540 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
21 |
Correct |
1 ms |
8540 KB |
Output is correct |
22 |
Correct |
1 ms |
10588 KB |
Output is correct |
23 |
Correct |
2 ms |
10588 KB |
Output is correct |
24 |
Correct |
2 ms |
10588 KB |
Output is correct |
25 |
Correct |
2 ms |
10796 KB |
Output is correct |
26 |
Correct |
2 ms |
10588 KB |
Output is correct |
27 |
Correct |
1 ms |
10716 KB |
Output is correct |
28 |
Correct |
1 ms |
10588 KB |
Output is correct |
29 |
Correct |
1 ms |
10588 KB |
Output is correct |
30 |
Correct |
1 ms |
10584 KB |
Output is correct |
31 |
Correct |
2 ms |
10588 KB |
Output is correct |
32 |
Correct |
1 ms |
10588 KB |
Output is correct |
33 |
Correct |
6 ms |
14940 KB |
Output is correct |
34 |
Correct |
6 ms |
19024 KB |
Output is correct |
35 |
Correct |
14 ms |
19288 KB |
Output is correct |
36 |
Correct |
7 ms |
15192 KB |
Output is correct |
37 |
Correct |
7 ms |
16988 KB |
Output is correct |
38 |
Correct |
19 ms |
19372 KB |
Output is correct |
39 |
Correct |
9 ms |
15196 KB |
Output is correct |
40 |
Correct |
9 ms |
17244 KB |
Output is correct |
41 |
Correct |
22 ms |
19388 KB |
Output is correct |
42 |
Correct |
10 ms |
15196 KB |
Output is correct |
43 |
Correct |
10 ms |
19292 KB |
Output is correct |
44 |
Correct |
27 ms |
21596 KB |
Output is correct |
45 |
Correct |
12 ms |
15448 KB |
Output is correct |
46 |
Correct |
12 ms |
19292 KB |
Output is correct |
47 |
Correct |
33 ms |
21720 KB |
Output is correct |
48 |
Correct |
14 ms |
15452 KB |
Output is correct |
49 |
Correct |
14 ms |
19616 KB |
Output is correct |
50 |
Correct |
40 ms |
21792 KB |
Output is correct |
51 |
Correct |
16 ms |
15708 KB |
Output is correct |
52 |
Correct |
16 ms |
19708 KB |
Output is correct |
53 |
Correct |
47 ms |
21852 KB |
Output is correct |
54 |
Correct |
18 ms |
17756 KB |
Output is correct |
55 |
Correct |
18 ms |
17744 KB |
Output is correct |
56 |
Correct |
70 ms |
21852 KB |
Output is correct |
57 |
Correct |
20 ms |
17756 KB |
Output is correct |
58 |
Correct |
20 ms |
17872 KB |
Output is correct |
59 |
Correct |
70 ms |
21968 KB |
Output is correct |
60 |
Correct |
23 ms |
18012 KB |
Output is correct |
61 |
Correct |
23 ms |
17964 KB |
Output is correct |
62 |
Correct |
93 ms |
22196 KB |
Output is correct |
63 |
Correct |
82 ms |
22096 KB |
Output is correct |
64 |
Correct |
132 ms |
22232 KB |
Output is correct |
65 |
Correct |
126 ms |
22164 KB |
Output is correct |
66 |
Correct |
90 ms |
21936 KB |
Output is correct |
67 |
Correct |
79 ms |
22164 KB |
Output is correct |
68 |
Correct |
46 ms |
22480 KB |
Output is correct |
69 |
Correct |
32 ms |
22096 KB |
Output is correct |
70 |
Correct |
30 ms |
22100 KB |
Output is correct |
71 |
Correct |
26 ms |
22100 KB |
Output is correct |
72 |
Incorrect |
26 ms |
20056 KB |
Output isn't correct |
73 |
Incorrect |
29 ms |
22612 KB |
Output isn't correct |
74 |
Correct |
42 ms |
22584 KB |
Output is correct |
75 |
Correct |
39 ms |
22608 KB |
Output is correct |
76 |
Correct |
46 ms |
22712 KB |
Output is correct |
77 |
Correct |
44 ms |
22684 KB |
Output is correct |
78 |
Correct |
58 ms |
18472 KB |
Output is correct |
79 |
Correct |
39 ms |
18256 KB |
Output is correct |
80 |
Correct |
48 ms |
18468 KB |
Output is correct |
81 |
Correct |
44 ms |
18264 KB |
Output is correct |
82 |
Correct |
41 ms |
18488 KB |
Output is correct |
83 |
Correct |
73 ms |
20444 KB |
Output is correct |
84 |
Correct |
58 ms |
20564 KB |
Output is correct |
85 |
Correct |
55 ms |
20564 KB |
Output is correct |
86 |
Correct |
69 ms |
20560 KB |
Output is correct |
87 |
Correct |
49 ms |
20600 KB |
Output is correct |
88 |
Correct |
70 ms |
20532 KB |
Output is correct |
89 |
Correct |
62 ms |
20504 KB |
Output is correct |
90 |
Correct |
87 ms |
20504 KB |
Output is correct |
91 |
Correct |
71 ms |
20344 KB |
Output is correct |
92 |
Correct |
75 ms |
20504 KB |
Output is correct |