Problem:
Problem D:
Dice Room
You are playing a popular video game which is famous for its depthful story and interesting
puzzles. In the game you were locked in a mysterious house alone and there is no way to call for
help, so you have to escape on yours own. However, almost every room in the house has some
kind of puzzles and you cannot move to neighboring room without solving them.
One of the puzzles you encountered in the house is following. In a room, there was a device
which looked just like a dice and laid on a table in the center of the room. Direction was written
on the wall. It read:
"This cube is a remote controller and you can manipulate a remote room, Dice
      Room, by it. The room has also a cubic shape whose surfaces are made up of 3x3
      unit squares and some squares have a hole on them large enough for you to go though
      it. You can rotate this cube so that in the middle of rotation at least one edge always
      touch the table, that is, to 4 directions. Rotating this cube affects the remote room
      in the same way and positions of holes on the room change. To get through the
      room, you should have holes on at least one of lower three squares on the front and
      back side of the room."
You can see current positions of holes by a monitor. Before going to Dice Room, you should
rotate the cube so that you can go though the room. But you know rotating a room takes some
time and you don’t have much time, so you should minimize the number of rotation. How many
rotations do you need to make it possible to get though Dice Room?
Input
The input consists of several datasets. Each dataset contains 6 tables of 3x3 characters which
describe the initial position of holes on each side. Each character is either '
*
' or '
.
'. A hole is
indicated by '
*
'. The order which six sides appears in is: front, right, back, left, top, bottom.
The order and orientation of them are described by this development view:
Figure 4: Dice Room
Figure 5: available dice rotations
There is a blank line after each dataset. The end of the input is indicated by a single '
#
'.
Output
Print the minimum number of rotations needed in a line for each dataset. You may assume all
datasets have a solution.
Sample Input
...
...
.*.
...
...
.*.
...
...
...
...
...
.*.
...
...
.*.
...
...
...

...
.*.
...
*..
...
..*
*.*
*.*
*.*
*.*
.*.
*.*
*..
.*.
..*
*.*
...
*.*

...
.*.
.*.
...
.**
*..
...
...
.*.
.*.
...
*..
..*
...
.**
...
*..
...

#
Output for the Sample Input
3
1
0

Submitted solutions:

code_snippet: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	vector<vector<string>>s(6, vector<string>(3));
	//	while (getline(cin, s[0][0]), s[0][0].size() >= 3) {
	while (cin >> s[0][0], s[0][0].size() >= 3) {
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 3; j++) {
				if (!i && !j)continue;
				cin >> s[i][j];
			}
		}
		map<vector<vector<string>>, int>dis;
		dis[s] = 0;
		queue<vector<vector<string>>>Q;
		Q.push(s);
		while (!Q.empty()) {
			auto t = Q.front();
			//cout << "dice" << endl;
			//for (auto i : t) {
			//	for (auto j : i)cout << j << endl;
			//}
			Q.pop();
			{
				auto nt = t;
				for (int i = 0; i < 3; i++) {
					for (int j = 0; j < 3; j++) {
						nt[0][i][j] = t[0][j][2 - i];
						nt[1][i][j] = t[4][j][2 - i];
						nt[2][i][j] = t[2][2 - j][i];
						nt[3][i][j] = t[5][j][2 - i];
						nt[4][i][j] = t[3][j][2 - i];
						nt[5][i][j] = t[1][j][2 - i];
					}
				}
				if (dis.find(nt)==dis.end()) {
					dis[nt] = dis[t] + 1;
					Q.push(nt);
				}
				auto nnt = nt;
				{
					for (int loop = 0; loop < 2; loop++) {
						for (int i = 0; i < 3; i++) {
							for (int j = 0; j < 3; j++) {
								nnt[0][i][j] = nt[0][j][2 - i];
								nnt[1][i][j] = nt[4][j][2 - i];
								nnt[2][i][j] = nt[2][2 - j][i];
								nnt[3][i][j] = nt[5][j][2 - i];
								nnt[4][i][j] = nt[3][j][2 - i];
								nnt[5][i][j] = nt[1][j][2 - i];
							}
						}
						nt = nnt;
					}
					if (dis.find(nnt) == dis.end()) {
						dis[nnt] = dis[t] + 1;
						Q.push(nt);
					}
				}
			}
			{
				auto nt = t;
				for (int i = 0; i < 3; i++) {
					for (int j = 0; j < 3; j++) {
						nt[0][i][j] = t[5][i][j];
						nt[1][i][j] = t[1][j][2 - i];
						nt[2][i][j] = t[4][2 - i][2 - j];
						nt[3][i][j] = t[3][2 - j][i];
						nt[4][i][j] = t[0][i][j];
						nt[5][i][j] = t[2][2 - i][2 - j];
					}
				}
				if (dis.find(nt) == dis.end()) {
					dis[nt] = dis[t] + 1;
					Q.push(nt);
				}
				auto nnt = nt;
				{
					for (int loop = 0; loop < 2; loop++) {
						for (int i = 0; i < 3; i++) {
							for (int j = 0; j < 3; j++) {
								nnt[0][i][j] = nt[5][i][j];
								nnt[1][i][j] = nt[1][j][2 - i];
								nnt[2][i][j] = nt[4][2 - i][2 - j];
								nnt[3][i][j] = nt[3][2 - j][i];
								nnt[4][i][j] = nt[0][i][j];
								nnt[5][i][j] = nt[2][2 - i][2 - j];
							}
						}
						nt = nnt;
					}
					if (dis.find(nnt) == dis.end()) {
						dis[nnt] = dis[t] + 1;
						Q.push(nt);
					}
				}
			}
		}
		int ans = MOD;
		for (auto i : dis) {
			if ((i.first[0][2][0] == '*' || i.first[0][2][1] == '*' || i.first[0][2][2] == '*') && (i.first[2][2][0] == '*' || i.first[2][2][1] == '*' || i.first[2][2][2] == '*')) {
				ans = min(ans, i.second);
			}
		}
		cout << ans << endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3256
final_rank: 7

code_snippet: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

struct State{
  string grid[6][3];
  bool operator < (const State& s) const {
    REP(i, 6) REP(j, 3) if(grid[i][j] != s.grid[i][j]) return grid[i][j] < s.grid[i][j];
    return false;
  }
};
bool is_goal(State& s){
  return s.grid[0][2].find("*") != string::npos && s.grid[2][2].find("*") != string::npos;
}

void copy(string src[3], string dst[3]){
  REP(i, 3) dst[i] = src[i];
}

void rotate(string s[6][3], int a, int b, int c, int d){
  a--; b--; c--; d--;
  // a -> b -> c -> d
  string tmp[3];
  copy(s[d], tmp);
  copy(s[c], s[d]);
  copy(s[b], s[c]);
  copy(s[a], s[b]);
  copy(tmp, s[a]);
}

void revolve90(string s[3]){
  string tmp[3];
  copy(s, tmp);
  REP(y, 3) REP(x, 3){
    s[y][x] = tmp[2 - x][y];
  }
}

void revolve(string s[6][3], int k, int t){
  REP(_, t) revolve90(s[k - 1]);
}

State rotate(State s, int r){
  if(r >= 2){
    return rotate(rotate(rotate(s, r - 2), r - 2), r - 2);
  }
  State next;
  REP(i, 6) copy(s.grid[i], next.grid[i]);
  if(r == 0) rotate(next.grid, 1, 5, 3, 6);
  if(r == 1) rotate(next.grid, 2, 5, 4, 6);
  if(r == 0) {
    revolve(next.grid, 3, 2);
    revolve(next.grid, 6, 2);

    revolve(next.grid, 2, 1);
    revolve(next.grid, 4, 3);
  }
  if(r == 1){
    revolve(next.grid, 5, 1);
    revolve(next.grid, 4, 1);
    revolve(next.grid, 6, 1);
    revolve(next.grid, 2, 1);

    revolve(next.grid, 1, 3);
    revolve(next.grid, 3, 1);
  }
  return next;
}

void print(State s){
  REP(i, 6){
    printf("--- %d ---\n", i + 1);
    REP(j, 3) cout << s.grid[i][j] << endl;
  }
}

int main(){
  while(true){
    State start;
    REP(i, 6) REP(j, 3) cin >> start.grid[i][j];
    if(cin.eof()) break;
    map<State, int> dist;
    queue<State> que;
    dist[start] = 0;
    que.push(start);
    while(!que.empty()){
      State s = que.front(); que.pop();
      if(is_goal(s)){
        cout << dist[s] << endl;
        break;
      }
      REP(r, 4){
        State next = rotate(s, r);
        if(!dist.count(next)){
          dist[next] = dist[s] + 1;
          que.push(next);
        }
      }
    }
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1256
final_rank: 4

code_snippet: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <iostream>
#include <string>
#include <set>
#define X first
#define Y second
#define sqr(x) (x)*(x)
using namespace std;
const double PI = acos(-1.0);
map<int,int>::iterator it;
typedef long long LL ;
#pragma comment(linker,"/STACK:102400000,102400000")

struct face
{
    int p[3][3];
    face rotate_left()
    {
        face ret;
        for(int i=0;i<3;++i)
        {
            for(int j=0;j<3;++j)
            {
                ret.p[2-j][i]=p[i][j];
            }
        }
        return ret;
    }
    face rotate_right()
    {
        face ret = rotate_left();
        ret = ret.rotate_left();
        ret = ret.rotate_left();
        return ret;
    }
    void input()
    {
        char s[5][5];
        for(int i=0;i<3;++i)
        {
            scanf("%s",s[i]);
        }
        for(int i=0;i<3;++i)
        {
            for(int j=0;j<3;++j)
            {
                p[i][j]=(s[i][j]=='.')?0:1;
            }
        }
    }
    void pf()
    {
        for(int i=0;i<3;++i)
        {
            for(int j=0;j<3;++j)
            {
                printf("%d",p[i][j]);
            }
            puts("");
        }
    }
};




struct cube
{
    face f[8];
    int dis;
    cube left()
    {
        cube ret ;
        ret.f[0] = (*this).f[0].rotate_left();
        ret.f[1] = (*this).f[5].rotate_right();
        ret.f[2] = (*this).f[2].rotate_right();
        ret.f[3] = (*this).f[4].rotate_right();
        ret.f[4] = (*this).f[1].rotate_right();
        ret.f[5] = (*this).f[3].rotate_right();
        return ret;
    }
    cube right()
    {
        cube ret;
        ret.f[0] = (*this).f[0].rotate_right();
        ret.f[1] = (*this).f[4].rotate_left();
        ret.f[2] = (*this).f[2].rotate_left();
        ret.f[3] = (*this).f[5].rotate_left();
        ret.f[4] = (*this).f[1].rotate_left();
        ret.f[5] = (*this).f[3].rotate_left();
        return ret;
    }
    cube forward()
    {
        cube ret;
        ret.f[0] = (*this).f[5];
        ret.f[1] = (*this).f[1].rotate_right();
        ret.f[2] = (*this).f[4].rotate_left();
        ret.f[2] = ret.f[2].rotate_left();
        ret.f[3] = (*this).f[3].rotate_left();
        ret.f[4] = (*this).f[0];
        ret.f[5] = (*this).f[2].rotate_left();
        ret.f[5] = ret.f[5].rotate_left();
        return ret;
    }
    cube back()
    {
        cube ret = (*this).forward();
        ret = ret.forward();
        ret = ret.forward();
        return ret;
    }
    bool good()
    {
        bool ok1=0,ok2=0;
        for(int i=0;i<3;++i)
        {
            if(f[0].p[2][i]==1)ok1=1;
            if(f[2].p[2][i]==1)ok2=1;
        }
        return ok1&&ok2;
    }
    void pf()
    {
        for(int i=0;i<6;++i)
        {
            f[i].pf();
        }
    }
};
cube C;
int res;
void dfs(cube u,int depth)
{
    if(depth>9)return ;
    if(u.good())
    {
        //printf("d = %d\n",depth);
        res = min(res,depth);
    }
    else
    {
        cube v = u.back();
        dfs(v,depth+1);
        v = u.forward();
        dfs(v,depth+1);
        v = u.left();
        dfs(v,depth+1);
        v = u.right();
        dfs(v,depth+1);
    }
}
int main()
{
    char s[8][8];
//    face ff;
//    ff.input();
//    ff.pf();
//    ff=ff.rotate_left();
//    ff.pf();
//    ff=ff.rotate_left();
//    ff.pf();

    while(1)
    {
        scanf("%s",s[0]);
        if(s[0][0]=='#')break;
        for(int i=1;i<3;++i)scanf("%s",s[i]);
        for(int i=0;i<3;++i)
        {
            for(int j=0;j<3;++j)
            {
                C.f[0].p[i][j]=(s[i][j]=='.')?0:1;
            }
        }
        for(int i=1;i<6;++i)C.f[i].input();
        //puts("OK");
        res=10000;
        //C.f[0].pf();
        //C.f[2].pf();
//        C.pf();
//        C = C.left();
//        puts("");
//        C.pf();
//        C = C.right();
//        puts("");
//        C.pf();
        dfs(C,0);
        printf("%d\n",res);
    }
	return 0;
}
accuracy: 1
time_ms: 1190
memory_kb: 1148
final_rank: 8

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cassert>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
using namespace std;

string Message[] = {"Front","Right","Back","Left","Top","Bottom"};
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};

class Box
{
public:
  vector<vector<string> > box;
  int used;  

  Box(){
    used = 0;
    box.resize(6);
    for(int i=0;i<6;i++)box[i].resize(3);
  }

  vector<string> rotate90(vector<string> &x)
  {
    vector<string> ret = x;
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
	ret[j][2-i] = x[i][j];

    return ret;
  }

  vector<string> rotate270(vector<string> &x)
  {
    vector<string> ret = x;
    for(int i=0;i<3;i++)
      ret = rotate90(ret);
    return ret;
  }

  vector<vector<string> > R1(vector<vector<string> > &x)
  {
    vector<vector<string> > r = x;
    string pre[3];
    for(int i=0;i<3;i++)pre[i] = r[2][i];

    //2 <- 4
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
	r[2][2-i][2-j] = r[4][i][j];

    //4 <- 0
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
	r[4][i][j] = r[0][i][j];

    //0 <- 5
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
	r[0][i][j] = r[5][i][j];

    //5 <- 2
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
	r[5][2-i][2-j] = pre[i][j];

    r[1] = rotate90(r[1]);
    r[3] = rotate270(r[3]);

    return r;
  }

  vector<vector<string> > R2(vector<vector<string> > &x)
  {
    vector<vector<string> > r = x;
    for(int i=0;i<3;i++)
      r = R1(r);
    return r;
  }

  vector<vector<string> > R3(vector<vector<string> > &x)
  {
    vector<vector<string> > r = x;
    string pre[3];
    for(int i=0;i<3;i++)pre[i] = r[4][i];

    //4 <- 1
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
	r[4][2-j][i] = r[1][i][j];

    //1 <- 5
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
	r[1][2-j][i] = r[5][i][j];


    //5 <- 3
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
	r[5][2-j][i] = r[3][i][j];


    //3 <- 4
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
	r[3][2-j][i] = pre[i][j];


    r[0] = rotate270(r[0]);
    r[2] = rotate90(r[2]);
    return r;
  }

  vector<vector<string> > R4(vector<vector<string> > &x)
  {
    vector<vector<string> > ret = x;
    for(int i=0;i<3;i++)
      ret = R3(ret);
    return ret;
  }


  void print()
  {
    cout << "print--------------" << endl;
    for(int k=0;k<6;k++)
      {
	cout << Message[k] << "*************" << endl;
	for(int i=0;i<3;i++)
	  {
	    for(int j=0;j<3;j++)
	      {
		cout << box[k][i][j];
	      }
	    cout << endl;
	  }
      }
    cout << endl;
  }

  bool isOK(int x)
  {
    queue<pair<int,int> > que;
    bool used[3][3];
    for(int i=0;i<3;i++)for(int j=0;j<3;j++)used[i][j] = false;
    if(box[x][0][0] != '*')
      que.push(pair<int,int>(0,0)),used[0][0] = true;
    if(box[x][0][1] != '*')
      que.push(pair<int,int>(1,0)),used[0][1] = true;
    if(box[x][0][2] != '*')
      que.push(pair<int,int>(2,0)),used[0][2] = true;

    while(!que.empty())
      {
	pair<int,int> p = que.front(); que.pop();
	if(p.second == 2)return true;
	for(int i=0;i<4;i++)
	  {
	    int nx = p.first + dx[i];
	    int ny = p.second + dy[i];
	    if(!(0 <= nx && nx < 3 && 0 <= ny && ny < 3))continue;
	    if(used[ny][nx])continue;
	    used[ny][nx] = true;
	    que.push(pair<int,int>(nx,ny));
	  }

      }
    return false;
  }

  bool isFin()
  {
    bool p1 = false;
    bool p2 = false;
    if(box[0][2][0] == '*')p1 = true;
    if(box[0][2][1] == '*')p1 = true;
    if(box[0][2][2] == '*')p1 = true;
    if(box[2][2][0] == '*')p2 = true;
    if(box[2][2][1] == '*')p2 = true;
    if(box[2][2][2] == '*')p2 = true;

    return p1 & p2;
  }

};

typedef pair<Box,int> P;

int main()
{
  while(true)
    {
      Box box;
      rep(k,6)
	{
	  rep(i,3)
	    {
	      string line;
	      cin >> line;	
	      box.box[k][i] = line;
	      if(line == "#")
		{
		  assert(i == 0 && k == 0);
		  goto F;		
		}
	    }
	}

      bool found = false;
	  queue<P> que;
	  que.push(P(box,0));
	  while(!que.empty())
	    {
	      P p = que.front(); que.pop();
	      if(p.first.isFin())
		{
		  found = true;
		  cout << p.second << endl;
		  break;
		}

	      for(int i=0;i<4;i++)
		{
		  if((p.first.used >> i) & 1)continue;
		  Box bx = p.first;
		  if(i == 0)
		    bx.box = bx.R1(bx.box),p.first.used <<= (1<<i);
		  else if(i == 1)
		    bx.box = bx.R2(bx.box),p.first.used <<= (1<<i);
		  else if(i == 2)
		    bx.box = bx.R3(bx.box),p.first.used <<= (1<<i);
		  else if(i == 3)
		    bx.box = bx.R4(bx.box),p.first.used <<= (1<<i);
		  que.push(P(bx,p.second+1));
		}
	    }
	  assert(found);

    }
 F:;
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1352
final_rank: 6

code_snippet: #include<iostream>
#include<sstream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>

#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

const int r[][6] ={
	{1, 2, 3, 0, 4, 5},
	{5, 1, 4, 3, 0, 2},
	{0, 4, 2, 5, 3, 1}
};
vector<vector<string> >in;
void rot(int it, vector<string> &f){
	while(it--){
		vector<string> nx(3, string("..."));
		rep(i, 3) rep(j, 3) nx[i][j] = f[2 - j][i];
		swap(nx, f);
	}
}
vector<vector<string> > rot(int d, const vector<vector<string> > &in){
	vector<vector<string> > res;
	rep(i, 6) res.pb(in[r[d][i]]);
	if(d == 0){
		rot(1, res[4]); rot(3, res[5]);
	}
	if(d == 1){
		rot(1, res[1]); rot(3, res[3]); rot(2, res[5]); rot(2, res[2]);
	}
	if(d == 2){
		rot(1, res[0]); rot(3, res[2]); rot(1, res[1]); rot(1, res[4]); rot(1, res[3]); rot(1, res[5]);
	}
	return res;
}

int main(){
	in = vector<vector<string> >(6, vector<string>(3, "..."));
	while(1){
		rep(i, 6) rep(j, 3){
			cin >> in[i][j];
			if(in[i][j] == "#") exit(0);
		}
		
		set<vector<vector<string> > > s;
		queue<pair<int, vector<vector<string> > > > q;
		q.push(mp(0, in));
		while(!q.empty()){
			in = q.front().second;
			int c = q.front().first; q.pop();
			if(s.count(in)) continue;
			s.insert(in);
			
			bool ok1 = 0, ok2 = 0;
			rep(i, 3){
				ok1 |= in[0][2][i] == '*';
				ok2 |= in[2][2][i] == '*';
			}
			if(ok1 && ok2){
				cout << c << endl;
				goto END;
			}
			
			for(int d = 1; d < 3; d++) rep(it, 4){
				in = rot(d, in);
				if(it == 0 || it == 2) q.push(mp(c + 1, in));
			}
		}
		cout << -1 << endl;
		END:;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1300
final_rank: 5

code_snippet: #include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

vector<string> rotate(int n, const vector<string>& s)
{
    if(n == 0)
        return s;

    vector<string> t(3, string(3, ' '));
    for(int i=0; i<3; ++i){
        for(int j=0; j<3; ++j){
            t[j][2-i] = s[i][j];
        }
    }
    return rotate(n-1, t);
}

class Dice
{
public:
    vector<vector<string> > pip; // 東南西北上下の目
    Dice(){
        pip.assign(6, vector<string>(3, string(3, ' ')));
    }
    void roll(int d){ // 東南西北右左に回転
        int dir[][4] = {{4,2,5,0},{4,3,5,1},{4,0,5,2},{4,1,5,3},{0,3,2,1},{0,1,2,3}};
        vector<string> tmp = pip[dir[d][0]];
        for(int i=0; i<3; ++i)
            pip[dir[d][i]] = pip[dir[d][i+1]];
        pip[dir[d][3]] = tmp;

        if(d == 0){
            pip[1] = rotate(3, pip[1]);
            pip[2] = rotate(2, pip[2]);
            pip[3] = rotate(1, pip[3]);
            pip[5] = rotate(2, pip[5]);
        }else if(d == 1){
            pip[0] = rotate(1, pip[0]);
            pip[2] = rotate(3, pip[2]);
        }else if(d == 2){
            pip[0] = rotate(2, pip[0]);
            pip[1] = rotate(1, pip[1]);
            pip[3] = rotate(3, pip[3]);
            pip[5] = rotate(2, pip[5]);
        }else{
            pip[0] = rotate(3, pip[0]);
            pip[2] = rotate(1, pip[2]);
        }
    }
    bool operator==(const Dice& d) const{
        return pip == d.pip;
    }
};

int solve(Dice d0)
{
    set<vector<vector<string> > > s;
    queue<vector<vector<string> > > q;
    s.insert(d0.pip);
    q.push(d0.pip);

    int ret = 0;
    for(;;){
        int n = q.size();
        while(--n >= 0){
            Dice d;
            d.pip= q.front();
            q.pop();

            int tmp = 0;
            for(int i=0; i<3; ++i){
                if(d.pip[1][2][i] == '*')
                    tmp |= 1;
                if(d.pip[3][0][i] == '*')
                    tmp |= 2;
            }
            if(tmp == 3)
                return ret;

            for(int i=0; i<4; ++i){
                Dice d2 = d;
                d2.roll(i);
                if(s.find(d2.pip) == s.end()){
                    s.insert(d2.pip);
                    q.push(d2.pip);
                }
            }
        }
        ++ ret;
    }
}

int main()
{
    int to[] = {1, 0, 3, 2, 4, 5};

    for(;;){
        Dice d;
        for(int i=0; i<6; ++i){
            for(int j=0; j<3; ++j){
                cin >> d.pip[to[i]][j];
                if(d.pip[to[i]][j] == "#")
                    return 0;
            }
        }
        d.pip[0] = rotate(3, d.pip[0]);
        d.pip[2] = rotate(1, d.pip[2]);
        d.pip[3] = rotate(2, d.pip[3]);

        cout << solve(d) << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 0
final_rank: 1

code_snippet: #include <iostream>
#include <fstream>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define EPS 1e-9
#define INF MOD
#define MOD 1000000007LL
#define fir first
#define iss istringstream
#define sst stringstream
#define ite iterator
#define ll long long
#define mp make_pair
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<n;i++)
#define pi pair<int,int>
#define pb push_back
#define sec second
#define sh(i) (1LL<<i)
#define sz size()
#define vi vector<int>
#define vl vector<ll>
#define vs vector<string>

#define vc vector<char>
#define C vector<vc>

C c;

vc r90(vc v){
	vc r;
	rep(i,3)for(int j=2;j>=0;j--)r.pb(v[j*3+i]);
	return r;
}

vc l90(vc v){
	vc r;
	for(int i=2;i>=0;i--)rep(j,3)r.pb(v[j*3+i]);
	return r;
}

vc r180(vc v){
	vc r;
	for(int i=2;i>=0;i--)for(int j=2;j>=0;j--)r.pb(v[i*3+j]);
	return r;
}

C rotf(C c){
	C r;
	r.pb(c[5]);
	r.pb(r90(c[1]));
	r.pb(r180(c[4]));
	r.pb(l90(c[3]));
	r.pb(c[0]);
	r.pb(r180(c[2]));
	return r;
}

C rotr(C c){
	C r;
	r.pb(r90(c[0]));
	r.pb(r90(c[4]));
	r.pb(l90(c[2]));
	r.pb(r90(c[5]));
	r.pb(r90(c[3]));
	r.pb(r90(c[1]));
	return r;
}

C rotba(C c){
	C r;
	r.pb(c[4]);
	r.pb(l90(c[1]));
	r.pb(r180(c[5]));
	r.pb(r90(c[3]));
	r.pb(r180(c[2]));
	r.pb(c[0]);
	return r;
}

C rotl(C c){
	C r;
	r.pb(l90(c[0]));
	r.pb(l90(c[5]));
	r.pb(r90(c[2]));
	r.pb(l90(c[4]));
	r.pb(l90(c[1]));
	r.pb(l90(c[3]));
	return r;
}

int main(){
	c.resize(6);
	rep(i,6)c[i].resize(9);
	while(cin>>c[0][0]){
		if(c[0][0]=='#')break;
		rep(i,8)cin>>c[0][i+1];
		rep(i,5)rep(j,9)cin>>c[i+1][j];
		map<C,int> M;
		queue<C> Q;
		M[c]=0;
		Q.push(c);
		while(1){
			C c=Q.front();Q.pop();
			int s=M[c];
			if((c[0][6]=='*'||c[0][7]=='*'||c[0][8]=='*')&&(c[2][6]=='*'||c[2][7]=='*'||c[2][8]=='*')){
				cout<<s<<endl;
				break;
			}
			C c0=rotf(c),c1=rotr(c),c2=rotba(c),c3=rotl(c);
			if(M.find(c0)==M.end())M[c0]=s+1,Q.push(c0);
			if(M.find(c1)==M.end())M[c1]=s+1,Q.push(c1);
			if(M.find(c2)==M.end())M[c2]=s+1,Q.push(c2);
			if(M.find(c3)==M.end())M[c3]=s+1,Q.push(c3);
		}
	}
}
accuracy: 1
time_ms: 10
memory_kb: 0
final_rank: 1

code_snippet: #include <iostream>
#include <vector>
#include <queue>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)

typedef vector<string> S;

//3*3ツづ個配ツ療アツづーツ篠楪計ツ嘉アツづィ90ツ度ツ嘉アツ転
S rot90(S s){
	S res = s;
	rep(i,3) rep(j,3) res[j][2-i] = s[i][j];
	return res;
}
S rot180(S s){return rot90(rot90(s));}
S rot270(S s){return rot90(rot180(s));}

class Dice{
public:
	vector<string> t,b,w,e,n,s;

	Dice(S _t,S _b,S _w,S _e,S _n,S _s){
		t = _t;
		b = _b;
		w = _w;
		e = _e;
		n = _n;
		s = _s;
	}

	Dice move(int d){
		if(d == 0)
			return Dice(s,rot180(n),rot90(w),rot270(e),rot180(t),b);
		else
			return Dice(rot270(w),rot270(e),rot270(b),rot270(t),rot90(n),rot270(s));
	}

	bool isAns(void){
		return
			(s[2][0]=='*' || s[2][1]=='*' || s[2][2]=='*') &&
			(n[2][0]=='*' || n[2][1]=='*' || n[2][2]=='*');
	}
};

int solve(Dice d){
	typedef pair<Dice,int> P;
	queue<P> open;
	open.push(P(d,0));

	while(!open.empty()){
		P p = open.front(); open.pop();

		if(p.first.isAns()){
			return p.second;
		}

		open.push(P(p.first.move(0),p.second+1));
		open.push(P(p.first.move(1),p.second+1));
		open.push(P(p.first.move(0).move(0).move(0),p.second+1));
		open.push(P(p.first.move(1).move(1).move(1),p.second+1));
	}

	return -1;
}

int main(void){
	while(1){
		S s[6];
		rep(i,6) rep(j,3){
			string str;
			cin>>str;
			s[i].push_back(str);
			if(s[i][j] == "#") return 0;
		}

		Dice d(s[4],s[5],s[3],s[1],s[2],s[0]);
		cout<<solve(d)<<endl;
	}

	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 988
final_rank: 3