#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
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' and matrica[x][y]!='D') return true;
else return false;
}
bool check2(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=0;i<=n;i++)
{
for (long long j=0;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 (check2(x+1,y) and dist2[x+1][y]==LLONG_MAX)
{
long long d=dist2[x][y]+1;
if (d<dist1[x+1][y]*s or dist1[x+1][y]==LLONG_MAX) dist2[x+1][y]=d;
else dist2[x+1][y]=-1;
if (dist2[x+1][y]!=-1) bfsq2.push({x+1,y});
}
if (check2(x-1,y) and dist2[x-1][y]==LLONG_MAX)
{
long long d=dist2[x][y]+1;
if (d<dist1[x-1][y]*s or dist1[x+1][y]==LLONG_MAX) dist2[x-1][y]=d;
else dist2[x-1][y]=-1;
if (dist2[x-1][y]!=-1) bfsq2.push({x-1,y});
}
if (check2(x,y+1) and dist2[x][y+1]==LLONG_MAX)
{
long long d=dist2[x][y]+1;
if (d<dist1[x][y+1]*s or dist1[x+1][y]==LLONG_MAX) dist2[x][y+1]=d;
else dist2[x][y+1]=-1;
if (dist2[x][y+1]!=-1) bfsq2.push({x,y+1});
}
if (check2(x,y-1) and dist2[x][y-1]==LLONG_MAX)
{
long long d=dist2[x][y]+1;
if (d<dist1[x][y-1]*s or dist1[x+1][y]==LLONG_MAX) dist2[x][y-1]=d;
else dist2[x][y-1]=-1;
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:135:23: warning: 'd' may be used uninitialized in this function [-Wmaybe-uninitialized]
135 | if (dist2[c][d]!=LLONG_MAX and dist2[c][d]!=-1) {rez=mid;l=mid+1;}
| ~~~~~~~~~~^
mecho.cpp:135:23: warning: 'c' may be used uninitialized in this function [-Wmaybe-uninitialized]
mecho.cpp:129:15: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
129 | long long r=dist1[a][b];
| ^
mecho.cpp:129:15: warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4564 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4560 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
51 ms |
12012 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
14 |
Correct |
1 ms |
6748 KB |
Output is correct |
15 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Incorrect |
1 ms |
6608 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
26 |
Correct |
1 ms |
6748 KB |
Output is correct |
27 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
6744 KB |
Output isn't correct |
30 |
Incorrect |
2 ms |
6748 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
32 |
Correct |
1 ms |
6748 KB |
Output is correct |
33 |
Incorrect |
5 ms |
9048 KB |
Output isn't correct |
34 |
Incorrect |
13 ms |
9052 KB |
Output isn't correct |
35 |
Incorrect |
14 ms |
9096 KB |
Output isn't correct |
36 |
Incorrect |
6 ms |
11096 KB |
Output isn't correct |
37 |
Correct |
6 ms |
11352 KB |
Output is correct |
38 |
Incorrect |
15 ms |
11352 KB |
Output isn't correct |
39 |
Incorrect |
7 ms |
11232 KB |
Output isn't correct |
40 |
Incorrect |
23 ms |
11100 KB |
Output isn't correct |
41 |
Incorrect |
19 ms |
11100 KB |
Output isn't correct |
42 |
Incorrect |
8 ms |
11352 KB |
Output isn't correct |
43 |
Correct |
8 ms |
11356 KB |
Output is correct |
44 |
Incorrect |
25 ms |
11344 KB |
Output isn't correct |
45 |
Incorrect |
10 ms |
11336 KB |
Output isn't correct |
46 |
Incorrect |
41 ms |
11356 KB |
Output isn't correct |
47 |
Incorrect |
41 ms |
11340 KB |
Output isn't correct |
48 |
Incorrect |
14 ms |
11356 KB |
Output isn't correct |
49 |
Correct |
11 ms |
11484 KB |
Output is correct |
50 |
Incorrect |
37 ms |
11356 KB |
Output isn't correct |
51 |
Incorrect |
13 ms |
11504 KB |
Output isn't correct |
52 |
Incorrect |
47 ms |
11612 KB |
Output isn't correct |
53 |
Incorrect |
47 ms |
11776 KB |
Output isn't correct |
54 |
Incorrect |
15 ms |
11592 KB |
Output isn't correct |
55 |
Correct |
17 ms |
11768 KB |
Output is correct |
56 |
Incorrect |
62 ms |
11760 KB |
Output isn't correct |
57 |
Incorrect |
18 ms |
11868 KB |
Output isn't correct |
58 |
Incorrect |
64 ms |
11868 KB |
Output isn't correct |
59 |
Incorrect |
60 ms |
11876 KB |
Output isn't correct |
60 |
Incorrect |
19 ms |
11868 KB |
Output isn't correct |
61 |
Correct |
19 ms |
11756 KB |
Output is correct |
62 |
Incorrect |
71 ms |
12016 KB |
Output isn't correct |
63 |
Correct |
66 ms |
11856 KB |
Output is correct |
64 |
Correct |
114 ms |
12116 KB |
Output is correct |
65 |
Correct |
129 ms |
11984 KB |
Output is correct |
66 |
Incorrect |
81 ms |
11864 KB |
Output isn't correct |
67 |
Correct |
71 ms |
11868 KB |
Output is correct |
68 |
Incorrect |
32 ms |
11860 KB |
Output isn't correct |
69 |
Incorrect |
38 ms |
11868 KB |
Output isn't correct |
70 |
Correct |
27 ms |
11888 KB |
Output is correct |
71 |
Correct |
24 ms |
12120 KB |
Output is correct |
72 |
Incorrect |
40 ms |
11864 KB |
Output isn't correct |
73 |
Incorrect |
24 ms |
12480 KB |
Output isn't correct |
74 |
Incorrect |
26 ms |
12504 KB |
Output isn't correct |
75 |
Incorrect |
28 ms |
12536 KB |
Output isn't correct |
76 |
Incorrect |
27 ms |
12380 KB |
Output isn't correct |
77 |
Incorrect |
28 ms |
12380 KB |
Output isn't correct |
78 |
Correct |
47 ms |
12432 KB |
Output is correct |
79 |
Correct |
48 ms |
12356 KB |
Output is correct |
80 |
Correct |
38 ms |
12380 KB |
Output is correct |
81 |
Correct |
41 ms |
12368 KB |
Output is correct |
82 |
Incorrect |
44 ms |
12372 KB |
Output isn't correct |
83 |
Incorrect |
50 ms |
12380 KB |
Output isn't correct |
84 |
Correct |
42 ms |
12368 KB |
Output is correct |
85 |
Incorrect |
44 ms |
12368 KB |
Output isn't correct |
86 |
Incorrect |
45 ms |
12380 KB |
Output isn't correct |
87 |
Correct |
41 ms |
12372 KB |
Output is correct |
88 |
Incorrect |
45 ms |
12304 KB |
Output isn't correct |
89 |
Correct |
47 ms |
12124 KB |
Output is correct |
90 |
Correct |
70 ms |
12304 KB |
Output is correct |
91 |
Incorrect |
65 ms |
12388 KB |
Output isn't correct |
92 |
Correct |
52 ms |
12116 KB |
Output is correct |