/*
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({{i,j+1},cnt});
}else if(directii[pos]=='W'&&j>1){
v[i][j].pb({{i,j-1},cnt});
}
}else {
if(directii[pos]=='N'&&i>1){
v[i][j].pb({{i-1,j},cnt});
}else if(directii[pos]=='S'&&i<n){
v[i][j].pb({{i+1,j},cnt});
}
}
pos++;
}
}
ll sol(){
ll x=1,y=1;
viz[1][1]=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[1][1]){
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 it1:v[x][y]){
if(viz[it1.f.f][it1.f.s]==0){
q.push({val+it1.s,{it1.f.f,it1.f.s}});
}
}
viz[x][y]=1;
}
if(q.empty()==true){
return -1;
}
return q.top().f;
}
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);
}
cout<<sol();
}
Compilation message
adventure.cpp: In function 'long long int sol()':
adventure.cpp:56:4: warning: unused variable 'x' [-Wunused-variable]
ll x=1,y=1;
^
adventure.cpp:56:8: warning: unused variable 'y' [-Wunused-variable]
ll x=1,y=1;
^
# |
결과 |
실행 시간 |
메모리 |
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 |
8 ms |
6400 KB |
Output is correct |
6 |
Correct |
8 ms |
6400 KB |
Output is correct |
7 |
Correct |
7 ms |
6272 KB |
Output is correct |
8 |
Correct |
8 ms |
6400 KB |
Output is correct |
9 |
Correct |
8 ms |
6400 KB |
Output is correct |
10 |
Correct |
11 ms |
6400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
8 ms |
6400 KB |
Output is correct |
6 |
Correct |
8 ms |
6400 KB |
Output is correct |
7 |
Correct |
7 ms |
6272 KB |
Output is correct |
8 |
Correct |
8 ms |
6400 KB |
Output is correct |
9 |
Correct |
8 ms |
6400 KB |
Output is correct |
10 |
Correct |
11 ms |
6400 KB |
Output is correct |
11 |
Correct |
8 ms |
6400 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 |
6400 KB |
Output is correct |
15 |
Correct |
8 ms |
6400 KB |
Output is correct |
16 |
Correct |
8 ms |
6400 KB |
Output is correct |
17 |
Correct |
8 ms |
6400 KB |
Output is correct |
18 |
Correct |
8 ms |
6400 KB |
Output is correct |
19 |
Correct |
8 ms |
6400 KB |
Output is correct |
20 |
Correct |
8 ms |
6528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
9 ms |
6400 KB |
Output is correct |
5 |
Correct |
8 ms |
6528 KB |
Output is correct |
6 |
Correct |
9 ms |
6400 KB |
Output is correct |
7 |
Correct |
8 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
8 ms |
6400 KB |
Output is correct |
6 |
Correct |
8 ms |
6400 KB |
Output is correct |
7 |
Correct |
7 ms |
6272 KB |
Output is correct |
8 |
Correct |
8 ms |
6400 KB |
Output is correct |
9 |
Correct |
8 ms |
6400 KB |
Output is correct |
10 |
Correct |
11 ms |
6400 KB |
Output is correct |
11 |
Correct |
8 ms |
6400 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 |
6400 KB |
Output is correct |
15 |
Correct |
8 ms |
6400 KB |
Output is correct |
16 |
Correct |
8 ms |
6400 KB |
Output is correct |
17 |
Correct |
8 ms |
6400 KB |
Output is correct |
18 |
Correct |
8 ms |
6400 KB |
Output is correct |
19 |
Correct |
8 ms |
6400 KB |
Output is correct |
20 |
Correct |
8 ms |
6528 KB |
Output is correct |
21 |
Correct |
8 ms |
6400 KB |
Output is correct |
22 |
Correct |
8 ms |
6400 KB |
Output is correct |
23 |
Correct |
8 ms |
6400 KB |
Output is correct |
24 |
Correct |
8 ms |
6400 KB |
Output is correct |
25 |
Correct |
8 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 |
9 ms |
6400 KB |
Output is correct |
29 |
Correct |
8 ms |
6528 KB |
Output is correct |
30 |
Correct |
9 ms |
6400 KB |
Output is correct |
31 |
Correct |
8 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 |
19 ms |
8576 KB |
Output is correct |
36 |
Correct |
9 ms |
6528 KB |
Output is correct |
37 |
Correct |
23 ms |
9216 KB |
Output is correct |
38 |
Correct |
17 ms |
10112 KB |
Output is correct |
39 |
Correct |
141 ms |
30968 KB |
Output is correct |
40 |
Correct |
137 ms |
30712 KB |
Output is correct |
41 |
Correct |
61 ms |
28664 KB |
Output is correct |
42 |
Correct |
141 ms |
30840 KB |
Output is correct |
43 |
Correct |
124 ms |
39140 KB |
Output is correct |
44 |
Correct |
60 ms |
28792 KB |
Output is correct |