#include <bits/stdc++.h>
using namespace std;
vector<pair<int,long long>>g[1000000];
int currentnode(int i,int j,int column)
{
return i*(column+2)+j+1;
}
vector<long long>shortest_path(int source)
{
vector<long long>answer(1000000,1000000000000000000);
priority_queue<pair<long long,int>,vector<pair<long long,int>>,greater<pair<long long,int>>>pq;
vector<int>used(1000000,0);
pq.push({0,source});
while(pq.size())
{
//cout<<"A"<<endl;
int node=pq.top().second;
long long cost=pq.top().first;
pq.pop();
if(used[node])continue;
used[node]=1;
answer[node]=cost;
for(pair<int,long long>go:g[node])
{
if(used[go.first])continue;
pq.push({go.second+cost,go.first});
}
}
return answer;
}
int main()
{
int n,m;
cin>>m>>n;
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
char a;
cin>>a;
//cout<<"currentnode "<<currentnode(i,j,n)<<endl;
if(a=='W')
{
g[currentnode(i,j,n)].push_back({currentnode(i,j,n)-1,0});
g[currentnode(i,j,n)].push_back({currentnode(i,j,n)-n-2,1});
g[currentnode(i,j,n)].push_back({currentnode(i,j,n)+1,2});
g[currentnode(i,j,n)].push_back({currentnode(i,j,n)+n+2,3});
}
else if(a=='E')
{
g[currentnode(i,j,n)].push_back({currentnode(i,j,n)+1,0});
g[currentnode(i,j,n)].push_back({currentnode(i,j,n)+n+2,1});
g[currentnode(i,j,n)].push_back({currentnode(i,j,n)-1,2});
g[currentnode(i,j,n)].push_back({currentnode(i,j,n)-n-2,3});
}
else if(a=='S')
{
g[currentnode(i,j,n)].push_back({currentnode(i,j,n)+n+2,0});
g[currentnode(i,j,n)].push_back({currentnode(i,j,n)-1,1});
g[currentnode(i,j,n)].push_back({currentnode(i,j,n)-n-2,2});
g[currentnode(i,j,n)].push_back({currentnode(i,j,n)+1,3});
}
else if(a=='N')
{
g[currentnode(i,j,n)].push_back({currentnode(i,j,n)-n-2,0});
g[currentnode(i,j,n)].push_back({currentnode(i,j,n)+1,1});
g[currentnode(i,j,n)].push_back({currentnode(i,j,n)+n+2,2});
g[currentnode(i,j,n)].push_back({currentnode(i,j,n)-1,3});
}
}
vector<long long>dis=shortest_path(n+4);
//for(int i=1;i<=(n+2)*(m+2);i++)cout<<dis[i]<<" ";cout<<endl;
if(dis[(n+2)*(m+2)-n-3]==1000000000000000000)cout<<-1;
else cout<<dis[(n+2)*(m+2)-n-3];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
35700 KB |
Output is correct |
2 |
Correct |
10 ms |
35676 KB |
Output is correct |
3 |
Correct |
8 ms |
35676 KB |
Output is correct |
4 |
Correct |
9 ms |
35692 KB |
Output is correct |
5 |
Correct |
9 ms |
35676 KB |
Output is correct |
6 |
Correct |
8 ms |
35696 KB |
Output is correct |
7 |
Correct |
8 ms |
35532 KB |
Output is correct |
8 |
Correct |
8 ms |
35676 KB |
Output is correct |
9 |
Correct |
8 ms |
35676 KB |
Output is correct |
10 |
Correct |
9 ms |
35676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
35700 KB |
Output is correct |
2 |
Correct |
10 ms |
35676 KB |
Output is correct |
3 |
Correct |
8 ms |
35676 KB |
Output is correct |
4 |
Correct |
9 ms |
35692 KB |
Output is correct |
5 |
Correct |
9 ms |
35676 KB |
Output is correct |
6 |
Correct |
8 ms |
35696 KB |
Output is correct |
7 |
Correct |
8 ms |
35532 KB |
Output is correct |
8 |
Correct |
8 ms |
35676 KB |
Output is correct |
9 |
Correct |
8 ms |
35676 KB |
Output is correct |
10 |
Correct |
9 ms |
35676 KB |
Output is correct |
11 |
Correct |
8 ms |
35672 KB |
Output is correct |
12 |
Correct |
9 ms |
35932 KB |
Output is correct |
13 |
Correct |
9 ms |
35644 KB |
Output is correct |
14 |
Correct |
8 ms |
35444 KB |
Output is correct |
15 |
Correct |
9 ms |
35744 KB |
Output is correct |
16 |
Correct |
9 ms |
35676 KB |
Output is correct |
17 |
Correct |
8 ms |
35676 KB |
Output is correct |
18 |
Correct |
10 ms |
35676 KB |
Output is correct |
19 |
Correct |
9 ms |
35676 KB |
Output is correct |
20 |
Correct |
9 ms |
35676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
35676 KB |
Output is correct |
2 |
Correct |
9 ms |
35676 KB |
Output is correct |
3 |
Correct |
9 ms |
35676 KB |
Output is correct |
4 |
Correct |
8 ms |
35672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
35676 KB |
Output is correct |
2 |
Correct |
8 ms |
35676 KB |
Output is correct |
3 |
Correct |
9 ms |
35672 KB |
Output is correct |
4 |
Correct |
9 ms |
35676 KB |
Output is correct |
5 |
Correct |
9 ms |
35676 KB |
Output is correct |
6 |
Correct |
9 ms |
35676 KB |
Output is correct |
7 |
Correct |
9 ms |
35676 KB |
Output is correct |
8 |
Correct |
9 ms |
35676 KB |
Output is correct |
9 |
Correct |
8 ms |
35676 KB |
Output is correct |
10 |
Correct |
8 ms |
35676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
35700 KB |
Output is correct |
2 |
Correct |
10 ms |
35676 KB |
Output is correct |
3 |
Correct |
8 ms |
35676 KB |
Output is correct |
4 |
Correct |
9 ms |
35692 KB |
Output is correct |
5 |
Correct |
9 ms |
35676 KB |
Output is correct |
6 |
Correct |
8 ms |
35696 KB |
Output is correct |
7 |
Correct |
8 ms |
35532 KB |
Output is correct |
8 |
Correct |
8 ms |
35676 KB |
Output is correct |
9 |
Correct |
8 ms |
35676 KB |
Output is correct |
10 |
Correct |
9 ms |
35676 KB |
Output is correct |
11 |
Correct |
8 ms |
35672 KB |
Output is correct |
12 |
Correct |
9 ms |
35932 KB |
Output is correct |
13 |
Correct |
9 ms |
35644 KB |
Output is correct |
14 |
Correct |
8 ms |
35444 KB |
Output is correct |
15 |
Correct |
9 ms |
35744 KB |
Output is correct |
16 |
Correct |
9 ms |
35676 KB |
Output is correct |
17 |
Correct |
8 ms |
35676 KB |
Output is correct |
18 |
Correct |
10 ms |
35676 KB |
Output is correct |
19 |
Correct |
9 ms |
35676 KB |
Output is correct |
20 |
Correct |
9 ms |
35676 KB |
Output is correct |
21 |
Correct |
9 ms |
35676 KB |
Output is correct |
22 |
Correct |
9 ms |
35676 KB |
Output is correct |
23 |
Correct |
9 ms |
35676 KB |
Output is correct |
24 |
Correct |
8 ms |
35672 KB |
Output is correct |
25 |
Correct |
8 ms |
35676 KB |
Output is correct |
26 |
Correct |
8 ms |
35676 KB |
Output is correct |
27 |
Correct |
9 ms |
35672 KB |
Output is correct |
28 |
Correct |
9 ms |
35676 KB |
Output is correct |
29 |
Correct |
9 ms |
35676 KB |
Output is correct |
30 |
Correct |
9 ms |
35676 KB |
Output is correct |
31 |
Correct |
9 ms |
35676 KB |
Output is correct |
32 |
Correct |
9 ms |
35676 KB |
Output is correct |
33 |
Correct |
8 ms |
35676 KB |
Output is correct |
34 |
Correct |
8 ms |
35676 KB |
Output is correct |
35 |
Correct |
15 ms |
37148 KB |
Output is correct |
36 |
Correct |
11 ms |
35604 KB |
Output is correct |
37 |
Correct |
17 ms |
37212 KB |
Output is correct |
38 |
Correct |
14 ms |
38272 KB |
Output is correct |
39 |
Correct |
91 ms |
51540 KB |
Output is correct |
40 |
Correct |
92 ms |
51536 KB |
Output is correct |
41 |
Correct |
46 ms |
51364 KB |
Output is correct |
42 |
Correct |
92 ms |
51536 KB |
Output is correct |
43 |
Correct |
98 ms |
60592 KB |
Output is correct |
44 |
Correct |
44 ms |
51464 KB |
Output is correct |