// __builtin_popcount(x) broj bitova
// __builtin_popcountll(x) long long
#define here cerr<<"---------------------------\n"
#include <bits/stdc++.h>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define rall(a) a.begin(),a.end(),greater<int>()
using namespace std;
void setIO(string inoutname)
{
freopen((inoutname+".in").c_str(),"r",stdin);
freopen((inoutname+".out").c_str(),"w",stdout);
}
ll gcd(ll a, ll b)
{
if(b==0) return a;
if(a==0) return b;
if(a>=b) return gcd(a%b,b);
return gcd(a,b%a);
}
#define maxn 505
#define maxm 505
#define maxi 250005
ll a[maxn][maxn];
ll n,m;
vector<pll> g[maxi];
ll d[maxi];
map<char,map<char,ll> > c;
ll kod(ll x,ll y){
return (x-1)*m + y;
}
bool valid(ll x,ll y){
if(x==n&&y==m) return 1;
if(x>=1&&x<=n&&y>=1&&y<=m&&a[x][y]!='X') return 1;
return 0;
}
void tc(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
cin >> n >> m;
for(ll i = 1;i<=n;i++){
string s;
cin >> s;
for(ll j = 1;j<=m;j++) a[i][j] = s[j-1];
}
for(ll i = 1;i<=n;i++){
for(ll j = 1;j<=m;j++){
if(!valid(i,j)) continue;
if(valid(i+1,j)){
g[kod(i,j)].pb({kod(i+1,j),c[a[i][j]]['S']});
g[kod(i+1,j)].pb({kod(i,j),c[a[i+1][j]]['N']});
}
if(valid(i-1,j)){
g[kod(i,j)].pb({kod(i-1,j),c[a[i][j]]['N']});
g[kod(i-1,j)].pb({kod(i,j),c[a[i-1][j]]['S']});
}
if(valid(i,j+1)){
g[kod(i,j)].pb({kod(i,j+1),c[a[i][j]]['E']});
g[kod(i,j+1)].pb({kod(i,j),c[a[i][j+1]]['W']});
}
if(valid(i,j-1)){
g[kod(i,j)].pb({kod(i,j-1),c[a[i][j]]['W']});
g[kod(i,j-1)].pb({kod(i,j),c[a[i][j-1]]['E']});
}
}
}
fill(d,d+maxi-1,llinf);
d[1] = 0;
priority_queue<pll> q;
q.push({0,1});
while(!q.empty()){
ll u = q.top().sc;
q.pop();
for(pll p : g[u]){
ll s = p.fi;
ll w = p.sc;
if(d[u]+w<d[s]){
d[s] = d[u]+w;
q.push({-d[s],s});
}
}
}
if(d[kod(n,m)]==llinf) cout<<-1<<endl;
else cout<<d[kod(n,m)]<<endl;
}
int main(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
//setIO("lol");
c['N']['E']=1;
c['N']['S']=2;
c['N']['W']=3;
c['E']['S']=1;
c['E']['W']=2;
c['E']['N']=3;
c['S']['W']=1;
c['S']['N']=2;
c['S']['E']=3;
c['W']['N']=1;
c['W']['E']=2;
c['W']['S']=3;
int t; t = 1;
while(t--){
tc();
}
return 0;
}
Compilation message
adventure.cpp: In function 'void setIO(std::string)':
adventure.cpp:24:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | freopen((inoutname+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
adventure.cpp:25:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | freopen((inoutname+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8140 KB |
Output is correct |
2 |
Correct |
5 ms |
8120 KB |
Output is correct |
3 |
Correct |
5 ms |
8140 KB |
Output is correct |
4 |
Correct |
6 ms |
8116 KB |
Output is correct |
5 |
Correct |
6 ms |
8124 KB |
Output is correct |
6 |
Correct |
5 ms |
8140 KB |
Output is correct |
7 |
Correct |
4 ms |
8140 KB |
Output is correct |
8 |
Correct |
5 ms |
8140 KB |
Output is correct |
9 |
Correct |
5 ms |
8124 KB |
Output is correct |
10 |
Correct |
5 ms |
8140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8140 KB |
Output is correct |
2 |
Correct |
5 ms |
8120 KB |
Output is correct |
3 |
Correct |
5 ms |
8140 KB |
Output is correct |
4 |
Correct |
6 ms |
8116 KB |
Output is correct |
5 |
Correct |
6 ms |
8124 KB |
Output is correct |
6 |
Correct |
5 ms |
8140 KB |
Output is correct |
7 |
Correct |
4 ms |
8140 KB |
Output is correct |
8 |
Correct |
5 ms |
8140 KB |
Output is correct |
9 |
Correct |
5 ms |
8124 KB |
Output is correct |
10 |
Correct |
5 ms |
8140 KB |
Output is correct |
11 |
Correct |
4 ms |
8140 KB |
Output is correct |
12 |
Correct |
5 ms |
8140 KB |
Output is correct |
13 |
Correct |
5 ms |
8140 KB |
Output is correct |
14 |
Correct |
4 ms |
8140 KB |
Output is correct |
15 |
Correct |
5 ms |
8140 KB |
Output is correct |
16 |
Correct |
5 ms |
8140 KB |
Output is correct |
17 |
Correct |
5 ms |
8140 KB |
Output is correct |
18 |
Correct |
5 ms |
8140 KB |
Output is correct |
19 |
Correct |
5 ms |
8120 KB |
Output is correct |
20 |
Correct |
5 ms |
8120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8140 KB |
Output is correct |
2 |
Correct |
5 ms |
8116 KB |
Output is correct |
3 |
Correct |
4 ms |
8140 KB |
Output is correct |
4 |
Correct |
5 ms |
8140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8160 KB |
Output is correct |
2 |
Correct |
5 ms |
8120 KB |
Output is correct |
3 |
Correct |
5 ms |
8140 KB |
Output is correct |
4 |
Correct |
5 ms |
8140 KB |
Output is correct |
5 |
Correct |
5 ms |
8248 KB |
Output is correct |
6 |
Correct |
6 ms |
8268 KB |
Output is correct |
7 |
Correct |
5 ms |
8120 KB |
Output is correct |
8 |
Correct |
5 ms |
8116 KB |
Output is correct |
9 |
Correct |
5 ms |
8140 KB |
Output is correct |
10 |
Correct |
5 ms |
8140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8140 KB |
Output is correct |
2 |
Correct |
5 ms |
8120 KB |
Output is correct |
3 |
Correct |
5 ms |
8140 KB |
Output is correct |
4 |
Correct |
6 ms |
8116 KB |
Output is correct |
5 |
Correct |
6 ms |
8124 KB |
Output is correct |
6 |
Correct |
5 ms |
8140 KB |
Output is correct |
7 |
Correct |
4 ms |
8140 KB |
Output is correct |
8 |
Correct |
5 ms |
8140 KB |
Output is correct |
9 |
Correct |
5 ms |
8124 KB |
Output is correct |
10 |
Correct |
5 ms |
8140 KB |
Output is correct |
11 |
Correct |
4 ms |
8140 KB |
Output is correct |
12 |
Correct |
5 ms |
8140 KB |
Output is correct |
13 |
Correct |
5 ms |
8140 KB |
Output is correct |
14 |
Correct |
4 ms |
8140 KB |
Output is correct |
15 |
Correct |
5 ms |
8140 KB |
Output is correct |
16 |
Correct |
5 ms |
8140 KB |
Output is correct |
17 |
Correct |
5 ms |
8140 KB |
Output is correct |
18 |
Correct |
5 ms |
8140 KB |
Output is correct |
19 |
Correct |
5 ms |
8120 KB |
Output is correct |
20 |
Correct |
5 ms |
8120 KB |
Output is correct |
21 |
Correct |
4 ms |
8140 KB |
Output is correct |
22 |
Correct |
5 ms |
8116 KB |
Output is correct |
23 |
Correct |
4 ms |
8140 KB |
Output is correct |
24 |
Correct |
5 ms |
8140 KB |
Output is correct |
25 |
Correct |
5 ms |
8160 KB |
Output is correct |
26 |
Correct |
5 ms |
8120 KB |
Output is correct |
27 |
Correct |
5 ms |
8140 KB |
Output is correct |
28 |
Correct |
5 ms |
8140 KB |
Output is correct |
29 |
Correct |
5 ms |
8248 KB |
Output is correct |
30 |
Correct |
6 ms |
8268 KB |
Output is correct |
31 |
Correct |
5 ms |
8120 KB |
Output is correct |
32 |
Correct |
5 ms |
8116 KB |
Output is correct |
33 |
Correct |
5 ms |
8140 KB |
Output is correct |
34 |
Correct |
5 ms |
8140 KB |
Output is correct |
35 |
Correct |
15 ms |
10636 KB |
Output is correct |
36 |
Correct |
5 ms |
8396 KB |
Output is correct |
37 |
Correct |
21 ms |
11240 KB |
Output is correct |
38 |
Correct |
20 ms |
13424 KB |
Output is correct |
39 |
Correct |
132 ms |
36240 KB |
Output is correct |
40 |
Correct |
142 ms |
36036 KB |
Output is correct |
41 |
Correct |
89 ms |
35952 KB |
Output is correct |
42 |
Correct |
134 ms |
36160 KB |
Output is correct |
43 |
Correct |
159 ms |
49644 KB |
Output is correct |
44 |
Correct |
102 ms |
36076 KB |
Output is correct |