// __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;
ll kod(ll x,ll y){
return (x-1)*m + y;
}
bool valid(ll x,ll y){
if(x>=1&&x<=n&&y>=1&&y<=m) return 1;
return 0;
}
vector<pll> g[maxi];
ll d[maxi];
map<char,map<char,ll> > c;
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+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});
}
}
}
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 |
Incorrect |
5 ms |
8140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
8140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
8120 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
8244 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
8140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |