#include <bits/stdc++.h>
using namespace std;
struct ano{
int c;
int d;
bool operator<(const ano &o)const{
return d>o.d;
}
};
int main (){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,m;
cin>>n>>m;
vector<vector<ano> > a(n*m,vector<ano>());
int arr[n][m];
int i,j;
//int cnt=0;
for(i=0;i<n;i++)
for(j=0;j<m;j++){
char c;
cin>>c;
if(c=='X')
arr[i][j]=-1;
else if(c=='N')
arr[i][j]=0;
else if(c=='E')
arr[i][j]=1;
else if(c=='S')
arr[i][j]=2;
else
arr[i][j]=3;
}
for(i=0;i<n;i++)
for(j=0;j<m;j++){
if(arr[i][j]==-1)
continue;
if(arr[i][j]==0){
if(i-1>=0)
a[i*m+j].push_back({(i-1)*m+j,0});
if(j+1<m)
a[i*m+j].push_back({i*m+j+1,1});
if(i+1<n)
a[i*m+j].push_back({(i+1)*m+j,2});
if(j-1>=0)
a[i*m+j].push_back({i*m+j-1,3});
}
else if(arr[i][j]==1){
if(i-1>=0)
a[i*m+j].push_back({(i-1)*m+j,3});
if(j+1<m)
a[i*m+j].push_back({i*m+j+1,0});
if(i+1<n)
a[i*m+j].push_back({(i+1)*m+j,1});
if(j-1>=0)
a[i*m+j].push_back({i*m+j-1,2});
}
else if(arr[i][j]==2){
if(i-1>=0)
a[i*m+j].push_back({(i-1)*m+j,2});
if(j+1<m)
a[i*m+j].push_back({i*m+j+1,3});
if(i+1<n)
a[i*m+j].push_back({(i+1)*m+j,0});
if(j-1>=0)
a[i*m+j].push_back({i*m+j-1,1});
}
else{
if(i-1>=0)
a[i*m+j].push_back({(i-1)*m+j,1});
if(j+1<m)
a[i*m+j].push_back({i*m+j+1,2});
if(i+1<n)
a[i*m+j].push_back({(i+1)*m+j,3});
if(j-1>=0)
a[i*m+j].push_back({i*m+j-1,0});
}
}
/*
for(i=0;i<n*m;i++){
cout<<i<<" -> ";
for(j=0;j<a[i].size();j++)
cout<<a[i][j].c<<" - "<<a[i][j].d<<" ";
cout<<endl;
}
*/
priority_queue<ano> q;
vector <int> d(n*m,10000000);
q.push({0,0});
d[0]=0;
while(!q.empty()){
int start=q.top().c;
int w=q.top().d;
q.pop();
//cout<<start<<endl;
if(d[start]<w)
continue;
for(auto x : a[start])
if(d[x.c]>d[start]+x.d){
d[x.c]=d[start]+x.d;
q.push(x);
}
}
if(d[n*m-1]==10000000)
cout<<-1<<endl;
else
cout<<d[n*m-1]<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
4 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
6 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
4 ms |
384 KB |
Output is correct |
35 |
Correct |
158 ms |
4252 KB |
Output is correct |
36 |
Correct |
5 ms |
512 KB |
Output is correct |
37 |
Correct |
99 ms |
3440 KB |
Output is correct |
38 |
Correct |
12 ms |
3200 KB |
Output is correct |
39 |
Execution timed out |
2086 ms |
50812 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |