#include<bits/stdc++.h>
using namespace std;
int N, sx, sy, ex, ey;
const int MAXN = 4030303;
char arr[1010][1010];
int st;
vector<pair<int, int> > conn[MAXN];
bool visit[MAXN];
int tp;
int dijk()
{
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > Q;
Q.emplace(0, sx*N+sy);
while(!Q.empty())
{
int dist, no; tie(dist, no) = Q.top(); Q.pop();
//printf("%d\n", no);
if(no==ex*N+ey) return dist;
if(visit[no]) continue;
visit[no] = true;
for(auto x: conn[no])
{
int targ, w;
tie(targ, w) = x;
Q.emplace(w+dist, targ);
}
}
return -1;
}
int main()
{
scanf("%d", &N); tp = N*N+N+1;
for(int i=1; i<=N; ++i)
scanf("%s", arr[i]+1);
for(int i=1; i<=N; ++i)
for(int j=1; j<=N; ++j)
{
if(arr[i][j] == 'M')
{
tie(sx, sy) = tie(i, j);
arr[i][j] = '.';
}
if(arr[i][j] == 'H')
{
tie(ex, ey) = tie(i, j);
arr[i][j] = '.';
}
}
for(int i=1; i<=N; ++i)
{
int cnt = -1;
vector<int> inserted;
vector<int> weight;
for(int j=0; j<=N+1; ++j)
{
if(arr[i][j] == '.')
{
if(arr[i][j-1] != '.')
{
weight.push_back(cnt);
inserted.push_back(tp);
}
conn[tp].emplace_back(i*N+j, 0);
conn[i*N+j].emplace_back(tp+1, 0);
}
else
{
if(j!=0 && arr[i][j-1] == '.')
cnt = 0, tp += 2;
++cnt;
}
}
for(int i=0; i<(int)inserted.size()-1; ++i)
{
int ptp = inserted[i], ntp = inserted[i+1], w = weight[i+1]*weight[i+1];
conn[ptp+1].emplace_back(ntp, w);
conn[ntp+1].emplace_back(ptp, w);
}
}
for(int j=1; j<=N; ++j)
{
int cnt = -1;
vector<int> inserted, weight;
for(int i=0; i<=N+1; ++i)
{
if(arr[i][j] == '.')
{
if(arr[i-1][j] != '.')
{
weight.push_back(cnt);
inserted.push_back(tp);
}
conn[tp].emplace_back(i*N+j, 0);
conn[i*N+j].emplace_back(tp+1, 0);
}
else
{
if(i!=0 && arr[i-1][j] == '.')
cnt=0, tp += 2;
++cnt;
}
}
for(int i=0; i<(int)inserted.size()-1; ++i)
{
int ptp = inserted[i], ntp = inserted[i+1], w = weight[i+1]*weight[i+1];
conn[ptp+1].emplace_back(ntp, w);
conn[ntp+1].emplace_back(ptp, w);
}
}
printf("%d\n", dijk());
}
Compilation message
aqua.cpp: In function 'int main()':
aqua.cpp:35:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N); tp = N*N+N+1;
~~~~~^~~~~~~~~~
aqua.cpp:37:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", arr[i]+1);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
95116 KB |
Output is correct |
2 |
Correct |
87 ms |
95116 KB |
Output is correct |
3 |
Correct |
89 ms |
95116 KB |
Output is correct |
4 |
Correct |
87 ms |
95276 KB |
Output is correct |
5 |
Correct |
88 ms |
95732 KB |
Output is correct |
6 |
Correct |
85 ms |
95732 KB |
Output is correct |
7 |
Correct |
105 ms |
95928 KB |
Output is correct |
8 |
Correct |
97 ms |
95980 KB |
Output is correct |
9 |
Correct |
99 ms |
96176 KB |
Output is correct |
10 |
Correct |
86 ms |
96176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
95116 KB |
Output is correct |
2 |
Correct |
87 ms |
95116 KB |
Output is correct |
3 |
Correct |
89 ms |
95116 KB |
Output is correct |
4 |
Correct |
87 ms |
95276 KB |
Output is correct |
5 |
Correct |
88 ms |
95732 KB |
Output is correct |
6 |
Correct |
85 ms |
95732 KB |
Output is correct |
7 |
Correct |
105 ms |
95928 KB |
Output is correct |
8 |
Correct |
97 ms |
95980 KB |
Output is correct |
9 |
Correct |
99 ms |
96176 KB |
Output is correct |
10 |
Correct |
86 ms |
96176 KB |
Output is correct |
11 |
Correct |
164 ms |
101436 KB |
Output is correct |
12 |
Correct |
432 ms |
124260 KB |
Output is correct |
13 |
Correct |
849 ms |
125836 KB |
Output is correct |
14 |
Correct |
287 ms |
125836 KB |
Output is correct |
15 |
Correct |
236 ms |
130156 KB |
Output is correct |
16 |
Correct |
730 ms |
138172 KB |
Output is correct |
17 |
Correct |
1162 ms |
138696 KB |
Output is correct |
18 |
Correct |
892 ms |
161952 KB |
Output is correct |
19 |
Correct |
753 ms |
161952 KB |
Output is correct |
20 |
Correct |
107 ms |
161952 KB |
Output is correct |