#include <bits/stdc++.h>
#define mp(a,b) make_pair(a,b)
using namespace std;
struct ano{
int c;
int d;
bool operator<(const ano &o)const{
return d>o.d;
}
};
vector<pair<int,int> > a[250000];
priority_queue<pair<int,int> , vector<pair<int,int> >, greater<pair<int,int> > > q;
int d[250000];
int main (void){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,m;
cin>>n>>m;
int i,j;
char c;
for(i=0;i<n*m;i++)
d[i]=1000000;
for(i=0;i<n;i++)
for(j=0;j<m;j++){
cin>>c;
if(c=='X')
continue;
if(c=='N'){
if(i-1>=0)
a[i*m+j].push_back(mp(0,(i-1)*m+j));
if(j+1<m)
a[i*m+j].push_back(mp(1,i*m+j+1));
if(i+1<n)
a[i*m+j].push_back(mp(2,(i+1)*m+j));
if(j-1>=0)
a[i*m+j].push_back(mp(3,i*m+j-1));
}
else if(c=='E'){
if(i-1>=0)
a[i*m+j].push_back(mp(3,(i-1)*m+j));
if(j+1<m)
a[i*m+j].push_back(mp(0,i*m+j+1));
if(i+1<n)
a[i*m+j].push_back(mp(1,(i+1)*m+j));
if(j-1>=0)
a[i*m+j].push_back(mp(2,i*m+j-1));
}
else if(c=='S'){
if(i-1>=0)
a[i*m+j].push_back(mp(2,(i-1)*m+j));
if(j+1<m)
a[i*m+j].push_back(mp(3,i*m+j+1));
if(i+1<n)
a[i*m+j].push_back(mp(0,(i+1)*m+j));
if(j-1>=0)
a[i*m+j].push_back(mp(1,i*m+j-1));
}
else{
if(i-1>=0)
a[i*m+j].push_back(mp(1,(i-1)*m+j));
if(j+1<m)
a[i*m+j].push_back(mp(2,i*m+j+1));
if(i+1<n)
a[i*m+j].push_back(mp(3,(i+1)*m+j));
if(j-1>=0)
a[i*m+j].push_back(mp(0,i*m+j-1));
}
}
q.push({0,0});
d[0]=0;
int start,w;
while(!q.empty()){
start=q.top().second;
w=q.top().first;
q.pop();
if(d[start]<w)
continue;
for(auto x : a[start])
if(d[x.second]>d[start]+x.first){
d[x.second]=d[start]+x.first;
q.push(x);
}
}
if(d[n*m-1]==1000000)
cout<<-1<<'\n';
else
cout<<d[n*m-1]<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6272 KB |
Output is correct |
2 |
Correct |
8 ms |
6272 KB |
Output is correct |
3 |
Correct |
8 ms |
6272 KB |
Output is correct |
4 |
Correct |
8 ms |
6272 KB |
Output is correct |
5 |
Correct |
8 ms |
6272 KB |
Output is correct |
6 |
Correct |
8 ms |
6272 KB |
Output is correct |
7 |
Correct |
8 ms |
6144 KB |
Output is correct |
8 |
Correct |
8 ms |
6272 KB |
Output is correct |
9 |
Correct |
8 ms |
6272 KB |
Output is correct |
10 |
Correct |
8 ms |
6272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6272 KB |
Output is correct |
2 |
Correct |
8 ms |
6272 KB |
Output is correct |
3 |
Correct |
8 ms |
6272 KB |
Output is correct |
4 |
Correct |
8 ms |
6272 KB |
Output is correct |
5 |
Correct |
8 ms |
6272 KB |
Output is correct |
6 |
Correct |
8 ms |
6272 KB |
Output is correct |
7 |
Correct |
8 ms |
6144 KB |
Output is correct |
8 |
Correct |
8 ms |
6272 KB |
Output is correct |
9 |
Correct |
8 ms |
6272 KB |
Output is correct |
10 |
Correct |
8 ms |
6272 KB |
Output is correct |
11 |
Correct |
8 ms |
6144 KB |
Output is correct |
12 |
Correct |
8 ms |
6272 KB |
Output is correct |
13 |
Correct |
8 ms |
6272 KB |
Output is correct |
14 |
Correct |
8 ms |
6272 KB |
Output is correct |
15 |
Correct |
8 ms |
6144 KB |
Output is correct |
16 |
Correct |
8 ms |
6272 KB |
Output is correct |
17 |
Correct |
8 ms |
6272 KB |
Output is correct |
18 |
Correct |
8 ms |
6272 KB |
Output is correct |
19 |
Correct |
8 ms |
6144 KB |
Output is correct |
20 |
Correct |
8 ms |
6272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6144 KB |
Output is correct |
2 |
Correct |
8 ms |
6272 KB |
Output is correct |
3 |
Correct |
8 ms |
6144 KB |
Output is correct |
4 |
Correct |
8 ms |
6144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
6272 KB |
Output is correct |
2 |
Correct |
8 ms |
6272 KB |
Output is correct |
3 |
Correct |
8 ms |
6272 KB |
Output is correct |
4 |
Correct |
8 ms |
6272 KB |
Output is correct |
5 |
Correct |
9 ms |
6272 KB |
Output is correct |
6 |
Correct |
9 ms |
6272 KB |
Output is correct |
7 |
Correct |
8 ms |
6272 KB |
Output is correct |
8 |
Correct |
8 ms |
6272 KB |
Output is correct |
9 |
Correct |
8 ms |
6272 KB |
Output is correct |
10 |
Correct |
8 ms |
6272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6272 KB |
Output is correct |
2 |
Correct |
8 ms |
6272 KB |
Output is correct |
3 |
Correct |
8 ms |
6272 KB |
Output is correct |
4 |
Correct |
8 ms |
6272 KB |
Output is correct |
5 |
Correct |
8 ms |
6272 KB |
Output is correct |
6 |
Correct |
8 ms |
6272 KB |
Output is correct |
7 |
Correct |
8 ms |
6144 KB |
Output is correct |
8 |
Correct |
8 ms |
6272 KB |
Output is correct |
9 |
Correct |
8 ms |
6272 KB |
Output is correct |
10 |
Correct |
8 ms |
6272 KB |
Output is correct |
11 |
Correct |
8 ms |
6144 KB |
Output is correct |
12 |
Correct |
8 ms |
6272 KB |
Output is correct |
13 |
Correct |
8 ms |
6272 KB |
Output is correct |
14 |
Correct |
8 ms |
6272 KB |
Output is correct |
15 |
Correct |
8 ms |
6144 KB |
Output is correct |
16 |
Correct |
8 ms |
6272 KB |
Output is correct |
17 |
Correct |
8 ms |
6272 KB |
Output is correct |
18 |
Correct |
8 ms |
6272 KB |
Output is correct |
19 |
Correct |
8 ms |
6144 KB |
Output is correct |
20 |
Correct |
8 ms |
6272 KB |
Output is correct |
21 |
Correct |
8 ms |
6144 KB |
Output is correct |
22 |
Correct |
8 ms |
6272 KB |
Output is correct |
23 |
Correct |
8 ms |
6144 KB |
Output is correct |
24 |
Correct |
8 ms |
6144 KB |
Output is correct |
25 |
Correct |
9 ms |
6272 KB |
Output is correct |
26 |
Correct |
8 ms |
6272 KB |
Output is correct |
27 |
Correct |
8 ms |
6272 KB |
Output is correct |
28 |
Correct |
8 ms |
6272 KB |
Output is correct |
29 |
Correct |
9 ms |
6272 KB |
Output is correct |
30 |
Correct |
9 ms |
6272 KB |
Output is correct |
31 |
Correct |
8 ms |
6272 KB |
Output is correct |
32 |
Correct |
8 ms |
6272 KB |
Output is correct |
33 |
Correct |
8 ms |
6272 KB |
Output is correct |
34 |
Correct |
8 ms |
6272 KB |
Output is correct |
35 |
Correct |
121 ms |
7796 KB |
Output is correct |
36 |
Correct |
8 ms |
6272 KB |
Output is correct |
37 |
Correct |
82 ms |
7924 KB |
Output is correct |
38 |
Correct |
14 ms |
7936 KB |
Output is correct |
39 |
Correct |
1359 ms |
21072 KB |
Output is correct |
40 |
Execution timed out |
2084 ms |
25272 KB |
Time limit exceeded |
41 |
Halted |
0 ms |
0 KB |
- |