#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll unsigned long long
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define f first
#define s second
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define imp cout<<-1<<"\n"
#define pb push_back
#define pii pair<int,int>
#define piii pair<int,pair<int,int>>
#define ls v<<1
#define rs v<<1|1
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ptree tree *
const int mod=1e9+7;
const int INF = 1e18;
const int sqr=317;
const int N=1e6;
int modi(int a,int b){
return (a*b)%mod;
}
vector<vector<pii>>g;
void solve(){
int n,m,k,a=1e18,b=0,c=0,d,e=0,f=0,q;
cin>>n>>m;
n++;
m++;
g.resize((n+2)*(m+2));
for(int i=1;i<n;i++){
for(int j=1;j<m;j++){
char ch;
cin>>ch;
if(ch=='W'){
g[i*m+j].pb({m*i+j-1,0});
g[i*m+j].pb({m*(i-1)+j,1});
g[i*m+j].pb({m*i+j+1,2});
g[i*m+j].pb({m*(i+1)+j,3});
}
if(ch=='N'){
g[i*m+j].pb({m*i+j-1,3});
g[i*m+j].pb({m*(i-1)+j,0});
g[i*m+j].pb({m*i+j+1,1});
g[i*m+j].pb({m*(i+1)+j,2});
}
if(ch=='E'){
g[i*m+j].pb({m*i+j-1,2});
g[i*m+j].pb({m*(i-1)+j,3});
g[i*m+j].pb({m*i+j+1,0});
g[i*m+j].pb({m*(i+1)+j,1});
}
if(ch=='S'){
g[i*m+j].pb({m*i+j-1,1});
g[i*m+j].pb({m*(i-1)+j,2});
g[i*m+j].pb({m*i+j+1,3});
g[i*m+j].pb({m*(i+1)+j,0});
}
}
}
int sz=(n+2)*(m+2);
vector<int>dd((n+2)*(m+2),INF);
vector<int>us((n+2)*(m+2),0);
dd[m+1]=0;
for(int i=0;i<sz;i++){
int v=-1;
for(int j=0;j<sz;j++){
if(!us[j] and (v==-1 or dd[v]>dd[j])){
v=j;
}
}
us[v]=1;
for(auto k:g[v]){
dd[k.f]=min(dd[k.f],dd[v]+k.s);
}
}
cout<<dd[(n-1)*m+m-1]<<"\n";
}
signed main(){
fast;
int t=1;
// cin>>t;
while(t--){
solve();
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |