#include<stdio.h>
#include<vector>
#include<queue>
using namespace std;
typedef long long lld;
struct dd {
int ix; lld v;
dd(int ix_, lld v_){ ix=ix_, v=v_; }
bool operator< (const dd& c) const {
return v>c.v;
}
};
int N, nod[910][910], vcn, st, en;
char mz[910][910];
vector<int> con[910101], val[910101];
priority_queue<dd> pq;
lld dist[910101];
int sq(int a){ return a*a; }
void edge(int s, int e, lld v){
con[s].push_back(e), val[s].push_back(v);
}
int main(){
scanf("%d", &N);
for(int i=0; i<N; i++){
scanf("\n%s", mz[i]);
for(int j=0; j<N; j++){
if(mz[i][j]=='W') continue;
if(mz[i][j]=='M') st=vcn, mz[i][j]='.';
if(mz[i][j]=='H') en=vcn, mz[i][j]='.';
nod[i][j]=vcn++;
}
}
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
if(mz[i][j]=='W') continue;
int ty=0, wcn=0;
for(int k=j; k>=0; k--){
if(mz[i][k]=='W') wcn++;
else if(ty==2) edge(nod[i][j], nod[i][k], sq(wcn));
if(k && mz[i][k]!=mz[i][k-1]) ty++;
if(ty>2) break;
}
ty=0, wcn=0;
for(int k=j; k<N; k++){
if(mz[i][k]=='W') wcn++;
else if(ty==2) edge(nod[i][j], nod[i][k], sq(wcn));
if(k<N-1 && mz[i][k]!=mz[i][k+1]) ty++;
if(ty>2) break;
}
ty=0, wcn=0;
for(int k=i; k>=0; k--){
if(mz[k][j]=='W') wcn++;
else if(ty==2) edge(nod[i][j], nod[k][j], sq(wcn));
if(k && mz[k][j]!=mz[k-1][j]) ty++;
if(ty>2) break;
}
ty=0, wcn=0;
for(int k=i; k<N; k++){
if(mz[k][j]=='W') wcn++;
else if(ty==2) edge(nod[i][j], nod[k][j], sq(wcn));
if(k<N-1 && mz[k][j]!=mz[k+1][j]) ty++;
if(ty>2) break;
}
}
}
for(int i=0; i<vcn; i++) dist[i]=-1;
dist[st]=0, pq.push(dd(st,0));
while(!pq.empty()){
dd x=pq.top(); pq.pop();
if(x.v != dist[x.ix]) continue;
int s=x.ix;
for(int i=con[s].size(); i--;){
int e=con[s][i]; lld v=val[s][i];
if(dist[e]<0 || dist[e]>x.v+v) dist[e]=x.v+v, pq.push(dd(e,x.v+v));
}
}
printf("%lld\n", dist[en]);
return 0;
}
Compilation message
aqua.cpp: In function 'int main()':
aqua.cpp:29:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
aqua.cpp:31:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("\n%s", mz[i]);
~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
43000 KB |
Output is correct |
2 |
Correct |
40 ms |
43236 KB |
Output is correct |
3 |
Correct |
39 ms |
43432 KB |
Output is correct |
4 |
Correct |
39 ms |
43632 KB |
Output is correct |
5 |
Correct |
41 ms |
43632 KB |
Output is correct |
6 |
Correct |
41 ms |
43892 KB |
Output is correct |
7 |
Correct |
45 ms |
44280 KB |
Output is correct |
8 |
Correct |
47 ms |
44404 KB |
Output is correct |
9 |
Correct |
44 ms |
44404 KB |
Output is correct |
10 |
Correct |
40 ms |
44404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
43000 KB |
Output is correct |
2 |
Correct |
40 ms |
43236 KB |
Output is correct |
3 |
Correct |
39 ms |
43432 KB |
Output is correct |
4 |
Correct |
39 ms |
43632 KB |
Output is correct |
5 |
Correct |
41 ms |
43632 KB |
Output is correct |
6 |
Correct |
41 ms |
43892 KB |
Output is correct |
7 |
Correct |
45 ms |
44280 KB |
Output is correct |
8 |
Correct |
47 ms |
44404 KB |
Output is correct |
9 |
Correct |
44 ms |
44404 KB |
Output is correct |
10 |
Correct |
40 ms |
44404 KB |
Output is correct |
11 |
Correct |
106 ms |
51820 KB |
Output is correct |
12 |
Correct |
143 ms |
58236 KB |
Output is correct |
13 |
Correct |
426 ms |
82036 KB |
Output is correct |
14 |
Correct |
253 ms |
82036 KB |
Output is correct |
15 |
Execution timed out |
3039 ms |
82036 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |