#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pii pair<long long,long long>
vector < vector <pii> > Adj;
vector <long long int> Dist;
long long int INF = LLONG_MAX;
long long int LARGE = (long long)1e9;
void Dijkstra(int start) {
Dist[start] = 0;
priority_queue<pii,vector <pii>,greater <pii> > PQ;
PQ.push(mp(0,start));
while (!PQ.empty()) {
int v = PQ.top().second;
long long int d_v = PQ.top().first;
PQ.pop();
if (d_v != Dist[v]) continue;
for (auto PR : Adj[v]) {
if (Dist[PR.first] > Dist[v] + PR.second) {
Dist[PR.first] = Dist[v] + PR.second;
PQ.push(mp(Dist[PR.first],PR.first));
}
}
}
}
int n,m;
inline long long vertex(int a,int b) {
return (a-1)*(m+1)+b;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
long long int val = (n+2)*(m+2)+5;
Adj.resize(val);
Dist.resize(val,INF);
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= m;j++) {
char c;
cin >> c;
if (c == 'W') {
if (j != 1) Adj[vertex(i,j)].push_back(mp(vertex(i,j-1),0));
if (i != 1) Adj[vertex(i,j)].push_back(mp(vertex(i-1,j),1));
if (j != m) Adj[vertex(i,j)].push_back(mp(vertex(i,j+1),2));
if (i != n) Adj[vertex(i,j)].push_back(mp(vertex(i+1,j),3));
}
if (c == 'E') {
if (j != 1) Adj[vertex(i,j)].push_back(mp(vertex(i,j-1),2));
if (i != 1) Adj[vertex(i,j)].push_back(mp(vertex(i-1,j),3));
if (j != m) Adj[vertex(i,j)].push_back(mp(vertex(i,j+1),0));
if (i != n) Adj[vertex(i,j)].push_back(mp(vertex(i+1,j),1));
}
if (c == 'N') {
if (j != 1) Adj[vertex(i,j)].push_back(mp(vertex(i,j-1),3));
if (i != 1) Adj[vertex(i,j)].push_back(mp(vertex(i-1,j),0));
if (j != m) Adj[vertex(i,j)].push_back(mp(vertex(i,j+1),1));
if (i != n) Adj[vertex(i,j)].push_back(mp(vertex(i+1,j),2));
}
if (c == 'S') {
if (j != 1) Adj[vertex(i,j)].push_back(mp(vertex(i,j-1),1));
if (i != 1) Adj[vertex(i,j)].push_back(mp(vertex(i-1,j),2));
if (j != m) Adj[vertex(i,j)].push_back(mp(vertex(i,j+1),3));
if (i != n) Adj[vertex(i,j)].push_back(mp(vertex(i+1,j),0));
}
}
}
Dijkstra(vertex(1,1));
if (Dist[vertex(n,m)] == INF) {
cout << -1;
return 0;
}
cout << Dist[vertex(n,m)];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
0 ms |
364 KB |
Output is correct |
22 |
Correct |
0 ms |
364 KB |
Output is correct |
23 |
Correct |
0 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
8 ms |
2668 KB |
Output is correct |
36 |
Correct |
1 ms |
620 KB |
Output is correct |
37 |
Correct |
9 ms |
2924 KB |
Output is correct |
38 |
Correct |
8 ms |
4352 KB |
Output is correct |
39 |
Correct |
92 ms |
24172 KB |
Output is correct |
40 |
Correct |
93 ms |
24172 KB |
Output is correct |
41 |
Correct |
43 ms |
24044 KB |
Output is correct |
42 |
Correct |
94 ms |
24172 KB |
Output is correct |
43 |
Correct |
111 ms |
32240 KB |
Output is correct |
44 |
Correct |
43 ms |
24172 KB |
Output is correct |