/*
ID: Sho10
LANG: C++
*/
#include <bits/stdc++.h> //Andrei Alexandru a.k.a Sho10
#define ll long long int
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define all(a) (a).begin(), (a).end()
#define sz size
#define f first
#define s second
#define pb push_back
#define er erase
#define in insert
#define mp make_pair
#define pi pair
#define rc(s) return cout<<s,0
#define endl '\n'
#define mod 1000000007
#define PI 3.14159265359
#define CODE_START ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,m,dp[3][505];
char a[505][505];
int32_t main(){
CODE_START;
cin>>n>>m;
for(ll i=1;i<=n;i++)
{
for(ll j=1;j<=m;j++)
cin>>a[i][j];
}
if(n==2){
for(ll j=1;j<=m-1;j++)
{
if(a[1][j]=='X'&&a[1][j]=='X'){
rc("-1");
}else if(j+1<=m){
if(a[1][j]=='X'&&a[2][j+1]=='X'){
rc("-1");
}else if(a[2][j]=='X'&&a[1][j+1]=='X'){
rc("-1");
}
}
}
dp[1][1]=0;
if(a[1][1]=='E'){
dp[2][1]=1;
}else if(a[1][1]=='N'){
dp[2][1]=2;
}else if(a[1][1]=='W'){
dp[2][1]=3;
}else if(a[1][1]=='S'){
dp[2][1]=0;
}
for(ll j=2;j<=m;j++)
{
ll s1=0;
if(a[1][j-1]=='E'){
s1=0;
}else if(a[1][j-1]=='N'){
s1=1;
}else if(a[1][j-1]=='W'){
s1=2;
}else if(a[1][j-1]=='S'){
s1=3;
}
dp[1][j]=dp[1][j-1]+s1;
s1=0;
if(a[2][j-1]=='E'){
s1=0;
}else if(a[2][j-1]=='N'){
s1=1;
}else if(a[2][j-1]=='W'){
s1=2;
}else if(a[2][j-1]=='S'){
s1=3;
}
dp[2][j]=dp[2][j-1]+s1;
s1=0;
if(a[1][j]=='E'){
s1=1;
}else if(a[1][j]=='N'){
s1=2;
}else if(a[1][j]=='W'){
s1=3;
}
dp[2][j]=min(dp[2][j],dp[1][j]+s1);
s1=0;
if(a[2][j]=='N'){
s1=0;
}else if(a[2][j]=='W'){
s1=1;
}else if(a[2][j]=='S'){
s1=2;
}else if(a[2][j]=='E'){
s1=3;
}
dp[1][j]=min(dp[1][j],dp[2][j]+s1);
}
cout<<dp[2][m]<<endl;
}else {
ll ans=0;
for(ll i=1;i<=n;i++)
for(ll j=1;j<=m-1;j++)
if(a[i][j]=='X'){
cout<<"-1"<<endl;
return 0;
}else if(a[i][j]=='S'){
ans=ans+3;
}else if(a[i][j]=='W'){
ans=ans+2;
}else if(a[i][j]=='N'){
ans++;
}
cout<<ans<<endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
6 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
6 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
21 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |