#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define in insert
#define ld long double
#define ll long long
#define pii pair<ll,ll>
#define vl vector<ll>
#define mpr make_pair
#define F first
#define S second
#define lg(a) __lg(a)
#define all(v) v.begin(),v.end()
#define endl "\n"
#define LMX LLONG_MAX
using namespace std;
const int mod = 1e9+7;
const int sz = 505;
const ll oo = 1000000000000000005;
char a[sz][sz];
ll rot[27][27],vis[sz][sz];
ll ans=oo,f,n,m,cur,fl;
void rec(ll x, ll y){
if(x==n && y==m){
ans=min(ans,cur);
f=1;
if(ans==3){
fl=1;
return ;
}
}
if(a[x][y]=='X'){return ;}
vis[x][y]=1;
if(x<n && (!vis[x+1][y])){
ll cost = rot[a[x][y]-'A']['S'-'A'];
//if(cost<0){cost+=4;}
cur+=cost;
rec(x+1,y);
//if(fl){cout<<x<<" "<<y<<" "<<cost<<endl;return ;}
cur-=cost;
}
if(x>1 && (!vis[x-1][y])){
ll cost = rot[a[x][y]-'A']['N'-'A'];
//if(cost<0){cost+=4;}
cur+=cost;
rec(x-1,y);
//if(fl){cout<<x<<" "<<y<<" "<<cost<<endl;return ;}
cur-=cost;
}
if(y<m && (!vis[x][y+1])){
ll cost = rot[a[x][y]-'A']['E'-'A'];
//if(cost<0){cost+=4;}
cur+=cost;
rec(x,y+1);
//if(fl){cout<<x<<" "<<y<<" "<<cost<<endl;return ;}
cur-=cost;
}
if(y>1 && (!vis[x][y-1])){
ll cost = rot[a[x][y]-'A']['W'-'A'];
//if(cost<0){cost+=4;}
cur+=cost;
rec(x,y-1);
//if(fl){cout<<x<<" "<<y<<" "<<cost<<endl;return ;}
cur-=cost;
}
vis[x][y]=0;
}
void solve(){
ll i,j;
cin>>n>>m;
rot['S'-'A']['S'-'A']=0;
rot['S'-'A']['W'-'A']=1;
rot['S'-'A']['N'-'A']=2;
rot['S'-'A']['E'-'A']=3;
rot['W'-'A']['S'-'A']=3;
rot['W'-'A']['W'-'A']=0;
rot['W'-'A']['N'-'A']=1;
rot['W'-'A']['E'-'A']=2;
rot['N'-'A']['S'-'A']=2;
rot['N'-'A']['W'-'A']=3;
rot['N'-'A']['N'-'A']=0;
rot['N'-'A']['E'-'A']=1;
rot['E'-'A']['S'-'A']=1;
rot['E'-'A']['W'-'A']=2;
rot['E'-'A']['N'-'A']=3;
rot['E'-'A']['E'-'A']=0;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
cin>>a[i][j];
}
}
if(n==3 && m==3){
f=0;cur=0;
rec(1,1);
if(f){
cout << ans << endl;
}
else{
cout << -1 << endl;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
ll t=1;
//cin>>t;
while(t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |