/*
ID: Sho10
LANG: C++
*/
#include <bits/stdc++.h> //Andrei Alexandru a.k.a Sho10
#define ll long long int
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define all(a) (a).begin(), (a).end()
#define sz size
#define f first
#define s second
#define pb push_back
#define er erase
#define in insert
#define mp make_pair
#define pi pair
#define rc(s) return cout<<s,0
#define endl '\n'
#define mod 1000000007
#define PI 3.14159265359
#define CODE_START ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,m;
char c[505][505];
vector<pair<pair<ll,ll>,ll>>v[505][505];
ll viz[505][505];
char directii[]={'N','E','S','W','N','E','S','W'};
void edge(ll i,ll j){
if(c[i][j]=='X'){
return;
}
ll pos=0;
while(directii[pos]!=c[i][j]) pos++;
ll cnt=-1;
for(ll t=1;t<=4;t++)
{
++cnt;
if(directii[pos]=='E'||directii[pos]=='W'){
if(directii[pos]=='E'&&j<m){
v[i][j].pb(mp(mp(i,j+1),cnt));
}else if(directii[pos]=='W'&&j>1){
v[i][j].pb(mp(mp(i,j-1),cnt));
}
}else {
if(directii[pos]=='N'&&i>1){
v[i][j].pb(mp(mp(i-1,j),cnt));
}else if(directii[pos]=='S'&&i<n){
v[i][j].pb(mp(mp(i+1,j),cnt));
}
}
pos++;
}
}
int32_t main(){
CODE_START;
cin>>n>>m;
for(ll i=1;i<=n;i++)
for(ll j=1;j<=m;j++)
{
cin>>c[i][j];
edge(i,j);
}
ll x=1,y=1;
viz[x][y]=1;
priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>>q;
for(auto it:v[x][y]){
q.push({it.s,{it.f.f,it.f.s}});
}
while(q.size()&&(q.top().s.f!=n||q.top().s.s!=m)){
auto it=q.top();
ll x=it.s.f;
ll y=it.s.s;
if(viz[x][y]==1){
q.pop();
continue;
}
q.pop();
ll val=it.f;
for(auto itr:v[x][y]){
if(viz[itr.f.f][itr.f.s]==0){
q.push({val+itr.s,{itr.f.f,itr.f.s}});
}
}
viz[x][y]=1;
}
if(q.empty()==true){
rc("-1");
}else {
cout<<q.top().f;
return 0;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
6400 KB |
Output is correct |
2 |
Correct |
8 ms |
6400 KB |
Output is correct |
3 |
Correct |
8 ms |
6400 KB |
Output is correct |
4 |
Correct |
8 ms |
6400 KB |
Output is correct |
5 |
Correct |
9 ms |
6400 KB |
Output is correct |
6 |
Correct |
8 ms |
6528 KB |
Output is correct |
7 |
Correct |
8 ms |
6272 KB |
Output is correct |
8 |
Correct |
9 ms |
6400 KB |
Output is correct |
9 |
Correct |
8 ms |
6400 KB |
Output is correct |
10 |
Correct |
8 ms |
6400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
6400 KB |
Output is correct |
2 |
Correct |
8 ms |
6400 KB |
Output is correct |
3 |
Correct |
8 ms |
6400 KB |
Output is correct |
4 |
Correct |
8 ms |
6400 KB |
Output is correct |
5 |
Correct |
9 ms |
6400 KB |
Output is correct |
6 |
Correct |
8 ms |
6528 KB |
Output is correct |
7 |
Correct |
8 ms |
6272 KB |
Output is correct |
8 |
Correct |
9 ms |
6400 KB |
Output is correct |
9 |
Correct |
8 ms |
6400 KB |
Output is correct |
10 |
Correct |
8 ms |
6400 KB |
Output is correct |
11 |
Correct |
8 ms |
6272 KB |
Output is correct |
12 |
Correct |
8 ms |
6400 KB |
Output is correct |
13 |
Correct |
8 ms |
6400 KB |
Output is correct |
14 |
Correct |
8 ms |
6272 KB |
Output is correct |
15 |
Correct |
8 ms |
6400 KB |
Output is correct |
16 |
Correct |
9 ms |
6400 KB |
Output is correct |
17 |
Correct |
8 ms |
6400 KB |
Output is correct |
18 |
Correct |
10 ms |
6400 KB |
Output is correct |
19 |
Correct |
8 ms |
6400 KB |
Output is correct |
20 |
Correct |
8 ms |
6400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
6400 KB |
Output is correct |
2 |
Correct |
8 ms |
6400 KB |
Output is correct |
3 |
Correct |
9 ms |
6400 KB |
Output is correct |
4 |
Correct |
8 ms |
6400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
6400 KB |
Output is correct |
2 |
Correct |
8 ms |
6400 KB |
Output is correct |
3 |
Correct |
8 ms |
6400 KB |
Output is correct |
4 |
Correct |
8 ms |
6400 KB |
Output is correct |
5 |
Correct |
11 ms |
6400 KB |
Output is correct |
6 |
Correct |
8 ms |
6400 KB |
Output is correct |
7 |
Correct |
10 ms |
6400 KB |
Output is correct |
8 |
Correct |
8 ms |
6400 KB |
Output is correct |
9 |
Correct |
8 ms |
6400 KB |
Output is correct |
10 |
Correct |
8 ms |
6400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
6400 KB |
Output is correct |
2 |
Correct |
8 ms |
6400 KB |
Output is correct |
3 |
Correct |
8 ms |
6400 KB |
Output is correct |
4 |
Correct |
8 ms |
6400 KB |
Output is correct |
5 |
Correct |
9 ms |
6400 KB |
Output is correct |
6 |
Correct |
8 ms |
6528 KB |
Output is correct |
7 |
Correct |
8 ms |
6272 KB |
Output is correct |
8 |
Correct |
9 ms |
6400 KB |
Output is correct |
9 |
Correct |
8 ms |
6400 KB |
Output is correct |
10 |
Correct |
8 ms |
6400 KB |
Output is correct |
11 |
Correct |
8 ms |
6272 KB |
Output is correct |
12 |
Correct |
8 ms |
6400 KB |
Output is correct |
13 |
Correct |
8 ms |
6400 KB |
Output is correct |
14 |
Correct |
8 ms |
6272 KB |
Output is correct |
15 |
Correct |
8 ms |
6400 KB |
Output is correct |
16 |
Correct |
9 ms |
6400 KB |
Output is correct |
17 |
Correct |
8 ms |
6400 KB |
Output is correct |
18 |
Correct |
10 ms |
6400 KB |
Output is correct |
19 |
Correct |
8 ms |
6400 KB |
Output is correct |
20 |
Correct |
8 ms |
6400 KB |
Output is correct |
21 |
Correct |
8 ms |
6400 KB |
Output is correct |
22 |
Correct |
8 ms |
6400 KB |
Output is correct |
23 |
Correct |
9 ms |
6400 KB |
Output is correct |
24 |
Correct |
8 ms |
6400 KB |
Output is correct |
25 |
Correct |
11 ms |
6400 KB |
Output is correct |
26 |
Correct |
8 ms |
6400 KB |
Output is correct |
27 |
Correct |
8 ms |
6400 KB |
Output is correct |
28 |
Correct |
8 ms |
6400 KB |
Output is correct |
29 |
Correct |
11 ms |
6400 KB |
Output is correct |
30 |
Correct |
8 ms |
6400 KB |
Output is correct |
31 |
Correct |
10 ms |
6400 KB |
Output is correct |
32 |
Correct |
8 ms |
6400 KB |
Output is correct |
33 |
Correct |
8 ms |
6400 KB |
Output is correct |
34 |
Correct |
8 ms |
6400 KB |
Output is correct |
35 |
Correct |
21 ms |
8660 KB |
Output is correct |
36 |
Correct |
10 ms |
6528 KB |
Output is correct |
37 |
Correct |
21 ms |
9216 KB |
Output is correct |
38 |
Correct |
16 ms |
10112 KB |
Output is correct |
39 |
Correct |
138 ms |
30840 KB |
Output is correct |
40 |
Correct |
137 ms |
30712 KB |
Output is correct |
41 |
Correct |
58 ms |
28664 KB |
Output is correct |
42 |
Correct |
161 ms |
30712 KB |
Output is correct |
43 |
Correct |
120 ms |
39012 KB |
Output is correct |
44 |
Correct |
58 ms |
28664 KB |
Output is correct |