#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define mp make_pair
#define pb push_back
using namespace std;
int n, m;
char mat[510][510];
vector<pii> G[510 * 510];
void dijkstra(){
int dist[510 * 510];
for(int i = 0; i <510 * 510; i++)
{
dist[i] = 1e9;
}
priority_queue<pii> pq;
pq.push(mp(0,0));
dist[0] = 0;
while(!pq.empty()){
pii state = pq.top();
pq.pop();
for(auto next : G[state.second]){
int next_city = next.first, len = next.second;
if(dist[next_city] > dist[state.second] + len){
dist[next_city] = dist[state.second] + len;
pq.push(mp(-dist[next_city], next_city));
}
}
}
if(dist[(n - 1) * m + m-1] == 1e9)
cout<<-1<<endl;
else
cout<<dist[(n - 1) * m + m-1]<<endl;
}
inline long long vertex(int a,int b) {
return a*m + b;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
char c;
cin >> c;
if (c == 'W') {
if (j != 0) G[vertex(i,j)].push_back(mp(vertex(i,j-1),0));
if (i != 0) G[vertex(i,j)].push_back(mp(vertex(i-1,j),1));
if (j != m-1) G[vertex(i,j)].push_back(mp(vertex(i,j+1),2));
if (i != n-1) G[vertex(i,j)].push_back(mp(vertex(i+1,j),3));
}
if (c == 'E') {
if (j != 0) G[vertex(i,j)].push_back(mp(vertex(i,j-1),2));
if (i != 0) G[vertex(i,j)].push_back(mp(vertex(i-1,j),3));
if (j != m-1) G[vertex(i,j)].push_back(mp(vertex(i,j+1),0));
if (i != n-1) G[vertex(i,j)].push_back(mp(vertex(i+1,j),1));
}
if (c == 'N') {
if (j != 0) G[vertex(i,j)].push_back(mp(vertex(i,j-1),3));
if (i != 0) G[vertex(i,j)].push_back(mp(vertex(i-1,j),0));
if (j != m-1) G[vertex(i,j)].push_back(mp(vertex(i,j+1),1));
if (i != n-1) G[vertex(i,j)].push_back(mp(vertex(i+1,j),2));
}
if (c == 'S') {
if (j != 0) G[vertex(i,j)].push_back(mp(vertex(i,j-1),1));
if (i != 0) G[vertex(i,j)].push_back(mp(vertex(i-1,j),2));
if (j != m-1) G[vertex(i,j)].push_back(mp(vertex(i,j+1),3));
if (i != n-1) G[vertex(i,j)].push_back(mp(vertex(i+1,j),0));
}
}
}
dijkstra();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7404 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
6 ms |
7532 KB |
Output is correct |
5 |
Correct |
6 ms |
7532 KB |
Output is correct |
6 |
Correct |
6 ms |
7404 KB |
Output is correct |
7 |
Correct |
6 ms |
7404 KB |
Output is correct |
8 |
Correct |
6 ms |
7532 KB |
Output is correct |
9 |
Correct |
6 ms |
7532 KB |
Output is correct |
10 |
Correct |
6 ms |
7404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7404 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
6 ms |
7532 KB |
Output is correct |
5 |
Correct |
6 ms |
7532 KB |
Output is correct |
6 |
Correct |
6 ms |
7404 KB |
Output is correct |
7 |
Correct |
6 ms |
7404 KB |
Output is correct |
8 |
Correct |
6 ms |
7532 KB |
Output is correct |
9 |
Correct |
6 ms |
7532 KB |
Output is correct |
10 |
Correct |
6 ms |
7404 KB |
Output is correct |
11 |
Correct |
6 ms |
7424 KB |
Output is correct |
12 |
Correct |
6 ms |
7404 KB |
Output is correct |
13 |
Correct |
6 ms |
7532 KB |
Output is correct |
14 |
Correct |
6 ms |
7404 KB |
Output is correct |
15 |
Correct |
7 ms |
7552 KB |
Output is correct |
16 |
Correct |
6 ms |
7532 KB |
Output is correct |
17 |
Correct |
6 ms |
7532 KB |
Output is correct |
18 |
Correct |
6 ms |
7404 KB |
Output is correct |
19 |
Correct |
6 ms |
7404 KB |
Output is correct |
20 |
Correct |
6 ms |
7532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7404 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
6 ms |
7404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7532 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7532 KB |
Output is correct |
4 |
Correct |
6 ms |
7532 KB |
Output is correct |
5 |
Correct |
6 ms |
7532 KB |
Output is correct |
6 |
Correct |
6 ms |
7532 KB |
Output is correct |
7 |
Correct |
6 ms |
7532 KB |
Output is correct |
8 |
Correct |
6 ms |
7404 KB |
Output is correct |
9 |
Correct |
6 ms |
7404 KB |
Output is correct |
10 |
Correct |
6 ms |
7404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7404 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
6 ms |
7532 KB |
Output is correct |
5 |
Correct |
6 ms |
7532 KB |
Output is correct |
6 |
Correct |
6 ms |
7404 KB |
Output is correct |
7 |
Correct |
6 ms |
7404 KB |
Output is correct |
8 |
Correct |
6 ms |
7532 KB |
Output is correct |
9 |
Correct |
6 ms |
7532 KB |
Output is correct |
10 |
Correct |
6 ms |
7404 KB |
Output is correct |
11 |
Correct |
6 ms |
7424 KB |
Output is correct |
12 |
Correct |
6 ms |
7404 KB |
Output is correct |
13 |
Correct |
6 ms |
7532 KB |
Output is correct |
14 |
Correct |
6 ms |
7404 KB |
Output is correct |
15 |
Correct |
7 ms |
7552 KB |
Output is correct |
16 |
Correct |
6 ms |
7532 KB |
Output is correct |
17 |
Correct |
6 ms |
7532 KB |
Output is correct |
18 |
Correct |
6 ms |
7404 KB |
Output is correct |
19 |
Correct |
6 ms |
7404 KB |
Output is correct |
20 |
Correct |
6 ms |
7532 KB |
Output is correct |
21 |
Correct |
6 ms |
7404 KB |
Output is correct |
22 |
Correct |
6 ms |
7404 KB |
Output is correct |
23 |
Correct |
6 ms |
7404 KB |
Output is correct |
24 |
Correct |
6 ms |
7404 KB |
Output is correct |
25 |
Correct |
6 ms |
7532 KB |
Output is correct |
26 |
Correct |
6 ms |
7404 KB |
Output is correct |
27 |
Correct |
6 ms |
7532 KB |
Output is correct |
28 |
Correct |
6 ms |
7532 KB |
Output is correct |
29 |
Correct |
6 ms |
7532 KB |
Output is correct |
30 |
Correct |
6 ms |
7532 KB |
Output is correct |
31 |
Correct |
6 ms |
7532 KB |
Output is correct |
32 |
Correct |
6 ms |
7404 KB |
Output is correct |
33 |
Correct |
6 ms |
7404 KB |
Output is correct |
34 |
Correct |
6 ms |
7404 KB |
Output is correct |
35 |
Correct |
13 ms |
8300 KB |
Output is correct |
36 |
Correct |
6 ms |
7532 KB |
Output is correct |
37 |
Correct |
14 ms |
8556 KB |
Output is correct |
38 |
Correct |
12 ms |
9088 KB |
Output is correct |
39 |
Correct |
94 ms |
17132 KB |
Output is correct |
40 |
Correct |
84 ms |
17132 KB |
Output is correct |
41 |
Correct |
40 ms |
17132 KB |
Output is correct |
42 |
Correct |
84 ms |
17132 KB |
Output is correct |
43 |
Correct |
124 ms |
21600 KB |
Output is correct |
44 |
Correct |
42 ms |
17132 KB |
Output is correct |