#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==1){
for(i=1;i<m;i++){
if(a[1][i]=='N'){cur++;}
else if(a[1][i]=='E'){continue;}
else if(a[1][i]=='S'){cur+=3;}
else if(a[1][i]=='W'){cur+=2;}
else{cout<<-1<<endl;return ;}
}
cout<<cur<<endl;
return ;
}
else 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();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
328 KB |
Output is correct |
19 |
Correct |
1 ms |
324 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
328 KB |
Output is correct |
19 |
Correct |
1 ms |
324 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |