#include<iostream>
#include<queue>
#define true 1
#define false 0
using namespace std;
struct node
{
int x;
int y;
int color;
int dire;
};
node start,target;
queue<node>q1;
int sx,sy;
char sc,sd;
int ex,ey;
char ec;
char maze[20][20];
int used[20][20][5][4];
int time[20][20][5][4];
int walk[4][2]={//注意要与下面的所设定的四个方向的数字值对应一致
0,1, //E-东
1,0, //S-南
0,-1, //W-西
-1,0 //N-北
};//注意电脑显示屏上的坐标与数学上坐标的区别!!!
void input();
void init();
int colorToInt(char sc);
int direToInt(char sd);
int bfs();
node moveto(node now,int i);
int effective(node next);
int isTarget(node next);
int main()
{
input();
init();
cout<<bfs()<<endl;
return 0;
}
void input()
{
cin>>sx>>sy>>sc>>sd;
cin.get();
cin>>ex>>ey>>ec;
cin.get();
for(int i=0; i<20; i++){
for(int j=0; j<20; j++){
maze[i][j]=cin.get();
// cin>>maze[i][j];
}
cin.get();
}
}
void init()
{
start.x=sx-1;
start.y=sy-1;
start.color=colorToInt(sc);
start.dire=direToInt(sd);
used[start.x][start.y][start.color][start.dire]=1;
q1.push(start);
target.x=ex-1;
target.y=ey-1;
target.color=colorToInt(ec);
}
int colorToInt(char sc)
{
switch(sc)
{
case 'R':return 0;
case 'Y':return 1;
case 'B':return 2;
case 'W':return 3;
case 'G':return 4;
default:return -1;
}
}
int direToInt(char sd)
{
switch(sd)//注意要与上面的walk[][]一致
{
case 'E':return 0;
case 'S':return 1;
case 'W':return 2;
case 'N':return 3;
default:return -1;
}
}
int bfs()
{
node now,next;
while(!q1.empty()){
now=q1.front();
q1.pop();
for(int i=0;i<3;i++){
next=moveto(now,i);
if(effective(next)){
used[next.x][next.y][next.color][next.dire]=1;
time[next.x][next.y][next.color][next.dire]=time[now.x][now.y][now.color][now.dire]+1;
if(isTarget(next))
return time[next.x][next.y][next.color][next.dire];
else q1.push(next);
}
}
}
return -1;
}
node moveto(node now,int i)
{
node next;
if(i==0){
next.x=now.x+walk[now.dire][0];
next.y=now.y+walk[now.dire][1];
next.color=(now.color+1)%5;
next.dire=now.dire;
}
else{
next.x=now.x;
next.y=now.y;
next.color=now.color;
if(i==1)//左转
next.dire=(now.dire+3)%4;//注意dire和color的设定是有顺序的,左转相当于顺时针也即右转3次
else
next.dire=(now.dire+1)%4;
}
return next;
}
int effective(node next)
{
if(next.x>=0&&next.x<20&&next.y>=0&&next.y<20){
if(maze[next.x][next.y]!='X'){
if(!used[next.x][next.y][next.color][next.dire]){
return true;
}
}
}
return false;
}
int isTarget(node next)
{
if(next.x==target.x&&next.y==target.y&&next.color==target.color){
return true;
}
else return false;
}