#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define F first
#define S second
const int N = 250005;
vector<pair<int, int>> adj[N];
bool mark[N];
int n, m;
int dist[N];
void dijkstra(int start){
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> minq;
for(int i=0; i< n*m; i++){
dist[i]=1e9;
}
dist[0]=0;
minq.push(mp(0, 0));
while(!minq.empty()){
pair<int, int> node=minq.top();
minq.pop();
if(mark[node.S]){
continue;
}
for(pair<int, int> to : adj[node.S]){
dist[to.F]=min(dist[to.F], node.F+to.S);
minq.push(mp(dist[to.F], to.F));
}
mark[node.S]=true;
}
}
int main()
{
cin >> n >> m;
char a[n][m];
for(int i=0; i< n; i++){
for(int j=0; j< m; j++){
cin >> a[i][j];
}
}
for(int i=0; i< n; i++){
for(int j=0; j< m; j++){
int node=i*m+j;
if(a[i][j]=='X'){
continue;
}
else if(a[i][j]=='E'){
if(j<m-1)adj[node].pb(mp(node+1, 0));
if(j>0)adj[node].pb(mp(node-1, 2));
if(i>0)adj[node].pb(mp(node-m, 3));
if(i<n-1)adj[node].pb(mp(node+m, 1));
}
else if(a[i][j]=='S'){
if(j<m-1)adj[node].pb(mp(node+1, 3));
if(j>0)adj[node].pb(mp(node-1, 1));
if(i>0)adj[node].pb(mp(node-m, 2));
if(i<n-1)adj[node].pb(mp(node+m, 0));
}
else if(a[i][j]=='W'){
if(j<m-1)adj[node].pb(mp(node+1, 2));
if(j>0)adj[node].pb(mp(node-1, 0));
if(i>0)adj[node].pb(mp(node-m, 1));
if(i<n-1)adj[node].pb(mp(node+m, 3));
}
else{
if(j<m-1)adj[node].pb(mp(node+1, 1));
if(j>0)adj[node].pb(mp(node-1, 3));
if(i>0)adj[node].pb(mp(node-m, 0));
if(i<n-1)adj[node].pb(mp(node+m, 2));
}
}
}
dijkstra(0);
if(dist[n*m-1]==1000000000){
cout << -1 << '\n';
return 0;
}
cout << dist[n*m-1] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
2 |
Correct |
3 ms |
6100 KB |
Output is correct |
3 |
Correct |
3 ms |
6100 KB |
Output is correct |
4 |
Correct |
3 ms |
6176 KB |
Output is correct |
5 |
Correct |
3 ms |
6100 KB |
Output is correct |
6 |
Correct |
3 ms |
6100 KB |
Output is correct |
7 |
Correct |
3 ms |
6100 KB |
Output is correct |
8 |
Correct |
4 ms |
6180 KB |
Output is correct |
9 |
Correct |
3 ms |
6100 KB |
Output is correct |
10 |
Correct |
3 ms |
6100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
2 |
Correct |
3 ms |
6100 KB |
Output is correct |
3 |
Correct |
3 ms |
6100 KB |
Output is correct |
4 |
Correct |
3 ms |
6176 KB |
Output is correct |
5 |
Correct |
3 ms |
6100 KB |
Output is correct |
6 |
Correct |
3 ms |
6100 KB |
Output is correct |
7 |
Correct |
3 ms |
6100 KB |
Output is correct |
8 |
Correct |
4 ms |
6180 KB |
Output is correct |
9 |
Correct |
3 ms |
6100 KB |
Output is correct |
10 |
Correct |
3 ms |
6100 KB |
Output is correct |
11 |
Correct |
3 ms |
6100 KB |
Output is correct |
12 |
Correct |
3 ms |
6100 KB |
Output is correct |
13 |
Correct |
3 ms |
6100 KB |
Output is correct |
14 |
Correct |
3 ms |
6100 KB |
Output is correct |
15 |
Correct |
3 ms |
6180 KB |
Output is correct |
16 |
Correct |
3 ms |
6184 KB |
Output is correct |
17 |
Correct |
3 ms |
6100 KB |
Output is correct |
18 |
Correct |
3 ms |
6188 KB |
Output is correct |
19 |
Correct |
3 ms |
6100 KB |
Output is correct |
20 |
Correct |
3 ms |
6100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
2 |
Correct |
3 ms |
6100 KB |
Output is correct |
3 |
Correct |
3 ms |
6100 KB |
Output is correct |
4 |
Correct |
3 ms |
6100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
2 |
Correct |
3 ms |
6184 KB |
Output is correct |
3 |
Correct |
3 ms |
6100 KB |
Output is correct |
4 |
Correct |
3 ms |
6204 KB |
Output is correct |
5 |
Correct |
3 ms |
6228 KB |
Output is correct |
6 |
Correct |
3 ms |
6184 KB |
Output is correct |
7 |
Correct |
3 ms |
6100 KB |
Output is correct |
8 |
Correct |
3 ms |
6188 KB |
Output is correct |
9 |
Correct |
3 ms |
6100 KB |
Output is correct |
10 |
Correct |
3 ms |
6184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
2 |
Correct |
3 ms |
6100 KB |
Output is correct |
3 |
Correct |
3 ms |
6100 KB |
Output is correct |
4 |
Correct |
3 ms |
6176 KB |
Output is correct |
5 |
Correct |
3 ms |
6100 KB |
Output is correct |
6 |
Correct |
3 ms |
6100 KB |
Output is correct |
7 |
Correct |
3 ms |
6100 KB |
Output is correct |
8 |
Correct |
4 ms |
6180 KB |
Output is correct |
9 |
Correct |
3 ms |
6100 KB |
Output is correct |
10 |
Correct |
3 ms |
6100 KB |
Output is correct |
11 |
Correct |
3 ms |
6100 KB |
Output is correct |
12 |
Correct |
3 ms |
6100 KB |
Output is correct |
13 |
Correct |
3 ms |
6100 KB |
Output is correct |
14 |
Correct |
3 ms |
6100 KB |
Output is correct |
15 |
Correct |
3 ms |
6180 KB |
Output is correct |
16 |
Correct |
3 ms |
6184 KB |
Output is correct |
17 |
Correct |
3 ms |
6100 KB |
Output is correct |
18 |
Correct |
3 ms |
6188 KB |
Output is correct |
19 |
Correct |
3 ms |
6100 KB |
Output is correct |
20 |
Correct |
3 ms |
6100 KB |
Output is correct |
21 |
Correct |
3 ms |
6100 KB |
Output is correct |
22 |
Correct |
3 ms |
6100 KB |
Output is correct |
23 |
Correct |
3 ms |
6100 KB |
Output is correct |
24 |
Correct |
3 ms |
6100 KB |
Output is correct |
25 |
Correct |
3 ms |
6100 KB |
Output is correct |
26 |
Correct |
3 ms |
6184 KB |
Output is correct |
27 |
Correct |
3 ms |
6100 KB |
Output is correct |
28 |
Correct |
3 ms |
6204 KB |
Output is correct |
29 |
Correct |
3 ms |
6228 KB |
Output is correct |
30 |
Correct |
3 ms |
6184 KB |
Output is correct |
31 |
Correct |
3 ms |
6100 KB |
Output is correct |
32 |
Correct |
3 ms |
6188 KB |
Output is correct |
33 |
Correct |
3 ms |
6100 KB |
Output is correct |
34 |
Correct |
3 ms |
6184 KB |
Output is correct |
35 |
Correct |
12 ms |
7140 KB |
Output is correct |
36 |
Correct |
4 ms |
6228 KB |
Output is correct |
37 |
Correct |
13 ms |
7344 KB |
Output is correct |
38 |
Correct |
8 ms |
7856 KB |
Output is correct |
39 |
Correct |
113 ms |
17312 KB |
Output is correct |
40 |
Correct |
119 ms |
17284 KB |
Output is correct |
41 |
Correct |
33 ms |
16980 KB |
Output is correct |
42 |
Correct |
130 ms |
17288 KB |
Output is correct |
43 |
Correct |
140 ms |
21600 KB |
Output is correct |
44 |
Correct |
33 ms |
16912 KB |
Output is correct |