//#pragma GCC optimize("-O3")
#include<bits/stdc++.h>
using namespace std;
int N,M,er,ec,MP[900][900];
struct A;
priority_queue<A,vector<A>,greater<A> >PQ;
struct A
{
int CS,r,c;
bool operator>(const A&Tt)const{return CS>Tt.CS;}
void FF(int dr,int dc)const
{
int rr=r+dr,cc=c+dc,le=0,mini=CS+1;
for(;0<=rr&&rr<N&&0<=cc&&cc<N&&MP[rr][cc]!=-1;rr+=dr,cc+=dc)mini=min(mini,MP[rr][cc]);
if(mini<=CS)return;
for(;0<=rr&&rr<N&&0<=cc&&cc<N&&MP[rr][cc]==-1;le++,rr+=dr,cc+=dc);
for(le=CS+le*le;0<=rr&&rr<N&&0<=cc&&cc<N&&MP[rr][cc]!=-1;rr+=dr,cc+=dc)
if(MP[rr][cc]>le)
{
// printf("uped %d %d - > %d %d :: %d -> %d\n",r,c,rr,cc,MP[rr][cc],le);
PQ.push({MP[rr][cc]=le,rr,cc});
}
}
};
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin>>N;
string S;
for(int i=0;i<N;i++)
{
cin>>S;
for(int j=0;j<N;j++)
{
MP[i][j]=(S[j]=='W'?-1:1<<30);
if(S[j]=='M')
PQ.push({MP[i][j]=0,i,j});
else if(S[j]=='H')
er=i,ec=j;
}
}
while(!PQ.empty())
{
A TT=PQ.top();PQ.pop();
if(TT.r==er&&TT.c==ec){printf("%d",TT.CS);return 0;}
// printf("%d %d :: %d & %d\n",TT.r,TT.c,MP[TT.r][TT.c],TT.CS);
if(TT.CS==MP[TT.r][TT.c])
{
TT.FF(1,0);
TT.FF(-1,0);
TT.FF(0,1);
TT.FF(0,-1);
}
}
printf("-1 ");
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
520 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
852 KB |
Output is correct |
6 |
Correct |
3 ms |
928 KB |
Output is correct |
7 |
Correct |
3 ms |
1004 KB |
Output is correct |
8 |
Correct |
4 ms |
1004 KB |
Output is correct |
9 |
Correct |
2 ms |
1004 KB |
Output is correct |
10 |
Correct |
2 ms |
1004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
520 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
852 KB |
Output is correct |
6 |
Correct |
3 ms |
928 KB |
Output is correct |
7 |
Correct |
3 ms |
1004 KB |
Output is correct |
8 |
Correct |
4 ms |
1004 KB |
Output is correct |
9 |
Correct |
2 ms |
1004 KB |
Output is correct |
10 |
Correct |
2 ms |
1004 KB |
Output is correct |
11 |
Correct |
13 ms |
1956 KB |
Output is correct |
12 |
Correct |
15 ms |
2668 KB |
Output is correct |
13 |
Correct |
113 ms |
3628 KB |
Output is correct |
14 |
Correct |
9 ms |
3628 KB |
Output is correct |
15 |
Correct |
6 ms |
3628 KB |
Output is correct |
16 |
Correct |
69 ms |
4216 KB |
Output is correct |
17 |
Correct |
153 ms |
4216 KB |
Output is correct |
18 |
Correct |
29 ms |
4216 KB |
Output is correct |
19 |
Correct |
51 ms |
4216 KB |
Output is correct |
20 |
Correct |
11 ms |
4216 KB |
Output is correct |