Problem:
Problem C: Cubic Eight-Puzzle

Let's play a puzzle using eight cubes placed on a 3 × 3 board leaving one empty square.

Faces of cubes are painted with three colors. As a puzzle step, you can roll one of the cubes to
the adjacent empty square. Your goal is to make the specified color pattern visible from above
by a number of such steps.

The rules of this puzzle are as follows.

Coloring of Cubes: All the cubes are colored in the same way as shown in Figure 3.
The opposite faces have the same color.

Figure 3: Coloring of a cube

Initial Board State: Eight cubes are placed on the 3 × 3 board leaving one empty square.
All the cubes have the same orientation as shown in Figure 4. As shown in the figure,
squares on the board are given x and y coordinates, (1, 1), (1, 2), .. ., and (3, 3). The
position of the initially empty square may vary.

Figure 4: Initial board state

Rolling Cubes: At each step, we can choose one of the cubes adjacent to the empty square and roll it into the empty square, leaving the original position empty. Figure 5
shows an example.

Figure 5: Rolling a cube

Goal: The goal of this puzzle is to arrange the cubes so that their top faces form the
specified color pattern by a number of cube rolling steps described above.

Your task is to write a program that finds the minimum number of steps required to make the
specified color pattern from the given initial state.

Input

The input is a sequence of datasets. The end of the input is indicated by a line containing two
zeros separated by a space. The number of datasets is less than 16. Each dataset is formatted
as follows.

x y
F11 F21 F31
F12 F22 F32
F13 F23 F33

The first line contains two integers x and y separated by a space, indicating the position (x, y)
of the initially empty square. The values of x and y are 1, 2, or 3.

The following three lines specify the color pattern to make. Each line contains three characters
F1j, F2j, and F3j, separated by a space. Character Fij indicates the top color of the cube, if any, at position (i, j) as follows:

B: Blue
W: White
R: Red
E: the square is Empty.

There is exactly one 'E' character in each dataset.

Output

For each dataset, output the minimum number of steps to achieve the goal, when the goal can
be reached within 30 steps. Otherwise, output "-1" for the dataset.

Sample Input

1 2
W W W
E W W
W W W
2 1
R B W
R W W
E W W
3 3
W B W
B R E
R B R
3 3
B W R
B W R
B E R
2 1
B B B
B R B
B R E
1 1
R R R
W W W
R R E
2 1
R R R
B W B
R R E
3 2
R R R
W E W
R R R
0 0

Output for the Sample Input

0
3
13
23
29
30
-1
-1

Solution in C++ (judge verdict: Memory Limit Exceeded)

#include<iostream>
#include<algorithm>
#include<queue>
#include<set>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)

#define WHITE 0
#define RED   1
#define BLUE  2
#define EMPTY 3
#define rotate_swap(x,a,b,c,d) swap(x.a,x.b);swap(x.b,x.c);swap(x.c,x.d);

class Dice{
public:
  char top,front,right,bottom,back,left;
  bool operator<(const Dice &a)const;
  bool operator==(const Dice &a)const;
};

bool Dice::operator<(const Dice &a)const{
  if (top    != a.top)   return top    < a.top;
  if (front  != a.front) return front  < a.front;
  if (right  != a.right) return right  < a.right;
  if (bottom != a.bottom)return bottom < a.bottom;
  if (back   != a.back)  return back   < a.back;
  if (left   != a.left)  return left   < a.left;
  return false;
}

bool Dice::operator==(const Dice &a)const{
  return top   == a.top   && front  == a.front && 
    right == a.right && bottom == a.bottom &&
    back  == a.back  && left   == a.left;
}

class state{
public:
  int y,x;
  Dice d[3][3];
  int cnt;
  state(){
    cnt=0;
  }
  state(int ty,int tx);

  bool operator<(const state & a)const;

};

state::state(int ty,int tx){
  y=ty;
  x=tx;
  cnt=0;
  rep(i,3){
    rep(j,3){
      d[i][j].top  =d[i][j].bottom=WHITE;
      d[i][j].left =d[i][j].right =BLUE;
      d[i][j].front=d[i][j].back  =RED;
    }
  }
}

bool state::operator<(const state & a)const{
    if (x != a.x)return x < a.x;
    if (y != a.y)return y < a.y;
    rep(i,3){
      rep(j,3){
	if (y == i && j == x)continue;
	if (!(d[i][j] == a.d[i][j]))return d[i][j] < a.d[i][j];
      }
    }
    return false;
  }

class Top{
public:
  int d[3][3];
  bool operator<(const Top & a)const{
    rep(i,3)
      rep(j,3)if (d[i][j] != a.d[i][j])return d[i][j] < a.d[i][j];
    return false;
  }
};

void rotate_r(Dice &x){
  rotate_swap(x,top,left,bottom,right);
}

void rotate_l(Dice &x){
  rotate_swap(x,top,right,bottom,left);
}

void rotate_f(Dice &x){
  rotate_swap(x,top,back,bottom,front);
}

void rotate_b(Dice &x){
  rotate_swap(x,top,front,bottom,back);
}

int dx[]={0,0,1,-1};//
int dy[]={1,-1,0,0};//
void move(state &a,queue<state> &Q,set<state>&S){
  rep(i,4){
    state next=a;
    next.x+=dx[i];
    next.y+=dy[i];
    if (next.x==-1||next.x==3||next.y==-1||next.y==3)continue;
    next.cnt++;
    next.d[a.y][a.x]=next.d[next.y][next.x];
    if (i == 0)rotate_b(next.d[a.y][a.x]);
    if (i == 1)rotate_f(next.d[a.y][a.x]);
    if (i == 2)rotate_l(next.d[a.y][a.x]);
    if (i == 3)rotate_r(next.d[a.y][a.x]);
      
    if (S.find(next) == S.end()){
      S.insert(next);
      Q.push(next);
    }
  }
}

void make_ini(state &ini,int now,set<state>&S,queue<state> &Q){
  if (now == 9){
    if (S.find(ini) == S.end()){
      Q.push(ini);
      S.insert(ini);
    }
    return;
  }
  
  int y=now/3,x=now%3;

  if (ini.y == y && ini.x == x){
    make_ini(ini,now+1,S,Q);
  }else if (ini.d[y][x].top ==  WHITE){
    ini.d[y][x].front=ini.d[y][x].back =BLUE;
    ini.d[y][x].left =ini.d[y][x].right=RED;
    make_ini(ini,now+1,S,Q);

    ini.d[y][x].front=ini.d[y][x].back =RED;
    ini.d[y][x].left =ini.d[y][x].right=BLUE;
    make_ini(ini,now+1,S,Q);
  }else if (ini.d[y][x].top == BLUE){
    ini.d[y][x].front=ini.d[y][x].back =WHITE;
    ini.d[y][x].left =ini.d[y][x].right=RED;
    make_ini(ini,now+1,S,Q);

    ini.d[y][x].front=ini.d[y][x].back =RED;
    ini.d[y][x].left =ini.d[y][x].right=WHITE;
    make_ini(ini,now+1,S,Q);
  }else if (ini.d[y][x].top  == RED){
    ini.d[y][x].front=ini.d[y][x].back =WHITE;
    ini.d[y][x].left =ini.d[y][x].right=BLUE;
    make_ini(ini,now+1,S,Q);
    
    ini.d[y][x].front=ini.d[y][x].back =BLUE;
    ini.d[y][x].left =ini.d[y][x].right=WHITE;
    make_ini(ini,now+1,S,Q);
  }
}

int bfs(state ini,set<state> &G,bool isfromgoal){
  queue<state> Q;
  set<state> S;
  if (isfromgoal){
    make_ini(ini,0,S,Q);
    
  }else {  
    Q.push(ini);
    G.insert(ini);
  }
  int ans = 31;
  while(!Q.empty()){
    state now = Q.front();Q.pop();
    if (isfromgoal){
      set<state>::iterator itr = G.find(now);
      if (itr != G.end()){
	//	cout << (*itr).cnt <<" " << now.cnt << endl;
      	ans=min(ans,(*itr).cnt+now.cnt);
      }
      if (ans != 31)continue;
      if (now.cnt >=10)continue;
      move(now,Q,S);
    }else {
      if (now.cnt >=20)continue;
      move(now,Q,G);
    }
  }

  //  cout <<"check " <<  S.size() <<" " << G.size() <<  endl;
  if (ans == 31)return -1;
  else return ans;
}

main(){
  set<state>S[3][3];
  rep(i,3){
    rep(j,3){
      state ini(i,j);
      bfs(ini,S[i][j],false);
    }
  }


  int x,y;
  while(cin>>x>>y && x){
    //    cout << x <<" " << y << endl;
    x--;y--;
    state ini(y,x);
    
    rep(i,3){
      rep(j,3){
	char tmp;
	cin>>tmp;
	if (tmp == 'W'){
	  ini.d[i][j].top=WHITE;
	  ini.d[i][j].bottom=WHITE;
	}else if (tmp == 'E'){
	  ini.y=i;
	  ini.x=j;
	}else if (tmp == 'B'){
	  ini.d[i][j].top=BLUE;
	  ini.d[i][j].bottom=BLUE;
	}else if (tmp == 'R'){
	  ini.d[i][j].top=RED;
	  ini.d[i][j].bottom=RED;
	}
      }
    }

    cout << bfs(ini,S[y][x],true) << endl;
  }
}