#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define all(a) a.begin(),a.end()
const int N=1e6+500;
const int off=(1<<18);
const int MOD = 1e9+7;
vector<vector<int>>gr(N);
int val[N],vis[N];
map<pair<int,int>,int>mp;
void dij(int a){
// vis[a]=1;
// val[a]=0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0,a});
while(!pq.empty()){
int vl=pq.top().F,x=pq.top().S;
pq.pop();
if(vis[x])continue;
vis[x]=1;
val[x] = vl;
// cout<<vl<<' '<<x<<endl;
for(auto it:gr[x]){
pq.push({mp[{x, it}] + vl,it});
}
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,m;
cin >> m >> n;
char ch[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin >> ch[i][j];
}
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
val[n*i+j]=INT_MAX;
if(ch[i][j]=='X'){
// vis[n*i+j]=1;
continue;
}
if(i){
gr[n*i+j].pb(n*(i-1)+j);
if(ch[i][j]=='E')mp[{n*i+j,n*(i-1)+j}]=3;
if(ch[i][j]=='W')mp[{n*i+j,n*(i-1)+j}]=1;
if(ch[i][j]=='N')mp[{n*i+j,n*(i-1)+j}]=0;
if(ch[i][j]=='S')mp[{n*i+j,n*(i-1)+j}]=2;
}
if(i<m-1){
gr[n*i+j].pb(n*(i+1)+j);
if(ch[i][j]=='E')mp[{n*i+j,n*(i+1)+j}]=1;
if(ch[i][j]=='W')mp[{n*i+j,n*(i+1)+j}]=3;
if(ch[i][j]=='N')mp[{n*i+j,n*(i+1)+j}]=2;
if(ch[i][j]=='S')mp[{n*i+j,n*(i+1)+j}]=0;
}
if(j){
gr[n*i+j].pb(n*i+j-1);
if(ch[i][j]=='E')mp[{n*i+j,n*i+j-1}]=2;
if(ch[i][j]=='W')mp[{n*i+j,n*i+j-1}]=0;
if(ch[i][j]=='N')mp[{n*i+j,n*i+j-1}]=3;
if(ch[i][j]=='S')mp[{n*i+j,n*i+j-1}]=1;
}
if(j<n-1){
gr[n*i+j].pb(n*i+j+1);
if(ch[i][j]=='E')mp[{n*i+j,n*i+j+1}]=0;
if(ch[i][j]=='W')mp[{n*i+j,n*i+j+1}]=2;
if(ch[i][j]=='N')mp[{n*i+j,n*i+j+1}]=1;
if(ch[i][j]=='S')mp[{n*i+j,n*i+j+1}]=3;
}
}
}
dij(0);
int lst=(n)*(m)-1;
// for(int i=0;i<m;i++){
// for(int j=0;j<n;j++)
// cout << val[i * n + j] << ' ';
// cout << endl;
// }
if(val[lst]==INT_MAX)val[lst]=-1;
cout<<val[lst];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23824 KB |
Output is correct |
3 |
Correct |
13 ms |
23844 KB |
Output is correct |
4 |
Correct |
13 ms |
23804 KB |
Output is correct |
5 |
Correct |
13 ms |
23872 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23776 KB |
Output is correct |
8 |
Correct |
13 ms |
23892 KB |
Output is correct |
9 |
Correct |
13 ms |
23884 KB |
Output is correct |
10 |
Correct |
13 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23824 KB |
Output is correct |
3 |
Correct |
13 ms |
23844 KB |
Output is correct |
4 |
Correct |
13 ms |
23804 KB |
Output is correct |
5 |
Correct |
13 ms |
23872 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23776 KB |
Output is correct |
8 |
Correct |
13 ms |
23892 KB |
Output is correct |
9 |
Correct |
13 ms |
23884 KB |
Output is correct |
10 |
Correct |
13 ms |
23764 KB |
Output is correct |
11 |
Correct |
16 ms |
23764 KB |
Output is correct |
12 |
Correct |
13 ms |
23904 KB |
Output is correct |
13 |
Correct |
14 ms |
23924 KB |
Output is correct |
14 |
Correct |
13 ms |
23752 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
13 ms |
23892 KB |
Output is correct |
17 |
Correct |
13 ms |
23892 KB |
Output is correct |
18 |
Correct |
13 ms |
23764 KB |
Output is correct |
19 |
Correct |
13 ms |
23764 KB |
Output is correct |
20 |
Correct |
14 ms |
23840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23808 KB |
Output is correct |
2 |
Correct |
13 ms |
23848 KB |
Output is correct |
3 |
Correct |
12 ms |
23736 KB |
Output is correct |
4 |
Correct |
15 ms |
23748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
24016 KB |
Output is correct |
2 |
Correct |
12 ms |
23796 KB |
Output is correct |
3 |
Correct |
13 ms |
23944 KB |
Output is correct |
4 |
Correct |
12 ms |
23892 KB |
Output is correct |
5 |
Correct |
14 ms |
24020 KB |
Output is correct |
6 |
Correct |
12 ms |
24020 KB |
Output is correct |
7 |
Correct |
12 ms |
23892 KB |
Output is correct |
8 |
Correct |
14 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23824 KB |
Output is correct |
3 |
Correct |
13 ms |
23844 KB |
Output is correct |
4 |
Correct |
13 ms |
23804 KB |
Output is correct |
5 |
Correct |
13 ms |
23872 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23776 KB |
Output is correct |
8 |
Correct |
13 ms |
23892 KB |
Output is correct |
9 |
Correct |
13 ms |
23884 KB |
Output is correct |
10 |
Correct |
13 ms |
23764 KB |
Output is correct |
11 |
Correct |
16 ms |
23764 KB |
Output is correct |
12 |
Correct |
13 ms |
23904 KB |
Output is correct |
13 |
Correct |
14 ms |
23924 KB |
Output is correct |
14 |
Correct |
13 ms |
23752 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
13 ms |
23892 KB |
Output is correct |
17 |
Correct |
13 ms |
23892 KB |
Output is correct |
18 |
Correct |
13 ms |
23764 KB |
Output is correct |
19 |
Correct |
13 ms |
23764 KB |
Output is correct |
20 |
Correct |
14 ms |
23840 KB |
Output is correct |
21 |
Correct |
12 ms |
23808 KB |
Output is correct |
22 |
Correct |
13 ms |
23848 KB |
Output is correct |
23 |
Correct |
12 ms |
23736 KB |
Output is correct |
24 |
Correct |
15 ms |
23748 KB |
Output is correct |
25 |
Correct |
14 ms |
24016 KB |
Output is correct |
26 |
Correct |
12 ms |
23796 KB |
Output is correct |
27 |
Correct |
13 ms |
23944 KB |
Output is correct |
28 |
Correct |
12 ms |
23892 KB |
Output is correct |
29 |
Correct |
14 ms |
24020 KB |
Output is correct |
30 |
Correct |
12 ms |
24020 KB |
Output is correct |
31 |
Correct |
12 ms |
23892 KB |
Output is correct |
32 |
Correct |
14 ms |
23764 KB |
Output is correct |
33 |
Correct |
12 ms |
23764 KB |
Output is correct |
34 |
Correct |
12 ms |
23764 KB |
Output is correct |
35 |
Correct |
40 ms |
29524 KB |
Output is correct |
36 |
Correct |
15 ms |
24260 KB |
Output is correct |
37 |
Correct |
50 ms |
30520 KB |
Output is correct |
38 |
Correct |
37 ms |
33924 KB |
Output is correct |
39 |
Correct |
340 ms |
87760 KB |
Output is correct |
40 |
Correct |
374 ms |
87556 KB |
Output is correct |
41 |
Correct |
162 ms |
85320 KB |
Output is correct |
42 |
Correct |
357 ms |
87552 KB |
Output is correct |
43 |
Runtime error |
181 ms |
102400 KB |
Execution killed with signal 9 |
44 |
Halted |
0 ms |
0 KB |
- |