#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)
{
dist2[a][b]=0; 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()
{
cin>>n>>s;
for (long long i=1;i<=n;i++)
{
for (long long j=1;j<=n;j++)
{
dist1[i][j]=LLONG_MAX;
dist2[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(); bfs2(a,b);
deque<pair<long long,long long>> dek;
pair<long long,long long> tren={c,d};
dek.push_front(tren);
while (parent[tren.first][tren.second].first!=0)
{
pair<long long,long long> par=parent[tren.first][tren.second];
dek.push_front(par); tren=par;
}
long long ans=LLONG_MAX;
while (dek.empty()==false)
{
long long x=dek.front().first;
long long y=dek.front().second;
long long val=dist2[x][y];
long long z=val/s;
long long br;
if (z*s==val) br=dist1[x][y]-z;
else br=dist1[x][y]-z-1;
ans=min(ans,br);
dek.pop_front();
}
cout<<ans<<endl;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:122:17: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
122 | bfs1(); bfs2(a,b);
| ~~~~^~~~~
mecho.cpp:122:17: warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
6588 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
6592 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
6744 KB |
Output isn't correct |
5 |
Correct |
1 ms |
6588 KB |
Output is correct |
6 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
7 |
Incorrect |
43 ms |
22204 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
12 |
Incorrect |
1 ms |
10588 KB |
Output isn't correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Incorrect |
2 ms |
10584 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
6488 KB |
Output isn't correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
19 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
21 |
Incorrect |
2 ms |
10684 KB |
Output isn't correct |
22 |
Correct |
2 ms |
10588 KB |
Output is correct |
23 |
Incorrect |
1 ms |
10588 KB |
Output isn't correct |
24 |
Correct |
1 ms |
10588 KB |
Output is correct |
25 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
26 |
Correct |
1 ms |
10588 KB |
Output is correct |
27 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
30 |
Correct |
2 ms |
10712 KB |
Output is correct |
31 |
Incorrect |
2 ms |
10584 KB |
Output isn't correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Incorrect |
9 ms |
16972 KB |
Output isn't correct |
34 |
Correct |
8 ms |
19036 KB |
Output is correct |
35 |
Incorrect |
13 ms |
19036 KB |
Output isn't correct |
36 |
Incorrect |
13 ms |
17240 KB |
Output isn't correct |
37 |
Correct |
9 ms |
19356 KB |
Output is correct |
38 |
Incorrect |
11 ms |
19152 KB |
Output isn't correct |
39 |
Incorrect |
12 ms |
17240 KB |
Output isn't correct |
40 |
Correct |
11 ms |
19292 KB |
Output is correct |
41 |
Incorrect |
13 ms |
19292 KB |
Output isn't correct |
42 |
Incorrect |
13 ms |
17244 KB |
Output isn't correct |
43 |
Correct |
14 ms |
21336 KB |
Output is correct |
44 |
Incorrect |
29 ms |
21584 KB |
Output isn't correct |
45 |
Incorrect |
15 ms |
17500 KB |
Output isn't correct |
46 |
Correct |
16 ms |
21440 KB |
Output is correct |
47 |
Incorrect |
18 ms |
21592 KB |
Output isn't correct |
48 |
Incorrect |
24 ms |
17500 KB |
Output isn't correct |
49 |
Correct |
19 ms |
21636 KB |
Output is correct |
50 |
Incorrect |
23 ms |
21592 KB |
Output isn't correct |
51 |
Incorrect |
21 ms |
17624 KB |
Output isn't correct |
52 |
Correct |
32 ms |
21688 KB |
Output is correct |
53 |
Incorrect |
31 ms |
21716 KB |
Output isn't correct |
54 |
Incorrect |
24 ms |
19804 KB |
Output isn't correct |
55 |
Correct |
24 ms |
21832 KB |
Output is correct |
56 |
Incorrect |
30 ms |
21948 KB |
Output isn't correct |
57 |
Incorrect |
38 ms |
19788 KB |
Output isn't correct |
58 |
Correct |
28 ms |
21852 KB |
Output is correct |
59 |
Incorrect |
35 ms |
21872 KB |
Output isn't correct |
60 |
Incorrect |
32 ms |
20048 KB |
Output isn't correct |
61 |
Correct |
31 ms |
22060 KB |
Output is correct |
62 |
Incorrect |
44 ms |
22100 KB |
Output isn't correct |
63 |
Correct |
46 ms |
22096 KB |
Output is correct |
64 |
Correct |
47 ms |
22092 KB |
Output is correct |
65 |
Correct |
45 ms |
22060 KB |
Output is correct |
66 |
Incorrect |
58 ms |
22040 KB |
Output isn't correct |
67 |
Incorrect |
56 ms |
22096 KB |
Output isn't correct |
68 |
Correct |
39 ms |
22100 KB |
Output is correct |
69 |
Correct |
39 ms |
22116 KB |
Output is correct |
70 |
Correct |
55 ms |
22096 KB |
Output is correct |
71 |
Correct |
44 ms |
22076 KB |
Output is correct |
72 |
Incorrect |
57 ms |
22116 KB |
Output isn't correct |
73 |
Incorrect |
38 ms |
22620 KB |
Output isn't correct |
74 |
Correct |
59 ms |
22564 KB |
Output is correct |
75 |
Correct |
38 ms |
22612 KB |
Output is correct |
76 |
Correct |
36 ms |
22728 KB |
Output is correct |
77 |
Correct |
53 ms |
22612 KB |
Output is correct |
78 |
Incorrect |
38 ms |
18256 KB |
Output isn't correct |
79 |
Incorrect |
45 ms |
22612 KB |
Output isn't correct |
80 |
Incorrect |
36 ms |
18356 KB |
Output isn't correct |
81 |
Incorrect |
39 ms |
18264 KB |
Output isn't correct |
82 |
Incorrect |
39 ms |
18256 KB |
Output isn't correct |
83 |
Correct |
39 ms |
20564 KB |
Output is correct |
84 |
Incorrect |
50 ms |
22608 KB |
Output isn't correct |
85 |
Incorrect |
38 ms |
20536 KB |
Output isn't correct |
86 |
Incorrect |
54 ms |
20544 KB |
Output isn't correct |
87 |
Incorrect |
50 ms |
20564 KB |
Output isn't correct |
88 |
Correct |
42 ms |
20304 KB |
Output is correct |
89 |
Incorrect |
46 ms |
22532 KB |
Output isn't correct |
90 |
Incorrect |
45 ms |
22460 KB |
Output isn't correct |
91 |
Incorrect |
51 ms |
22528 KB |
Output isn't correct |
92 |
Incorrect |
40 ms |
20468 KB |
Output isn't correct |