#include<iostream>
#include<queue>
#include<map>
#include<stdio.h>
using namespace std;
queue<int> q1;
queue<int> q2;
queue<int> q3;
queue<int> q4;
int row1,col1;
int row2,col2;
char color1 , dire0 , color2 ;
int color2s;
char map1[20][20] = {0};
int map2[20][20][4][5] = {0};
int row,col;
int color,dire;
int drow[4] = {0,1,0,-1};
int dcol[4] = {-1,0,1,0};
void input();
int bfs();
void init();
void move();
int main()
{
input();
bfs();
return 0;
}
void input()
{
int i,j;
cin>> row1 >> col1 >> color1 >> dire0;
cin>> row2 >> col2 >> color2;
row1--;
col1--;
row2--;
col2--;
init();
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
cin>>map1[i][j];
}
}
}
int bfs()
{
int i,j;
while(!q1.empty())
{
row = q1.front();
q1.pop();
col = q2.front();
q2.pop();
dire = q3.front();
q3.pop();
color = q4.front();
q4.pop();
move();
for(i=0;i<4;i++)
{
if(map2[row2][col2][i][color2s] != 0)
{
cout<< map2[row2][col2][i][color2s] << endl;
return 0;
}
}
}
return (-1);
}
void init()
{
int tmp1,tmp2,tmp3;
int i;
if(dire0 == 'W')
{
tmp1 = 0;
}
if(dire0 == 'S')
{
tmp1 = 1;
}
if(dire0 == 'E')
{
tmp1 = 2;
}
if(dire0 == 'N')
{
tmp1 = 3;
}
if(color1 == 'R')
{
tmp2 = 0;
}
if(color1 == 'Y')
{
tmp2 = 1;
}
if(color1 == 'B')
{
tmp2 = 2;
}
if(color1 == 'W')
{
tmp2 = 3;
}
if(color1 == 'G')
{
tmp2 = 4;
}
if(color2 == 'R')
{
tmp3 = 0;
}
if(color2 == 'Y')
{
tmp3 = 1;
}
if(color2 == 'B')
{
tmp3 = 2;
}
if(color2 == 'W')
{
tmp3 = 3;
}
if(color2 == 'G')
{
tmp3 = 4;
}
color2s = tmp3;
q1.push(row1);
q2.push(col1);
q3.push(tmp1);
q4.push(tmp2);
map2[row1][col1][tmp1][tmp2] = 0;
}
void move()
{
int i,j;
int tmp1 , tmp2 ;
int rows,cols;
tmp1 = (dire + 1) % 4;
tmp2 = (dire - 1 + 4) % 4;
rows = row + drow[dire];
cols = col + dcol[dire];
if(map2[rows][cols][dire][(color+1)%5] == 0 && map1[rows][cols] == '.')
{
map2[rows][cols][dire][(color+1)%5] = map2[row][col][dire][color] +1;
q1.push(rows);
q2.push(cols);
q3.push(dire);
q4.push((color+1)%5);
}
if(map2[row][col][tmp1][color] == 0 )
{
map2[row][col][tmp1][color] = map2[row][col][dire][color] +1;
q1.push(row);
q2.push(col);
q3.push(tmp1);
q4.push(color);
}
if(map2[row][col][tmp2][color] == 0 )
{
map2[row][col][tmp2][color] = map2[row][col][dire][color] +1;
q1.push(row);
q2.push(col);
q3.push(tmp2);
q4.push(color);
}
}
#include<queue>
#include<map>
#include<stdio.h>
using namespace std;
queue<int> q1;
queue<int> q2;
queue<int> q3;
queue<int> q4;
int row1,col1;
int row2,col2;
char color1 , dire0 , color2 ;
int color2s;
char map1[20][20] = {0};
int map2[20][20][4][5] = {0};
int row,col;
int color,dire;
int drow[4] = {0,1,0,-1};
int dcol[4] = {-1,0,1,0};
void input();
int bfs();
void init();
void move();
int main()
{
input();
bfs();
return 0;
}
void input()
{
int i,j;
cin>> row1 >> col1 >> color1 >> dire0;
cin>> row2 >> col2 >> color2;
row1--;
col1--;
row2--;
col2--;
init();
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
cin>>map1[i][j];
}
}
}
int bfs()
{
int i,j;
while(!q1.empty())
{
row = q1.front();
q1.pop();
col = q2.front();
q2.pop();
dire = q3.front();
q3.pop();
color = q4.front();
q4.pop();
move();
for(i=0;i<4;i++)
{
if(map2[row2][col2][i][color2s] != 0)
{
cout<< map2[row2][col2][i][color2s] << endl;
return 0;
}
}
}
return (-1);
}
void init()
{
int tmp1,tmp2,tmp3;
int i;
if(dire0 == 'W')
{
tmp1 = 0;
}
if(dire0 == 'S')
{
tmp1 = 1;
}
if(dire0 == 'E')
{
tmp1 = 2;
}
if(dire0 == 'N')
{
tmp1 = 3;
}
if(color1 == 'R')
{
tmp2 = 0;
}
if(color1 == 'Y')
{
tmp2 = 1;
}
if(color1 == 'B')
{
tmp2 = 2;
}
if(color1 == 'W')
{
tmp2 = 3;
}
if(color1 == 'G')
{
tmp2 = 4;
}
if(color2 == 'R')
{
tmp3 = 0;
}
if(color2 == 'Y')
{
tmp3 = 1;
}
if(color2 == 'B')
{
tmp3 = 2;
}
if(color2 == 'W')
{
tmp3 = 3;
}
if(color2 == 'G')
{
tmp3 = 4;
}
color2s = tmp3;
q1.push(row1);
q2.push(col1);
q3.push(tmp1);
q4.push(tmp2);
map2[row1][col1][tmp1][tmp2] = 0;
}
void move()
{
int i,j;
int tmp1 , tmp2 ;
int rows,cols;
tmp1 = (dire + 1) % 4;
tmp2 = (dire - 1 + 4) % 4;
rows = row + drow[dire];
cols = col + dcol[dire];
if(map2[rows][cols][dire][(color+1)%5] == 0 && map1[rows][cols] == '.')
{
map2[rows][cols][dire][(color+1)%5] = map2[row][col][dire][color] +1;
q1.push(rows);
q2.push(cols);
q3.push(dire);
q4.push((color+1)%5);
}
if(map2[row][col][tmp1][color] == 0 )
{
map2[row][col][tmp1][color] = map2[row][col][dire][color] +1;
q1.push(row);
q2.push(col);
q3.push(tmp1);
q4.push(color);
}
if(map2[row][col][tmp2][color] == 0 )
{
map2[row][col][tmp2][color] = map2[row][col][dire][color] +1;
q1.push(row);
q2.push(col);
q3.push(tmp2);
q4.push(color);
}
}