# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
897573 |
2024-01-03T12:35:21 Z |
ivaziva |
Mecho (IOI09_mecho) |
C++14 |
|
48 ms |
22812 KB |
#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;
if (x==c and y==d) break;
long long val=dist2[x][y];
long long z=val/s;
long long br;
if (z*s==val) br=dist1[x][y]-z-1;
else br=dist1[x][y]-z;
ans=min(ans,br);
dek.pop_front();
}
cout<<ans<<endl;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:136:18: warning: 'd' may be used uninitialized in this function [-Wmaybe-uninitialized]
136 | if (x==c and y==d) break;
| ~~~~~^~~~~~~~
mecho.cpp:136:9: warning: 'c' may be used uninitialized in this function [-Wmaybe-uninitialized]
136 | if (x==c and y==d) break;
| ^~
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]
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6592 KB |
Output is correct |
5 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
7 |
Incorrect |
46 ms |
22356 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
9 |
Correct |
1 ms |
8656 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Incorrect |
2 ms |
8540 KB |
Output isn't correct |
12 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
13 |
Correct |
2 ms |
10688 KB |
Output is correct |
14 |
Incorrect |
2 ms |
10684 KB |
Output isn't correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
17 |
Correct |
1 ms |
8540 KB |
Output is correct |
18 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
19 |
Correct |
1 ms |
10588 KB |
Output is correct |
20 |
Incorrect |
1 ms |
10588 KB |
Output isn't correct |
21 |
Correct |
1 ms |
10588 KB |
Output is correct |
22 |
Incorrect |
1 ms |
10588 KB |
Output isn't correct |
23 |
Correct |
2 ms |
10588 KB |
Output is correct |
24 |
Incorrect |
1 ms |
10588 KB |
Output isn't correct |
25 |
Correct |
2 ms |
10588 KB |
Output is correct |
26 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
27 |
Correct |
2 ms |
10588 KB |
Output is correct |
28 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
31 |
Correct |
2 ms |
10588 KB |
Output is correct |
32 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
33 |
Correct |
8 ms |
17100 KB |
Output is correct |
34 |
Incorrect |
8 ms |
19032 KB |
Output isn't correct |
35 |
Incorrect |
9 ms |
19036 KB |
Output isn't correct |
36 |
Correct |
9 ms |
17244 KB |
Output is correct |
37 |
Incorrect |
9 ms |
19220 KB |
Output isn't correct |
38 |
Incorrect |
13 ms |
19292 KB |
Output isn't correct |
39 |
Correct |
12 ms |
17364 KB |
Output is correct |
40 |
Incorrect |
12 ms |
19292 KB |
Output isn't correct |
41 |
Incorrect |
14 ms |
19292 KB |
Output isn't correct |
42 |
Correct |
13 ms |
17240 KB |
Output is correct |
43 |
Incorrect |
14 ms |
21336 KB |
Output isn't correct |
44 |
Incorrect |
17 ms |
21596 KB |
Output isn't correct |
45 |
Correct |
15 ms |
17496 KB |
Output is correct |
46 |
Incorrect |
16 ms |
21588 KB |
Output isn't correct |
47 |
Incorrect |
18 ms |
21596 KB |
Output isn't correct |
48 |
Correct |
18 ms |
17500 KB |
Output is correct |
49 |
Incorrect |
19 ms |
21640 KB |
Output isn't correct |
50 |
Incorrect |
27 ms |
21696 KB |
Output isn't correct |
51 |
Correct |
21 ms |
17756 KB |
Output is correct |
52 |
Incorrect |
25 ms |
21980 KB |
Output isn't correct |
53 |
Incorrect |
32 ms |
21852 KB |
Output isn't correct |
54 |
Correct |
24 ms |
19772 KB |
Output is correct |
55 |
Incorrect |
24 ms |
21852 KB |
Output isn't correct |
56 |
Incorrect |
30 ms |
21908 KB |
Output isn't correct |
57 |
Correct |
27 ms |
19792 KB |
Output is correct |
58 |
Incorrect |
27 ms |
21840 KB |
Output isn't correct |
59 |
Incorrect |
33 ms |
22080 KB |
Output isn't correct |
60 |
Correct |
30 ms |
20060 KB |
Output is correct |
61 |
Incorrect |
31 ms |
22064 KB |
Output isn't correct |
62 |
Incorrect |
40 ms |
22096 KB |
Output isn't correct |
63 |
Correct |
43 ms |
22096 KB |
Output is correct |
64 |
Correct |
46 ms |
22108 KB |
Output is correct |
65 |
Incorrect |
44 ms |
22096 KB |
Output isn't correct |
66 |
Correct |
44 ms |
22140 KB |
Output is correct |
67 |
Incorrect |
40 ms |
22100 KB |
Output isn't correct |
68 |
Incorrect |
37 ms |
22096 KB |
Output isn't correct |
69 |
Correct |
38 ms |
22052 KB |
Output is correct |
70 |
Incorrect |
37 ms |
22104 KB |
Output isn't correct |
71 |
Incorrect |
37 ms |
22100 KB |
Output isn't correct |
72 |
Correct |
42 ms |
22100 KB |
Output is correct |
73 |
Correct |
44 ms |
22812 KB |
Output is correct |
74 |
Incorrect |
48 ms |
22576 KB |
Output isn't correct |
75 |
Incorrect |
39 ms |
22612 KB |
Output isn't correct |
76 |
Incorrect |
36 ms |
22608 KB |
Output isn't correct |
77 |
Incorrect |
40 ms |
22608 KB |
Output isn't correct |
78 |
Incorrect |
37 ms |
18260 KB |
Output isn't correct |
79 |
Incorrect |
45 ms |
22612 KB |
Output isn't correct |
80 |
Incorrect |
40 ms |
18512 KB |
Output isn't correct |
81 |
Incorrect |
37 ms |
18256 KB |
Output isn't correct |
82 |
Incorrect |
37 ms |
18260 KB |
Output isn't correct |
83 |
Incorrect |
39 ms |
20336 KB |
Output isn't correct |
84 |
Incorrect |
48 ms |
22620 KB |
Output isn't correct |
85 |
Incorrect |
39 ms |
20564 KB |
Output isn't correct |
86 |
Incorrect |
40 ms |
20664 KB |
Output isn't correct |
87 |
Incorrect |
38 ms |
20456 KB |
Output isn't correct |
88 |
Incorrect |
38 ms |
20280 KB |
Output isn't correct |
89 |
Incorrect |
47 ms |
22352 KB |
Output isn't correct |
90 |
Incorrect |
41 ms |
22352 KB |
Output isn't correct |
91 |
Incorrect |
44 ms |
22352 KB |
Output isn't correct |
92 |
Incorrect |
38 ms |
20304 KB |
Output isn't correct |