Problem:
Problem E:
Driving an Icosahedral Rover
After decades of fruitless efforts, one of the expedition teams of ITO (Intersolar Tourism Organization) finally found a planet that would surely provide one of the best tourist attractions
within a ten light-year radius from our solar system. The most attractive feature of the planet,
besides its comfortable gravity and calm weather, is the area called
Mare Triangularis
. Despite
the name, the area is not covered with water but is a great plane. Its unique feature is that it is
divided into equilateral triangular sections of the same size, called
trigons
. The
trigons
provide
a unique impressive landscape, a must for tourism. It is no wonder the board of ITO decided
to invest a vast amount on the planet.
Despite the expected secrecy of the staff, the Society of Astrogeology caught this information
in no time, as always. They immediately sent their president's letter to the Institute of Science
and Education of the Commonwealth Galactica claiming that authoritative academic inspections
were to be completed before any commercial exploitation might damage the nature.
Fortunately, astrogeologists do not plan to practice all the possible inspections on all of the
trigons
; there are far too many of them. Inspections are planned only on some characteristic
trigons
and, for each of them, in one of twenty different scientific aspects.
To accelerate building this new tourist resort, ITO's construction machinery team has already succeeded in putting their brand-new
invention in practical use. It is a rover vehicle of the shape of an
icosahedron
, a regular polyhedron with twenty faces of equilateral triangles. 
The machine is customized
so that each of the twenty faces exactly fits each of the
trigons
. 
Controlling the high-tech
gyromotor
installed inside its body, the rover can roll onto one of the three
trigons
neighboring the one its bottom is
on.
Figure E.1: The Rover on Mare Triangularis
Each of the twenty faces has its own function. The set of equipments installed on the bottom
face touching the ground can be applied to the
trigon
it is on. Of course, the rover was meant to accelerate construction of the luxury hotels to host rich interstellar travelers, but, changing
the installed equipment sets, it can also be used to accelerate academic inspections.
You are the driver of this rover and are asked to move the vehicle onto the
trigon
specified by
the leader of the scientific commission with the smallest possible steps. What makes your task
more difficult is that the designated face installed with the appropriate set of equipments has to
be the bottom. The direction of the rover does not matter.
Figure E.2:The Coordinate System
Figure E.3: Face Numbering
The
trigons
of
Mare Triangularis
are given two-dimensional coordinates as shown in Figure E.2.
Like maps used for the Earth, the
x
axis is from the west to the east, and the
y
axis is from the
south to the north. Note that all the trigons with its coordinates 
...(truncated)

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;

const int nt[20][3]={
{5,1,4},
{6,2,0},
{7,3,1},
{8,4,2},
{9,0,3},
{0,10,11},
{1,11,12},
{2,12,13},
{3,13,14},
{4,14,10},
{15,5,9},
{16,6,5},
{17,7,6},
{18,8,7},
{19,9,8},
{10,19,16},
{11,15,17},
{12,16,18},
{13,17,19},
{14,18,15}};
const int tx[2][3]={{0,1,-1},{0,-1,1}},ty[2][3]={{1,0,0},{-1,0,0}};
map<pair<pair<int,int>,pair<int,int> >,int> mp;
queue<pair<pair<int,int>,pair<int,int> > > q;
pair<pair<int,int>,pair<int,int> > t,tt;

int main()
{
	int x,y,k,p,tp,xx,yy,kk,pp,s,i;
	t=make_pair(make_pair(0,0),make_pair(0,0));
	mp[t]=0;
	q.push(t);
	while(!q.empty())
	{
		t=q.front();
		q.pop();
		x=t.first.first;y=t.first.second;
		k=t.second.first;p=t.second.second;
		tp=(x+y)&1;
		for(i=0;i<3;i++)
		{
			xx=x+tx[tp][i];yy=y+ty[tp][i];
			kk=nt[k][(i+p)%3];
			for(pp=0;pp<3;pp++)
				if(nt[kk][pp]==k)
					break;
			pp=(pp+3-i)%3;
			if(!mp.count(tt=make_pair(make_pair(xx,yy),make_pair(kk,pp)))||mp[t]+1<mp[tt])
				if((mp[tt]=mp[t]+1)<100)
					q.push(tt);
		}
	}
	while(~scanf("%d%d%d",&x,&y,&k)&&(x||y||k))
	{
		s=1<<30;
		for(i=0;i<3;i++)
			if(mp.count(t=make_pair(make_pair(x,y),make_pair(k,i))))
				s=min(s,mp[t]);
		printf("%d\n",s);
	}
	return 0;
}
accuracy: 1
time_ms: 850
memory_kb: 59832
final_rank: 15

code_snippet: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
#include<cassert>
#include<set>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<LL,int> LP;
const int INF=1<<30;
const LL MAX=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}

int next_num(int cur,int to){
    to%=3;
    if(to==0){
        if(cur%10<5)return cur+5;
        return cur-5;
    }
    if(cur<5)to=3-to;
    if(cur<5 || cur>=15){
        if(to==1){
            if(cur%5==0)cur+=5;
            return cur-1;
        }
        cur++;
        if(cur%5==0)cur-=5;
        return cur;
    }
    if(cur<10){
        if(to==1)return cur+5;
        cur+=6;
        if(cur==15)cur=10;
        return cur;
    }
    if(to==1)return cur-5;
    cur-=6;
    if(cur==4)cur=9;
    return cur;
}

P next_pos(P pos,int to){
    to%=3;
    int a=(to-1)*2-1;
    if((pos.first+pos.second+(1<<20))%2){
        if(to==0)return make_pair(pos.first,pos.second-1);
        return make_pair(pos.first+a,pos.second);
    }else{
        if(to==0)return make_pair(pos.first,pos.second+1);
        return make_pair(pos.first-a,pos.second);
    }
}

int dis[300][300][30][5];
const int N=150;
queue<tuple<P,int,P,int,int>> q1;//pos,num,bef_pos,bef_num,dis

int main(){
    int n,m;
    int i,j,k;
    int a,b,c;
    int s;
    int num;
    P pos,bef_pos,pos2;
    memset(dis,-1,sizeof(dis));
    q1.push(make_tuple(P(N,N),0,P(N,N+1),5,0));
    while(!q1.empty()){
        tie(pos,a,bef_pos,b,c)=q1.front();q1.pop();
        if(c>100)continue;
        for(i=0;i<3;i++){
            if(bef_pos==next_pos(pos,i))break;
        }
        for(j=0;j<3;j++){
            if(b==next_num(a,j))break;
        }
        b=(j-i+3)%3;
        if(dis[pos.first][pos.second][a][b]!=-1)continue;
        dis[pos.first][pos.second][a][b]=c;
        for(i=0;i<3;i++){
            pos2=next_pos(pos,i);
            num=next_num(a,i+b);
            q1.push(make_tuple(pos2,num,pos,a,c+1));
        }
    }
    while(cin>>a>>b>>c){
        if(a==0 && b==0 && c==0)break;
        a+=N,b+=N;
        s=1e3;
        for(i=0;i<3;i++){
            if(dis[a][b][c][i]!=-1)s=min(s,dis[a][b][c][i]);
        }
        cout<<s<<endl;
    }
}
accuracy: 1
time_ms: 90
memory_kb: 57048
final_rank: 11

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define NUM 20

enum Type{
	UP,
	R_UP,
	R_DOWN,
	DOWN,
	L_DOWN,
	L_UP,
};

struct Info{
	void set(int arg_next_center,int arg_next_state){
		next_center = arg_next_center;
		next_state = arg_next_state;
	}
	int next_center,next_state;
};

struct State{
	State(int arg_center,int arg_state, int arg_x,int arg_y, int arg_sum_step){
		center = arg_center;
		state = arg_state;
		x = arg_x;
		y = arg_y;
		sum_step = arg_sum_step;
	}
	bool operator<(const struct State &arg) const{

		return sum_step > arg.sum_step; //ステップの昇順(PQ)
	}
	int center,state,x,y,sum_step;
};

bool check[NUM][NUM];
int base[NUM][6] = { {-1,4,-1,1,-1,5}, //0
					{-1,0,-1,2,-1,6}, //1
					{-1,1,-1,3,-1,7}, //2
					{-1,2,-1,4,-1,8}, //3
					{-1,3,-1,0,-1,9}, //4
					{-1,0,-1,11,-1,10}, //5
					{-1,1,-1,12,-1,11}, //6,
					{-1,2,-1,13,-1,12}, //7
					{-1,3,-1,14,-1,13}, //8
					{-1,4,-1,10,-1,14}, //9
					{-1,9,-1,5,-1,15}, //10
					{-1,16,-1,5,-1,6}, //11
					{-1,7,-1,17,-1,6}, //12
					{-1,7,-1,8,-1,18}, //13
					{-1,9,-1,19,-1,8}, //14
					{-1,10,-1,16,-1,19}, //15
					{-1,11,-1,17,-1,15}, //16
					{-1,16,-1,12,-1,18}, //17
					{-1,17,-1,13,-1,19}, //18
					{-1,14,-1,15,-1,18}}; //19

int STATES[NUM][6][6];
int min_dist[210][210][NUM],work[210][210][NUM][6];
Info info[NUM][6][6];
Type type_array[6] = {UP,R_UP,R_DOWN,DOWN,L_DOWN,L_UP};
Type rev_type[6] = {DOWN,L_DOWN,L_UP,UP,R_UP,R_DOWN};


int main(){

	for(int i = 0; i < NUM; i++){

		for(int k = 0; k < 6; k++){

			STATES[i][0][k] = base[i][k];
		}
		//手で持って見た時の遷移
		for(int k = 1; k <= 5; k++){

			STATES[i][k][UP] = STATES[i][k-1][L_UP];
			STATES[i][k][R_UP] = STATES[i][k-1][UP];
			STATES[i][k][R_DOWN] = STATES[i][k-1][R_UP];
			STATES[i][k][DOWN] = STATES[i][k-1][R_DOWN];
			STATES[i][k][L_DOWN] = STATES[i][k-1][DOWN];
			STATES[i][k][L_UP] = STATES[i][k-1][L_DOWN];
		}
	}

	//遷移表の作成
	for(int from = 0; from < NUM; from++){ //遷移元の中央
		for(int from_state = 0; from_state < 6; from_state++){
			for(int a = 0; a < 6;a++){ //転がる方向
				Type dir = type_array[a];

				if(STATES[from][from_state][dir] == -1){

					info[from][from_state][dir].set(-1,-1);

					continue;
				}

				int to = STATES[from][from_state][dir];
				Type next_type = rev_type[dir];
				for(int b = 0; b < 6; b++){
					if(STATES[to][b][next_type] == from){

						info[from][from_state][dir].set(to,b);
					}
				}
			}
		}
	}

	int add_X = 100,add_Y = 100;
	for(int i = 0; i < 210; i++){
		for(int k = 0; k < 210; k++){
			for(int a = 0; a < NUM; a++){

				min_dist[i][k][a] = BIG_NUM;
				for(int b = 0; b < 6; b++){

					work[i][k][a][b] = BIG_NUM;
				}
			}
		}
	}

	min_dist[add_X][add_Y][0] = 0;
	work[add_X][add_Y][0][1] = 0;
	priority_queue<State> Q;
	Q.push(State(0,1,0,0,0));

	int next_center,next_state,next_x,next_y,next_step;

	while(!Q.empty()){

		if(Q.top().sum_step == 100){

			Q.pop();

		}else if(Q.top().sum_step > work[Q.top().x+add_X][Q.top().y+add_Y][Q.top().center][Q.top().state]){

			Q.pop();

		}else{

			for(int i = 0; i < 6; i++){
				if(info[Q.top().center][Q.top().state][i].next_center == -1)continue;

				next_center = info[Q.top().center][Q.top().state][i].next_center;
				next_state = info[Q.top().center][Q.top().state][i].next_state;

				Type dir = type_array[i];
				next_x = Q.top().x;
				next_y = Q.top().y;

				next_step = Q.top().sum_step+1;

				//さいころの遷移は目で見た時と逆
				switch(dir){
				case UP:
					next_y++;
					break;

				case L_UP:
					next_x++;
					break;

				case L_DOWN:
					next_x++;
					break;

				case DOWN:
					next_y--;
					break;

				case R_DOWN:
					next_x--;
					break;

				case R_UP:
					next_x--;
					break;
				}

				if(work[next_x+add_X][next_y+add_Y][next_center][next_state] > next_step){
					work[next_x+add_X][next_y+add_Y][next_center][next_state] = next_step;
					min_dist[next_x+add_X][next_y+add_Y][next_center] = min(min_dist[next_x+add_X][next_y+add_Y][next_center],next_step);
					Q.push(State(next_center,next_state,next_x,next_y,next_step));
				}
			}
			Q.pop();
		}
	}

	int tmp_x,tmp_y,tmp_center;

	while(true){

		scanf("%d %d %d",&tmp_x,&tmp_y,&tmp_center);
		if(tmp_x == 0 && tmp_y == 0 && tmp_center == 0)break;

		printf("%d\n",min_dist[tmp_x+add_X][tmp_y+add_Y][tmp_center]);
	}

	return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 27444
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;

constexpr int inf = 1e9;

template<typename T>
std::vector<T> table(int n, T v) { return std::vector<T>(n, v); }

template <class... Args>
auto table(int n, Args... args) {
    auto val = table(args...);
    return std::vector<decltype(val)>(n, std::move(val));
}

constexpr int dx[3] = {1, 0, -1};
constexpr int dy[2][3] = {
    {0, 1, 0}, // (x + y) % 2 == 0
    {0, -1, 0}
};

constexpr int df[20][2][3] = {
    {{1, 5, 4}, {4, 5, 1}},
    {{2, 6, 0}, {0, 6, 2}},
    {{3, 7, 1}, {1, 7, 3}},
    {{4, 8, 2}, {2, 8, 4}},
    {{0, 9, 3}, {3, 9, 0}},
    {{10, 0, 11}, {11, 0, 10}},
    {{11, 1, 12}, {12, 1, 11}},
    {{12, 2, 13}, {13, 2, 12}},
    {{13, 3, 14}, {14, 3, 13}},
    {{14, 4, 10}, {10, 4, 14}},
    {{5, 15, 9}, {9, 15, 5}},
    {{6, 16, 5}, {5, 16, 6}},
    {{7, 17, 6}, {6, 17, 7}},
    {{8, 18, 7}, {7, 18, 8}},
    {{9, 19, 8}, {8, 19, 9}},
    {{19, 10, 16}, {16, 10, 19}},
    {{15, 11, 17}, {17, 11, 15}},
    {{16, 12, 18}, {18, 12, 16}},
    {{17, 13, 19}, {19, 13, 17}},
    {{18, 14, 15}, {15, 14, 18}}
};
constexpr int dd[3][2][3] = { // only used on 0, ..., 4, 15, ..., 19
    {{2, 0, 1}, {1, 0, 2}},
    {{1, 2, 0}, {0, 2, 1}},
    {{0, 1, 2}, {2, 1, 0}}
};

int main() {
    constexpr int max_x = 201, max_y = 102;
    constexpr int cx = 100, cy = 50;
    auto in_range = [] (int x, int y) {
        return 0 <= x && x < max_x && 0 <= y && y < max_y;
    };

    int gx, gy, n;
    while(cin >> gx >> gy >> n, gx | gy | n) {
        gx += cx, gy += cy;
        auto d = table(max_x, max_y, 20, 3, inf);
        d[cx][cy][0][0] = 0;
        queue<tuple<int, int, int, int>> que;
        que.emplace(cx, cy, 0, 0);
        int ans = inf;
        while(!que.empty()) {
            int x, y, f, dir; tie(x, y, f, dir) = que.front();
            que.pop();
            if(x == gx && y == gy && f == n) {
                ans = d[x][y][f][dir];
                break;
            }
            for(int i = 0; i < 3; ++i) {
                const int nx = x + dx[i], ny = y + dy[(x + y) & 1][i];
                int nf = 0, nd = 0;
                if((x + y) & 1) {
                    nf = df[f][1][(i - dir + 3) % 3];
                } else {
                    nf = df[f][0][(i + dir) % 3];
                }
                if((5 <= f && f < 15) || (5 <= nf && nf < 15)) {
                    nd = dir;
                } else {
                    nd = dd[dir][(x + y) & 1][i];
                }
                if(!in_range(nx, ny) || d[nx][ny][nf][nd] != inf) continue;
                d[nx][ny][nf][nd] = d[x][y][f][dir] + 1;
                que.emplace(nx, ny, nf, nd);
            }
        }

        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 1750
memory_kb: 26416
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

const int N = 230;
const int A = 108;

const int INF = 19191919;
const int F = 20;
int dp[N][N][F][F];

struct State{
    int x,y,b,s;
};

int nxt[20][3]={
    {5,1,4},
    {6,2,0},
    {7,3,1},
    {8,4,2},
    {9,0,3},
    {0,10,11}, //5
    {1,11,12},
    {2,12,13},
    {3,13,14},
    {4,14,10},
    {15,5,9}, //10
    {16,6,5},
    {17,7,6},
    {18,8,7},
    {19,9,8},
    {10,19,16}, //15
    {11,15,17},
    {12,16,18},
    {13,17,19},
    {14,18,15}
};

const int dx[2][3]={{0,1,-1},{0,-1,1}};
const int dy[2][3]={{1,0,0},{-1,0,0}};

const int LIM = 100;

int main(){
    rep(i,N)rep(j,N)rep(k,F)rep(l,F) dp[i][j][k][l] = INF;
    dp[A][A][0][5] = 0;
    queue<State> que;
    que.push({A,A,0,5});
    while(!que.empty()){
        State c = que.front();
        que.pop();
        if(dp[c.x][c.y][c.b][c.s]>=LIM) continue;

        // printf("NOW : %d %d %d %d   ::: %d\n",c.x,c.y,c.b,c.s,dp[c.x][c.y][c.b][c.s]);

        int ad = 0;
        while(nxt[c.b][ad] != c.s) ++ad;

        int oe = (c.x+c.y)%2;
        rep(i,3){
            int nx = c.x+dx[oe][i], ny = c.y+dy[oe][i];
            if(!(0<=nx && nx<N && 0<=ny && ny<N)) continue;
            int noe = (nx+ny)%2;

            int nb = nxt[c.b][(i+ad)%3];
            int ns = -1;
            rep(j,3){
                int xx = nx+dx[noe][j], yy = ny+dy[noe][j];
                if(xx == c.x && yy == c.y){
                    int idx = 0;
                    while(nxt[nb][idx] != c.b) ++idx;

                    idx = (idx-j+3)%3;
                    ns = nxt[nb][idx];
                    break;
                }
            }
            assert(ns != -1);

            // printf(" nx %d :: %d %d %d %d\n",i,nx,ny,nb,ns);
            if(dp[nx][ny][nb][ns] > dp[c.x][c.y][c.b][c.s]+1){
                dp[nx][ny][nb][ns] = dp[c.x][c.y][c.b][c.s]+1;
                que.push({nx,ny,nb,ns});
            }
        }
    }

    int x,y,n;
    while(cin >>x >>y >>n){
        if(x==0 && y==0 && n==0) break;
        int ans = INF;
        rep(i,F) ans = min(ans, dp[x+A][y+A][n][i]);
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 85876
final_rank: 16

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>
#include <cstring>
using namespace std;
typedef pair<int, int> pii;
typedef vector<pii> vpi;

struct info{
    int x,y,b,t;
    int step;
    bool isEven;
    info(int x, int y, int b, int t, int s, bool e):x(x),y(y),b(b),t(t),step(s),isEven(e){}
    info(){}
};
bool used[203][103][20][20];

int main(){
    vector<vector<vector<pii> > > next(20, vector<vector<pii> >(20));
    //next, 右左上
    //odd, 左右下
    next[0][1] = vpi{pii{4, 9}, pii{5, 10}, pii{1, 0}};
    next[0][4] = vpi{pii{5, 11}, pii{1, 6}, pii{4, 0}};
    next[0][5] = vpi{pii{1, 2}, pii{4, 3}, pii{5, 0}};
    next[1][0] = vpi{pii{6, 12}, pii{2, 7}, pii{0, 1}};
    next[1][2] = vpi{pii{0, 5}, pii{6, 11}, pii{2, 1}};
    next[1][6] = vpi{pii{2, 3}, pii{0, 4}, pii{6, 1}};
    next[2][1] = vpi{pii{7, 13}, pii{3, 8}, pii{1, 2}};
    next[2][3] = vpi{pii{1, 6}, pii{7, 12}, pii{3, 2}};
    next[2][7] = vpi{pii{3, 4}, pii{1, 0}, pii{7, 2}};
    next[3][2] = vpi{pii{8, 14}, pii{4, 9}, pii{2, 3}};
    next[3][4] = vpi{pii{2, 7}, pii{8, 13}, pii{4, 3}};
    next[3][8] = vpi{pii{4, 0}, pii{2, 1}, pii{8, 3}};
    next[4][0] = vpi{pii{3, 8}, pii{9, 14}, pii{0, 4}};
    next[4][3] = vpi{pii{9, 10}, pii{0, 5}, pii{3, 4}};
    next[4][9] = vpi{pii{0, 1}, pii{3, 2}, pii{9, 4}};
    next[5][0] = vpi{pii{10, 15}, pii{11, 16}, pii{0, 5}};
    next[5][10] = vpi{pii{11, 6}, pii{0, 1}, pii{10, 5}};
    next[5][11] = vpi{pii{0, 4}, pii{10, 9}, pii{11, 5}};
    next[6][1] = vpi{pii{11, 16}, pii{12, 17}, pii{1, 6}};
    next[6][11] = vpi{pii{12, 7}, pii{1, 2}, pii{11, 6}};
    next[6][12] = vpi{pii{1, 0}, pii{11, 5}, pii{12, 6}};
    next[7][2] = vpi{pii{12, 17}, pii{13, 18}, pii{2, 7}};
    next[7][12] = vpi{pii{13, 8}, pii{2, 3}, pii{12, 7}};
    next[7][13] = vpi{pii{2, 1}, pii{12, 6}, pii{13, 7}};
    next[8][3] = vpi{pii{13, 18}, pii{14, 19}, pii{3, 8}};
    next[8][13] = vpi{pii{14, 9}, pii{3, 4}, pii{13, 8}};
    next[8][14] = vpi{pii{3, 2}, pii{13, 7}, pii{14, 8}};
    next[9][4] = vpi{pii{14, 19}, pii{10, 15}, pii{4, 9}};
    next[9][10] = vpi{pii{4, 3}, pii{14, 8}, pii{10, 9}};
    next[9][14] = vpi{pii{10, 5}, pii{4, 0}, pii{14, 9}};
    next[10][5] = vpi{pii{9, 14}, pii{15, 19}, pii{5, 10}};
    next[10][9] = vpi{pii{15, 16}, pii{5, 11}, pii{9, 10}};
    next[10][15] = vpi{pii{5, 0}, pii{9, 4}, pii{15, 10}};
    next[11][5] = vpi{pii{16, 17}, pii{6, 12}, pii{5, 11}};
    next[11][6] = vpi{pii{5, 10}, pii{16, 15}, pii{6, 11}};
    next[11][16] = vpi{pii{6, 1}, pii{5, 0}, pii{16, 11}};
    next[12][6] = vpi{pii{17, 18}, pii{7, 13}, pii{6, 12}};
    next[12][7] = vpi{pii{6, 11}, pii{17, 16}, pii{7, 12}};
    next[12][17] = vpi{pii{7, 2}, pii{6, 1}, pii{17, 12}};
    next[13][7] = vpi{pii{18, 19}, pii{8, 14}, pii{7, 13}};
    next[13][8] = vpi{pii{7, 12}, pii{18, 17}, pii{8, 13}};
    next[13][18] = vpi{pii{8, 3}, pii{7, 2}, pii{18, 13}};
    next[14][8] = vpi{pii{19, 15}, pii{9, 10}, pii{8, 14}};
    next[14][9] = vpi{pii{8, 13}, pii{19, 18}, pii{9, 14}};
    next[14][19] = vpi{pii{9, 4}, pii{8, 3}, pii{19, 14}};
    next[15][10] = vpi{pii{19, 18}, pii{16, 17}, pii{10, 15}};
    next[15][16] = vpi{pii{10, 9}, pii{19, 14}, pii{16, 15}};
    next[15][19] = vpi{pii{16, 11}, pii{10, 5}, pii{19, 15}};
    next[16][11] = vpi{pii{15, 19}, pii{17, 18}, pii{11, 16}};
    next[16][15] = vpi{pii{17, 12}, pii{11, 6}, pii{15, 16}};
    next[16][17] = vpi{pii{11, 5}, pii{15, 10}, pii{17, 16}};
    next[17][12] = vpi{pii{16, 15}, pii{18, 19}, pii{12, 17}};
    next[17][16] = vpi{pii{18, 13}, pii{12, 7}, pii{16, 17}};
    next[17][18] = vpi{pii{12, 6}, pii{16, 11}, pii{18, 17}};
    next[18][13] = vpi{pii{17, 16}, pii{19, 15}, pii{13, 18}};
    next[18][17] = vpi{pii{19, 14}, pii{13, 8}, pii{17, 18}};
    next[18][19] = vpi{pii{13, 7}, pii{17, 12}, pii{19, 18}};
    next[19][14] = vpi{pii{18, 17}, pii{15, 16}, pii{14, 19}};
    next[19][15] = vpi{pii{14, 8}, pii{18, 13}, pii{15, 19}};
    next[19][18] = vpi{pii{15, 10}, pii{14, 9}, pii{18, 19}};

    while(1){
        int tx,ty,n;
        cin >> tx >> ty >> n;
        if(tx==0 && ty==0 && n==0) break;

        memset(used, 0, sizeof(used));
        queue<info> wait;
        wait.push(info(0, 0, 0, 5, 0, true));
        used[101][51][0][5] = true;
        int ans = -1;
        while(!wait.empty()){
            int x = wait.front().x;
            int y = wait.front().y;
            int b = wait.front().b;
            int t = wait.front().t;
            int step = wait.front().step;
            int isEven = wait.front().isEven;
            wait.pop();
            if(x == tx && y == ty && b == n){
                ans = step;
                break;
            }
            int dx[3], dy[3];
            if(isEven){
                dx[0] = 1; dx[1] = -1; dx[2] = 0;
                dy[0] = 0; dy[1] = 0; dy[2] = 1;
            }else{
                dx[0] = -1; dx[1] = 1; dx[2] = 0;
                dy[0] = 0; dy[1] = 0; dy[2] = -1;
            }
            for(int i=0; i<3; i++){
                int nx = x +dx[i];
                int ny = y +dy[i];
                int nb = next[b][t][i].first;
                int nt = next[b][t][i].second;
                if(used[nx+101][ny+51][nb][nt]) continue;
                used[nx+101][ny+51][nb][nt] = true;
                wait.push(info(nx, ny, nb, nt, step+1, !isEven));
            }
        }
        cout << ans << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 390
memory_kb: 11556
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
typedef pair<Int, Int> P;

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

vector<vector<P> > nx
{
  {P(0,5),P(1,6),P(2,7),P(3,8),P(4,9)},
  {P(0,1),P(4,3),P(9,14),P(10,15),P(5,11)},
  {P(0,4),P(5,10),P(11,16),P(6,12),P(1,2)},
  {P(1,0),P(6,11),P(12,17),P(7,13),P(2,3)},
  {P(2,1),P(7,12),P(13,18),P(8,14),P(3,4)},
  {P(3,2),P(8,13),P(14,19),P(9,10),P(4,0)},
  {P(5,0),P(10,9),P(15,19),P(16,17),P(11,6)},
  {P(6,1),P(11,5),P(16,15),P(17,18),P(12,7)},
  {P(7,2),P(12,6),P(17,16),P(18,19),P(13,8)},
  {P(8,3),P(13,7),P(18,17),P(19,15),P(14,9)},
  {P(9,4),P(14,8),P(19,18),P(15,16),P(10,5)},
  {P(15,10),P(19,14),P(18,13),P(17,12),P(16,11)}
};

signed char dp[203][203][21][21][6];
struct st{
  Int y,x,n,m,d;
  st(Int y,Int x,Int n,Int m,Int d):y(y),x(x),n(n),m(m),d(d){}
};
const Int CC = 101;
signed main(){  
  cin.tie(0);
  ios::sync_with_stdio(0);
  {
    memset(dp,-1,sizeof(dp));
    queue<st> q;
    dp[CC][CC][0][5][0]=0;
    q.emplace(CC,CC,0,5,0);
    Int dyl[6]={0,0,1,0,0,-1};
    Int dxl[6]={-1,-1,0,1,1,0};
    Int dyr[6]={0,-1,0,0,1,0};
    Int dxr[6]={1,0,-1,-1,0,1};
    Int dyo[6]={1,0,0,-1,0,0};    
    Int dxo[6]={0,1,1,0,-1,-1};

    auto push=[&](st a,st b){
      if(~dp[a.y][a.x][a.n][a.m][a.d]) return;
      dp[a.y][a.x][a.n][a.m][a.d]=dp[b.y][b.x][b.n][b.m][b.d]+1;
      q.emplace(a);
    };
    
    while(!q.empty()){
      st s=q.front();q.pop();
      if(dp[s.y][s.x][s.n][s.m][s.d]>100) break;
      
      Int k=0,l=0;
      for(Int i=0;i<12;i++)
	for(Int j=0;j<5;j++)
	  if(nx[i][j]==P(s.n,s.m)) k=i,l=j;
      
      //cout<<k<<" "<<l<<endl;
      
      {// L
	st t=s;
	t.y+=dyl[s.d];
	t.x+=dxl[s.d];
	tie(t.n,t.m)=nx[k][(l+4)%5];
	t.d=(s.d+5)%6;	
	push(t,s);
      }
      {// R
	st t=s;
	t.y+=dyr[s.d];
	t.x+=dxr[s.d];
	tie(t.n,t.m)=nx[k][(l+1)%5];
	t.d=(s.d+1)%6;	
	push(t,s);
      }
      {// O
	st t=s;
	t.y+=dyo[s.d];
	t.x+=dxo[s.d];
	swap(t.n,t.m);
	t.d=(s.d+3)%6;	
	push(t,s);
      }
    }
    
    
  }
  
  
  {
    Int x,y,n;
    while(cin>>x>>y>>n,x||y||n){
      short ans=101;
      x+=CC;y+=CC;
      for(Int i=0;i<20;i++){
	for(Int j=0;j<6;j++){
	  if(dp[y][x][n][i][j]<0) continue;
	  chmin(ans,dp[y][x][n][i][j]);
	}
      }
      cout<<ans<<endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 109956
final_rank: 18

code_snippet: #include <bits/stdc++.h>
#define int long long
using namespace std;

unsigned char dp[205][205][20][20]; // dp  zyanai kedo

int even[20][3]={
  {5,4,1},
  {6,0,2},
  {7,1,3},
  {8,2,4},
  {9,3,0},
  {0,11,10},
  {1,12,11},
  {2,13,12},
  {3,14,13},
  {4,10,14},
  {15,9,5},
  {16,5,6},
  {17,6,7},
  {18,7,8},
  {19,8,9},
  {10,16,19},
  {11,17,15},
  {12,18,16},
  {13,19,17},
  {14,15,18}
};
int odd[20][3]={
  {5,4,1},
  {6,0,2},
  {7,1,3},
  {8,2,4},
  {9,3,0},
  {0,11,10},
  {1,12,11},
  {2,13,12},
  {3,14,13},
  {4,10,14},
  {15,9,5},
  {16,5,6},
  {17,6,7},
  {18,7,8},
  {19,8,9},
  {10,16,19},
  {11,17,15},
  {12,18,16},
  {13,19,17},
  {14,15,18}
};
int odx[]={0,-1,1};
int ody[]={-1,0,0};
int edx[]={0,-1,1};
int edy[]={1,0,0};
void init(){
  for(int i=0;i<20;i++){
    swap(odd[i][1],odd[i][2]);
  }
  for(int i=0;i<205;i++)
    for(int j=0;j<205;j++)
      for(int k=0;k<20;k++)
        for(int l=0;l<20;l++)
          dp[i][j][k][l]=200;
      }

      struct point{
        int cost,x,y,muki,sita;
      };

      point A;

      void BFS(){
        A.sita=0;
        A.cost=0;
        A.muki=5;
        A.x=102;
        A.y=102;
        queue<point>q;
        q.push(A);
        dp[0][0][0][5]=0;
        while(!q.empty()){
          point P=q.front();q.pop();
          int y=P.y;
          int x=P.x;
          int muki=P.muki;
          int cost=P.cost;
    int sita=P.sita;     //cout<<y<<' '<<x<<' '<<sita<<endl;
    //cout<<x-102<<' '<<y-120<<' '<<sita<<' '<<cost<<endl;
    //if(cost==5)return;


    if(cost%2){
      int k=0;
      for(int i=0;i<3;i++){
        if(odd[sita][i]==muki){
          k=i;
          break;
        }
      }//continue;
      for(int i=0;i<3;i++,k=(k+1)%3){
        int yy=y+ody[i];
        int xx=x+odx[i];
        if( yy< 0 || xx< 0 || 204<xx || 204<yy )continue;
        int nex_sita=odd[sita][k];
        int nex_muki=sita;
        int l=0;
        //if(cost==1&&x==101&&y==102)cout<<i<<' '<<sita<<' '<<nex_sita<<' '<<muki<<endl;
        for(int j=0;j<3;j++){
          if(even[nex_sita][j]==sita){
            l=j;
            break;
          }
        }
        if(i==0){
          nex_muki=sita;
        }
        else if(i==1){
          nex_muki=even[nex_sita][(l+1)%3];
        }
        else{
          nex_muki=even[nex_sita][(l+2)%3];
        }
        if(dp[yy][xx][nex_sita][nex_muki]!=200)continue;
        //if(cost==1&&x==101&&y==102)cout<<l<<' '<<nex_sita<<' '<<nex_muki<<endl;
        //if(cost==1&&xx==101&&yy==102&&cost==1)cout<<sita<<' '<<nex_sita<<' '<<cost<<endl;
        dp[yy][xx][nex_sita][nex_muki]=cost+1;
        A.sita=nex_sita;
        A.cost=cost+1;
        A.muki=nex_muki;
        A.x=xx;
        A.y=yy;
        q.push(A);
      }
    }
    else{
      int k=0;
      for(int i=0;i<3;i++){
        if(even[sita][i]==muki){
          k=i;
          break;
        }
      }//continue;
      for(int i=0;i<3;i++,k=(k+1)%3){
        int yy=y+edy[i];
        int xx=x+edx[i];
        if( yy< 0 || xx< 0 || 204<xx || 204<yy )continue;
        int nex_sita=even[sita][k];
        int nex_muki=sita;
        int l=0;
        //if(cost==0&&xx==0+102&&yy==101&&cost==2)cout<<sita<<' '<<nex_sita<<' '<<cost<<endl;
        for(int j=0;j<3;j++){
          if(odd[nex_sita][j]==sita){
            l=j;
            break;
          }
        }
        if(i==0){
          nex_muki=sita;
        }
        else if(i==1){
          nex_muki=odd[nex_sita][(l+1)%3];
        }
        else{
          nex_muki=odd[nex_sita][(l+2)%3];
        }
        if(dp[yy][xx][nex_sita][nex_muki]!=200)continue;
        //if(cost==0&&xx==101&&yy==102&&cost==0)cout<<"even"<<i<<' '<<l<<' '<<sita<<' '<<nex_sita<<' '<<nex_muki<<endl;
        //if(cost==2&&xx==0+102&&yy==101&&cost==2)cout<<sita<<' '<<nex_sita<<' '<<cost<<endl;
        dp[yy][xx][nex_sita][nex_muki]=cost+1;
        A.sita=nex_sita;
        A.cost=cost+1;
        A.muki=nex_muki;
        A.x=xx;
        A.y=yy;
        q.push(A);
      }
    }
  }
}

signed main(){
  int x,y,c;
  init();
  BFS();
  while(cin>>x>>y>>c){
    if(!x&&!y&&!c)break;
    x+=102;
    y+=102;
    int ans=1e9;
    for(int i=0;i<20;i++){
      ans=min(ans,(int)dp[y][x][c][i]);
    }
    cout<<ans<<endl;
  }

  return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 20116
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
typedef pair<Int, Int> P;

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

vector<vector<P> > nx
{
  {P(0,5),P(1,6),P(2,7),P(3,8),P(4,9)},
  {P(0,1),P(4,3),P(9,14),P(10,15),P(5,11)},
  {P(0,4),P(5,10),P(11,16),P(6,12),P(1,2)},
  {P(1,0),P(6,11),P(12,17),P(7,13),P(2,3)},
  {P(2,1),P(7,12),P(13,18),P(8,14),P(3,4)},
  {P(3,2),P(8,13),P(14,19),P(9,10),P(4,0)},
  {P(5,0),P(10,9),P(15,19),P(16,17),P(11,6)},
  {P(6,1),P(11,5),P(16,15),P(17,18),P(12,7)},
  {P(7,2),P(12,6),P(17,16),P(18,19),P(13,8)},
  {P(8,3),P(13,7),P(18,17),P(19,15),P(14,9)},
  {P(9,4),P(14,8),P(19,18),P(15,16),P(10,5)},
  {P(15,10),P(19,14),P(18,13),P(17,12),P(16,11)}
};

signed char dp[203][203][21][21][6];
struct st{
  Int y,x,n,m,d;
  st(Int y,Int x,Int n,Int m,Int d):y(y),x(x),n(n),m(m),d(d){}
};
const Int CC = 101;
signed main(){  
  cin.tie(0);
  ios::sync_with_stdio(0);
  {
    memset(dp,-1,sizeof(dp));
    queue<st> q;
    dp[CC][CC][0][5][0]=0;
    q.emplace(CC,CC,0,5,0);
    Int dyl[6]={0,0,1,0,0,-1};
    Int dxl[6]={-1,-1,0,1,1,0};
    Int dyr[6]={0,-1,0,0,1,0};
    Int dxr[6]={1,0,-1,-1,0,1};
    Int dyo[6]={1,0,0,-1,0,0};    
    Int dxo[6]={0,1,1,0,-1,-1};

    auto push=[&](st a,st b){
      if(~dp[a.y][a.x][a.n][a.m][a.d]) return;
      dp[a.y][a.x][a.n][a.m][a.d]=dp[b.y][b.x][b.n][b.m][b.d]+1;
      q.emplace(a);
    };
    
    while(!q.empty()){
      st s=q.front();q.pop();
      if(dp[s.y][s.x][s.n][s.m][s.d]>100) break;
      
      Int k=0,l=0;
      for(Int i=0;i<12;i++)
	for(Int j=0;j<5;j++)
	  if(nx[i][j]==P(s.n,s.m)) k=i,l=j;
      
      //cout<<k<<" "<<l<<endl;
      
      {// L
	st t=s;
	t.y+=dyl[s.d];
	t.x+=dxl[s.d];
	tie(t.n,t.m)=nx[k][(l+4)%5];
	t.d=(s.d+5)%6;	
	push(t,s);
      }
      {// R
	st t=s;
	t.y+=dyr[s.d];
	t.x+=dxr[s.d];
	tie(t.n,t.m)=nx[k][(l+1)%5];
	t.d=(s.d+1)%6;	
	push(t,s);
      }
      {// O
	st t=s;
	t.y+=dyo[s.d];
	t.x+=dxo[s.d];
	swap(t.n,t.m);
	t.d=(s.d+3)%6;	
	push(t,s);
      }
    }
    
    
  }
  
  
  {
    Int x,y,n;
    while(cin>>x>>y>>n,x||y||n){
      short ans=101;
      x+=CC;y+=CC;
      for(Int i=0;i<20;i++){
	for(Int j=0;j<6;j++){
	  if(dp[y][x][n][i][j]<0) continue;
	  chmin(ans,dp[y][x][n][i][j]);
	}
      }
      cout<<ans<<endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 109956
final_rank: 18

code_snippet: #include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <utility>

const int drct[2][3][2] = {{{0, 1}, {-1, 0}, {1, 0}}, {{0, -1}, {1, 0}, {-1, 0}}};

int sd_x, sd_y, bottom;

struct State {
	int x, y, bot, up, step;
	
	State() {}
	State(int x, int y, int bot, int up, int step): 
		x(x), y(y), bot(bot), up(up), step(step) {}
};

int NEXT(int x, int t) {
	switch (t) {
		case 0:
			if (x < 5 || (x >= 10 && x < 15)) return x + 5;
			return x - 5;
		case 1:
			if (x < 5) return (x + 4) % 5;
			if (x < 10) return (x + 5 - 10 + 1) % 5 + 10;
			if (x < 15) return (x - 5 - 5 + 4) % 5 + 5;
			return (x - 15 + 1) % 5 + 15;
		case 2:
			if (x < 5) return (x + 1) % 5;
			if (x < 10) return x + 5;
			if (x < 15) return x - 5;
			return (x - 15 + 4) % 5 + 15;
	}
	
	return -1;
}

std::pair<int, int> NEXT(int x, int up, int t) {
	int a = NEXT(x, (up + t) % 3), b;
	switch ((up + t) % 3) {
		case 0:
			b = 0; break;
		case 1:
			if (x < 5 || x >= 15) b = 1;
			else b = 0;
			break;
		case 2:
			if (x < 5 || x >= 15) b = 2;
			else b = 0;
			break;
	}
	return std::make_pair(a, (b + up) % 3);
}

int dist[205][205][20][3];

void BFS() {
	static std::queue<State> que;
	static State now;
	
	que.push(State(0, 0, 0, 0, 0));
	dist[0 + 100][0 + 100][0][0] = 0;
	while (!que.empty()) {
		now = que.front(); que.pop();
		if (now.step >= 100) break;
		for (int t = 0; t < 3; ++t) {
			int xx = now.x + drct[(now.x + now.y) & 1][t][0], 
				yy = now.y + drct[(now.x + now.y) & 1][t][1];
			int fir = NEXT(now.bot, now.up, t).first, 
				sec = NEXT(now.bot, now.up, t).second;
			if (~dist[xx + 100][yy + 100][fir][sec]) continue;
			dist[xx + 100][yy + 100][fir][sec] = now.step + 1;
			que.push(State(xx, yy, fir, sec, now.step + 1));
		}
	}
}

int main() {
	memset(dist, -1, sizeof dist);
	BFS();
	
	while (~scanf("%d%d%d", &sd_x, &sd_y, &bottom) && (sd_x || sd_y || bottom)) {
		int ans = 0x3f3f3f3f;
		for (int t = 0; t < 3; ++t) if (~dist[sd_x + 100][sd_y + 100][bottom][t])
			ans = std::min(ans, dist[sd_x + 100][sd_y + 100][bottom][t]);
		printf("%d\n", ans);
	}
	
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 12640
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
 
void rA(int t[20]){
  int u[20]={5,10,15,16,11,0,9,19,17,6,1,4,14,18,12,2,3,8,13,7};
  int tmp[20];
  for(int i=0;i<20;i++)tmp[i]=t[ u[i] ];
  for(int i=0;i<20;i++)t[i]=tmp[i];
}

void rB(int t[20]){
  int u[20]={1,0,5,11,6,2,4,10,16,12,7,3,9,15,17,13,8,14,19,18};
  int tmp[20];
  for(int i=0;i<20;i++)tmp[i]=t[ u[i] ];
  for(int i=0;i<20;i++)t[i]=tmp[i];
}
 
void rC(int t[20]){
  int u[20]={4,9,10,5,0,3,14,15,11,1,2,8,19,16,6,7,13,18,17,12};
  int tmp[20];
  for(int i=0;i<20;i++)tmp[i]=t[ u[i] ];
  for(int i=0;i<20;i++)t[i]=tmp[i];
}
 
void rD(int t[20]){
  int u[20]={5,10,15,16,11,0,9,19,17,6,1,4,14,18,12,2,3,8,13,7};
  int tmp[20];
  for(int i=0;i<20;i++)tmp[i]=t[ u[i] ];
  for(int i=0;i<20;i++)t[i]=tmp[i];
}
 
void rE(int t[20]){
  int u[20]={1,0,5,11,6,2,4,10,16,12,7,3,9,15,17,13,8,14,19,18};
  int tmp[20];
  for(int i=0;i<20;i++)tmp[i]=t[ u[i] ];
  for(int i=0;i<20;i++)t[i]=tmp[i];
}
 
void rF(int t[20]){
  int u[20]={4,9,10,5,0,3,14,15,11,1,2,8,19,16,6,7,13,18,17,12};
  int tmp[20];
  for(int i=0;i<20;i++)tmp[i]=t[ u[i] ];
  for(int i=0;i<20;i++)t[i]=tmp[i];
}
 
struct die{
  int p[20];
  bool operator < ( const die &A ) const {
    for(int i=0;i<20;i++)
      if(p[i]!=A.p[i])return (p[i]<A.p[i]);
    return false;
  }
};
 
 
typedef pair<int,int> P;
typedef pair< P , int > state;
  
typedef pair< P , int > PP;
typedef unsigned int ull;
 
map< ull , int > d;
map< PP , int > ans;
queue< state > Q;
map< die , int > dice;
map< int , die > redice;


int dieId(die d){
  if(dice.count(d)>0)return dice[d];
  int res=dice.size();
  dice[d]=res;
  redice[res]=d;
  return res;
}

int RA(int id){
  static map<int,int> mp;
  if(mp.count(id)>0)return mp[id];
  die d=redice[id];
  rA(d.p);
  mp[id]=dieId(d);
  return mp[id];
}
int RB(int id){
  static map<int,int> mp;
  if(mp.count(id)>0)return mp[id];
  die d=redice[id];
  rB(d.p);
  mp[id]=dieId(d);
  return mp[id];
}
int RC(int id){
  static map<int,int> mp;
  if(mp.count(id)>0)return mp[id];
  die d=redice[id];
  rC(d.p);
  mp[id]=dieId(d);
  return mp[id];
}
int RD(int id){
  static map<int,int> mp;
  if(mp.count(id)>0)return mp[id];
  die d=redice[id];
  rD(d.p);
  mp[id]=dieId(d);
  return mp[id];
}
int RE(int id){
  static map<int,int> mp;
  if(mp.count(id)>0)return mp[id];
  die d=redice[id];
  rE(d.p);
  mp[id]=dieId(d);
  return mp[id];
}
int RF(int id){
  static map<int,int> mp;
  if(mp.count(id)>0)return mp[id];
  die d=redice[id];
  rF(d.p);
  mp[id]=dieId(d);
  return mp[id];
}
 
ull getHash(state s){
  ull res=0, B=5575777;
  int x=s.first.first+150;
  int y=s.first.second+150;
  res=x*B+y;
  die d=redice[s.second];
  for(int i=0;i<20;i++)res=res*B+d.p[i];
  return res;
}
 
void Push(state s,int cost){
  ull key=getHash(s);
  if(d.count(key)>0)return;
  d[key]=cost+1;
  Q.push(s);
}
 
int main(){
  die v;
  for(int i=0;i<20;i++)v.p[i]=i;
  state si=state( P(0,0) , dieId(v) );
  d[ getHash(si) ]=0;
  Q.push(si);
   
  while(!Q.empty()){
    state s=Q.front();Q.pop();
    int cost=d[ getHash(s) ];
     
    int x=s.first.first;
    int y=s.first.second;
    int f=abs(x+y)%2;
    die d=redice[ s.second ];
    die tmpd=d;
    int n=d.p[0];
 
    /*
      cout<<x<<' '<<y<<' '<<cost<<' '<<n<<endl;
      for(int i=0;i<20;i++)cout<<s.second.p[i]<<' ';
      cout<<endl;
      cout<<endl;
    */
    if( ans.count( PP(P(x,y), n ) ) == 0)
      ans[ PP(P(x,y),n) ]=cost;
     
    if(cost==100)continue;
 
     
    if(f==0){
      state A=s;
      A.first=P(x,y+1);
      A.second=RA(s.second);
      Push(A,cost);
       
      state B=s;
      B.first=P(x+1,y);
      B.second=RB(s.second);
      Push(B,cost);
       
      state C=s;
      C.first=P(x-1,y);
      C.second=RC(s.second);
      Push(C,cost);
 
       
    }else{
      state D=s;
      D.first=P(x,y-1);
      rD(d.p);
      D.second=RD(s.second);
      Push(D,cost);
       
      state E=s;
      E.first=P(x-1,y);
      rE(d.p);
      E.second=RE(s.second);
      Push(E,cost);
       
      state F=s;
      F.first=P(x+1,y);
      rF(d.p);
      F.second=RF(s.second);
      Push(F,cost);
    }
  }

  //cout<<dice.size()<<endl;
  
  while(1){
    int x,y,n;
    cin>>x>>y>>n;
    if(x==0&&y==0&&n==0)break;
    cout<< ans[ PP( P(x,y)  , n ) ] <<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 1950
memory_kb: 64708
final_rank: 17

code_snippet: #include "bits/stdc++.h"
using namespace std;
int dx[2][3] = { {0,-1,1},{0,1,-1} };
int dy[2][3] = { {1,0,0},{-1,0,0} };
struct poly {
	int x;
	int y;
	int under;
	int front;
	int left;
	int right;
	int turn;
	int id;
	poly() :x(101), y(101), under(0), front(5),turn(0),id(0) {
		setlr();
	}
	enum Way {
		W_Front,
		W_Left,
		W_Right,
	};
	int typeflr[4][3] = {
		{ 1,0,0 } ,
		{ -1,1,1 },
		{ 1,-1,-1 },
		{ -1,0,0 }
	};
	int dflr[4][3] = {
		{ 0,4,1 },
		{ 0,1,0 },
		{ 0,4,0 },
		{ 0,1,4 },
	};
	//0 5 4 1
	//1 6 0 2
	//5 0 11 10
	//6 1 12 11
	//10 15 9 5
	//11 16 5 6
	//15 10 16 19
	// 16 11 17 15
	void setid() {
		const int ftype = under / 5;
		const int fnum = under % 5;
		bool flag = false;
		for (int i = 0; i < 3; ++i) {
			const int atype = ftype + typeflr[ftype][i];
			const int anum = (fnum + dflr[ftype][i]) % 5;
			const int predict_front = atype * 5 + anum;
			if (predict_front == front) {
				id = i;
				break;
			}
		}
	}
	void setlr() {
		
		const int ftype = under / 5;
		const int fnum = under % 5;
		bool flag = false;
		for (int i = 0; i < 3; ++i) {
			const int atype = ftype + typeflr[ftype][i];
			const int anum = (fnum + dflr[ftype][i])%5;
			const int predict_front = atype * 5 + anum;
			if (predict_front == front) {
				{
					const int atype = ftype + typeflr[ftype][(i+1)%3];
					const int anum = (fnum + dflr[ftype][(i + 1) % 3]) % 5;
					left = atype * 5 + anum;
				}
				{
					const int atype = ftype + typeflr[ftype][(i + 2) % 3];
					const int anum = (fnum + dflr[ftype][(i + 2) % 3]) % 5;
					right= atype * 5 + anum;
				}
				flag = true;
				break;
			}
		}
		assert(flag);

	}
	void rotate_l() {
		int c = left;
		left = right;
		right = front;
		front = c;
	}
	void rotate(const Way way) {
		x += dx[(x + y) % 2][way];
		y += dy[(x + y) % 2][way];
		if (way == W_Front) {
			swap(front, under);
			setlr();
		}
		else if (way == W_Left) {
			swap(left, under);
			{
				rotate_l();
				setlr();
				rotate_l();
				rotate_l();
			}
		}
		else {
			swap(right, under);
			{
				rotate_l();
				rotate_l();
				setlr();
				rotate_l();
			}
		}
		setid();
		turn++;
	}
	void rotate(const int way) {
		rotate(static_cast<Way>(way));
	}
};


int main() {
	while (1) {
		int x, y, u; cin >> x >> y >> u;
		if (!x&&!y&&!u)break;
		x += 101;
		y += 101;

		vector<vector<vector<vector<int>>>>memo(203, vector<vector<vector<int>>>(203, vector<vector<int>>(20, vector<int>(3,1e9))));
		queue<poly>que;
		memo[101][101][0][0] = 0;
		que.push(poly());
		int ans = -1;
		while (!que.empty()) {
			poly atop(que.front());
			if (atop.x == x&&atop.y == y&&atop.under == u) {
				ans = atop.turn;
				break;
			}
			que.pop();
			for (int i = 0; i < 3; ++i) {
				poly apoly(atop);
				apoly.rotate(i);
				if (memo[apoly.x][apoly.y][apoly.under][apoly.id]>apoly.turn) {
					memo[apoly.x][apoly.y][apoly.under][apoly.id] = apoly.turn;
					que.push(apoly);
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 4560
memory_kb: 52104
final_rank: 20

code_snippet: #include<iostream>
#include<algorithm>
#include<tuple>
#include<vector>

using namespace std;

int mem[223][223][22][3];

int neighbors[][3]={
  {5,4,1},
  {6,0,2},
  {7,1,3},
  {8,2,4},
  {9,3,0},
  {0,11,10},
  {1,12,11},
  {2,13,12},
  {3,14,13},
  {4,10,14},
  {15,9,5},
  {16,5,6},
  {17,6,7},
  {18,7,8},
  {19,8,9},
  {10,16,19},
  {11,17,15},
  {12,18,16},
  {13,19,17},
  {14,15,18},
};

int main(){
  fill(***begin(mem),***end(mem),1e9);
  vector<tuple<int,int,int,int> > vs[123];
  vs[0].emplace_back(0,0,0,0);
  for(int i=0;i<=100;i++){
    //    cout<<"vs = "<<vs[i].size()<<endl;
    while(!vs[i].empty()){
      int y,x,f,d;
      tie(y,x,f,d)=vs[i].back();
      vs[i].pop_back();
      if(mem[y+100][x+100][f][d]<1e8){
	//	cout<<i<<endl;
	continue;
      }
      //      cout<<i<<' '<<x<<' '<<y<<' '<<f<<' '<<d<<endl;
      mem[y+100][x+100][f][d]=i;
      int n[3];
      for(int j=0;j<3;j++){
	n[j]=neighbors[f][(d+j)%3];
      }
      for(int j=0;j<3;j++){
	static int dy[][3]={{1,0,0},{0,-1,0}};
	static int dx[][3]={{0,-1,1},{-1,0,1}};
	static int l[][3]={{1,2,0},{2,0,1}};
	int b=(abs(y)+abs(x))%2;
	vs[i+1].emplace_back(y+dy[b][j],x+dx[b][j],n[j],(find(begin(neighbors[n[j]]),end(neighbors[n[j]]),f)-begin(neighbors[n[j]])-l[b][j]+3)%3);
	{
	  int ny=y+dy[b][j],nx=x+dx[b][j],nf=n[j],nd=(find(begin(neighbors[n[j]]),end(neighbors[n[j]]),f)-begin(neighbors[n[j]])-l[b][j]+3)%3;
	  int n[3];
	  for(int j=0;j<3;j++){
	    n[j]=neighbors[nf][(nd+j)%3];
	  }
	  bool flag=false;
	  for(int j=0;j<3;j++){
	    static int dy[][3]={{1,0,0},{0,-1,0}};
	    static int dx[][3]={{0,-1,1},{-1,0,1}};
	    static int l[][3]={{1,2,0},{2,0,1}};
	    int b=(abs(ny)+abs(nx))%2;
	    flag|=y==ny+dy[b][j]&&x==nx+dx[b][j]&&f==n[j]&&d==(find(begin(neighbors[n[j]]),end(neighbors[n[j]]),nf)-begin(neighbors[n[j]])-l[b][j]+3)%3;
	  }
	  if(!flag){
	    cout<<"a"<<' '<<x<<' '<<y<<' '<<f<<' '<<d<<endl;
	  }
	}
      }
    }
  }
  int x=13,y=-13,f=2,d=0;
  for(int i=28;i>0;i--){
      int n[3];
      for(int j=0;j<3;j++){
	n[j]=neighbors[f][(d+j)%3];
      }
    for(int j=0;j<3;j++){
      static int dy[][3]={{1,0,0},{0,-1,0}};
      static int dx[][3]={{0,-1,1},{-1,0,1}};
      static int l[][3]={{1,2,0},{2,0,1}};
      int b=(abs(y)+abs(x))%2;
      int ny=y+dy[b][j],nx=x+dx[b][j],nf=n[j],nd=(find(begin(neighbors[n[j]]),end(neighbors[n[j]]),f)-begin(neighbors[n[j]])-l[b][j]+3)%3;
      if(mem[ny+100][nx+100][nf][nd]==i-1){
	y=ny;
	x=nx;
	f=nf;
	d=nd;
	cout<<i<<' '<<y<<' '<<x<<' '<<f<<' '<<d<<endl;
	break;
      }
    }
  }
    
    
  
  for(int x,y,n;cin>>x>>y>>n,x|y|n;){
    // for(int i=0;i<3;i++){
    //   cout<<mem[y+100][x+100][n][i]<<' ';
    // }
    // cout<<endl;
    cout<<*min_element(begin(mem[y+100][x+100][n]),end(mem[y+100][x+100][n]))<<endl;
  }
}
accuracy: 1
time_ms: 110
memory_kb: 57088
final_rank: 12

code_snippet: #include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std;
int dx[2][3]={{-1,1,0},{1,-1,0}};
int dy[2][3]={{0,0,1},{0,0,-1}};
int dd[2][3]={{1,2,0},{2,1,0}};
int dv[20][3]={
{4,1,5},
{0,2,6},
{1,3,7},
{2,4,8},
{3,0,9},
{11,10,0},
{12,11,1},
{13,12,2},
{14,13,3},
{10,14,4},
{9,5,15},
{5,6,16},
{6,7,17},
{7,8,18},
{8,9,19},
{16,19,10},
{17,15,11},
{18,16,12},
{19,17,13},
{15,18,14},
};
int bfs[230][230][21][3];
int ABS(int a){return max(a,-a);}
int main(){
	int a,b,c;
		int T=110;
		for(int i=0;i<230;i++)for(int j=0;j<230;j++)
			for(int k=0;k<21;k++)for(int l=0;l<3;l++)
				bfs[i][j][k][l]=-1;
		queue<pair<pair<int,int>,pair<int,int> >  >Q;
		Q.push(make_pair(make_pair(0,0),make_pair(0,0)));
		bfs[T][T][0][0]=0;
		while(Q.size()){
			int x=Q.front().first.first;
			int y=Q.front().first.second;
			int st=Q.front().second.first;
			int rot=Q.front().second.second;
			Q.pop();
		//	if(bfs[x+T][y+T][st][rot]<=5)printf("%d %d %d %d: %d\n",x,y,st,rot,bfs[x+T][y+T][st][rot]);
				
			int t=ABS(x+y)%2;
			for(int i=0;i<3;i++){
				int nx=x+dx[t][i];
				int ny=y+dy[t][i];
				int ns=dv[st][(rot+i)%3];
				int nd;
				for(int j=0;j<3;j++)if(dv[ns][j]==st){nd=(j+3-i)%3;break;}
				if(nx<-T||nx>T||ny<-T||ny>T)continue;
				if(!~bfs[nx+T][ny+T][ns][nd]){
					bfs[nx+T][ny+T][ns][nd]=bfs[x+T][y+T][st][rot]+1;
					Q.push(make_pair(make_pair(nx,ny),make_pair(ns,nd)));
				}
			}
		}
		
	while(scanf("%d%d%d",&a,&b,&c),a||b||c){
		int ret=99999999;
		for(int i=0;i<3;i++)if(~bfs[a+T][b+T][c][i])ret=min(ret,bfs[a+T][b+T][c][i]);
		printf("%d\n",ret);
	}
}
accuracy: 1
time_ms: 150
memory_kb: 14276
final_rank: 3

code_snippet: #include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#include <functional>
#include <map>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
typedef pair<int,int> P;
struct state{
	P p;
	int b,m;
	bool operator < (const state& l) const{
		if(l.p!=p) return l.p<p;
		if(l.b!=b) return l.b<b;
		return l.m<m;
	}
};
map<state,int> d;
P trans[20][20][3];
void a(int a,int b,int c,int d,int e,int f){
	trans[a][b][0]=P(b,a);
	trans[a][b][1]=P(c,d);
	trans[a][b][2]=P(e,f);
}
int dx[3]={0,1,-1},dy[3]={1,0,0};
queue<state> que;
int main(){
	a(0,5,1,2,4,3);
	a(0,4,5,11,1,6);
	a(0,1,4,9,5,10);
	a(1,6,2,3,0,4);
	a(1,0,6,12,2,7);
	a(1,2,0,5,6,11);
	a(2,7,3,4,1,0);
	a(2,3,1,6,7,12);
	a(2,1,7,13,3,8);
	a(3,8,4,0,2,1);
	a(3,4,2,7,8,13);
	a(3,2,8,14,4,9);
	a(4,9,0,1,3,2);
	a(4,0,3,8,9,14);
	a(4,3,9,10,0,5);
	a(5,0,10,15,11,16);
	a(5,10,11,6,0,1);
	a(5,11,0,4,10,9);
	a(6,1,11,16,12,17);
	a(6,11,12,7,1,2);
	a(6,12,1,0,11,5);
	a(7,2,12,17,13,18);
	a(7,12,13,8,2,3);
	a(7,13,2,1,12,6);
	a(8,3,13,18,14,19);
	a(8,13,14,9,3,4);
	a(8,14,3,2,13,7);
	a(9,4,14,19,10,15);
	a(9,14,10,5,4,0);
	a(9,10,4,3,14,8);
	a(10,5,9,14,15,19);
	a(10,9,15,16,5,11);
	a(10,15,5,0,9,4);
	a(11,5,16,17,6,12);
	a(11,16,6,1,5,0);
	a(11,6,5,10,16,15);
	a(12,7,6,11,17,16);
	a(12,6,17,18,7,13);
	a(12,17,7,2,6,1);
	a(13,18,8,3,7,2);
	a(13,8,7,12,18,17);
	a(13,7,18,19,8,14);
	a(14,19,9,4,8,3);
	a(14,9,8,13,19,18);
	a(14,8,19,15,9,10);
	a(15,10,19,18,16,17);
	a(15,19,16,11,10,5);
	a(15,16,10,9,19,14);
	a(16,11,15,19,17,18);
	a(16,15,17,12,11,6);
	a(16,17,11,5,15,10);
	a(17,12,16,15,18,19);
	a(17,16,18,13,12,7);
	a(17,18,12,6,16,11);
	a(18,13,17,16,19,15);
	a(18,17,19,14,13,8);
	a(18,19,13,7,17,12);
	a(19,14,18,17,15,16);
	a(19,18,15,10,14,9);
	a(19,15,14,8,18,13);
	state start={P(0,0),0,5};
	que.push(start);
	d[start]=0;
	while(true){
		state s=que.front();
		if(d[s]>100) break;
		que.pop();
		P p=s.p;
		int k=((p.fs+p.sc+10000)%2==0?1:-1);
		//show(k);
		// show(p.fs);
		// show(p.sc);
		// show(s.b);
		// show(s.m);
		// cout<<endl;
		rep(i,3){
			P np=P(p.fs+dx[i]*k,p.sc+dy[i]*k);
			state nstate={np,trans[s.b][s.m][i].fs,trans[s.b][s.m][i].sc};
			if(d.find(nstate)!=d.end()) continue;
			d[nstate]=d[s]+1;
			// show(np.fs);
			// show(np.sc);
			// show(nstate.b);
			que.push(nstate);
		}
	}
	while(true){
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		if(x==0&&y==0&&z==0) break;
		int ret=1e5;
		rep(i,20){
			if(d.find({P(x,y),z,i})==d.end()) continue;
			ret=min(ret,d[state{P(x,y),z,i}]);
		}
		printf("%d\n",ret);
	}
}
accuracy: 1
time_ms: 760
memory_kb: 59652
final_rank: 14

code_snippet: #include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

const int f[20][3]={{1,4,5},{2,0,6},{3,1,7},{4,2,8},{0,3,9},{0,10,11},{1,11,12},{2,12,13},{3,13,14},{4,14,10},{5,9,15},{6,5,16},{7,6,17},{8,7,18},{9,8,19},{10,19,16},{11,15,17},{12,16,18},{13,17,19},{14,18,15}};

int dp[202][202][20][3];

void A_up(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][p];
    if(f[n][0]==tmp)p=0;
    else if(f[n][1]==tmp)p=1;
    else p=2;
}

void A_left(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+2)%3];
    if(f[n][0]==tmp)p=1;
    else if(f[n][1]==tmp)p=2;
    else p=0;
}

void A_right(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+1)%3];
    if(f[n][0]==tmp)p=2;
    else if(f[n][1]==tmp)p=0;
    else p=1;
}

void B_down(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][p];
    if(f[n][0]==tmp)p=0;
    else if(f[n][1]==tmp)p=1;
    else p=2;
}

void B_left(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+1)%3];
    if(f[n][0]==tmp)p=2;
    else if(f[n][1]==tmp)p=0;
    else p=1;
}

void B_right(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+2)%3];
    if(f[n][0]==tmp)p=1;
    else if(f[n][1]==tmp)p=2;
    else p=0;
}

void init(){
    static int x,y,n,p,n0,p0;
    static queue<int> Q;
    memset(dp,-1,sizeof(dp));
    dp[100][100][0][2]=0;
    while(!Q.empty())Q.pop();
    Q.push(100);Q.push(100);Q.push(0);Q.push(2);
    while(!Q.empty()){
        x=Q.front();Q.pop();
        y=Q.front();Q.pop();
        n=Q.front();Q.pop();
        p=Q.front();Q.pop();
        if(dp[x][y][n][p]==100)return;
        if((x+y)%2){
            n0=n,p0=p;
            B_down(n0,p0);
            if(dp[x][y-1][n0][p0]==-1){
                dp[x][y-1][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x);Q.push(y-1);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            B_left(n0,p0);
            if(dp[x-1][y][n0][p0]==-1){
                dp[x-1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x-1);Q.push(y);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            B_right(n0,p0);
            if(dp[x+1][y][n0][p0]==-1){
                dp[x+1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x+1);Q.push(y);Q.push(n0);Q.push(p0);
            }
        }
        else{
            n0=n,p0=p;
            A_up(n0,p0);
            if(dp[x][y+1][n0][p0]==-1){
                dp[x][y+1][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x);Q.push(y+1);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            A_left(n0,p0);
            if(dp[x-1][y][n0][p0]==-1){
                dp[x-1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x-1);Q.push(y);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            A_right(n0,p0);
            if(dp[x+1][y][n0][p0]==-1){
                dp[x+1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x+1);Q.push(y);Q.push(n0);Q.push(p0);
            }
        }
    }
}

bool solve(){
    static int x,y,n;
    if(scanf("%d%d%d",&x,&y,&n)!=3||((x|y|n)==0))return false;
    x+=100;
    y+=100;
    static int ans,i;
    ans=100;
    for(i=0;i<3;i++)
        if(dp[x][y][n][i]!=-1&&(ans>dp[x][y][n][i]))
            ans=dp[x][y][n][i];
    printf("%d\n",ans);
    return true;
}

int main(){
    init();
    while(solve());
    return 0;
}
/*
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

const int f[20][3]={{1,4,5},{2,0,6},{3,1,7},{4,2,8},{0,3,9},{0,10,11},{1,11,12},{2,12,13},{3,13,14},{4,14,10},{5,9,15},{6,5,16},{7,6,17},{8,7,18},{9,8,19},{10,19,16},{11,15,17},{12,16,18},{13,17,19},{14,18,15}};

int dp[202][202][20][3];

void A_up(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][p];
    if(f[n][0]==tmp)p=0;
    else if(f[n][1]==tmp)p=1;
    else p=2;
}

void A_left(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+2)%3];
    if(f[n][0]==tmp)p=1;
    else if(f[n][1]==tmp)p=2;
    else p=0;
}

void A_right(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+1)%3];
    if(f[n][0]==tmp)p=2;
    else if(f[n][1]==tmp)p=0;
    else p=1;
}

void B_down(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][p];
    if(f[n][0]==tmp)p=0;
    else if(f[n][1]==tmp)p=1;
    else p=2;
}

void B_left(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+1)%3];
    if(f[n][0]==tmp)p=2;
    else if(f[n][1]==tmp)p=0;
    else p=1;
}

void B_right(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+2)%3];
    if(f[n][0]==tmp)p=1;
    else if(f[n][1]==tmp)p=2;
    else p=0;
}

void init(){
    static int x,y,n,p,n0,p0;
    static queue<int> Q;
    memset(dp,-1,sizeof(dp));
    dp[100][100][0][2]=0;
    while(!Q.empty())Q.pop();
    Q.push(100);Q.push(100);Q.push(0);Q.push(2);
    while(!Q.empty()){
        x=Q.front();Q.pop();
        y=Q.front();Q.pop();
        n=Q.front();Q.pop();
        p=Q.front();Q.pop();
        if(dp[x][y][n][p]==100)return;
        if((x+y)%2){
            n0=n,p0=p;
            B_down(n0,p0);
            if(dp[x][y-1][n0][p0]==-1){
                dp[x][y-1][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x);Q.push(y-1);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            B_left(n0,p0);
            if(dp[x-1][y][n0][p0]==-1){
                dp[x-1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x-1);Q.push(y);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            B_right(n0,p0);
            if(dp[x+1][y][n0][p0]==-1){
                dp[x+1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x+1);Q.push(y);Q.push(n0);Q.push(p0);
            }
        }
        else{
            n0=n,p0=p;
            A_up(n0,p0);
            if(dp[x][y+1][n0][p0]==-1){
                dp[x][y+1][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x);Q.push(y+1);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            A_left(n0,p0);
            if(dp[x-1][y][n0][p0]==-1){
                dp[x-1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x-1);Q.push(y);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            A_right(n0,p0);
            if(dp[x+1][y][n0][p0]==-1){
                dp[x+1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x+1);Q.push(y);Q.push(n0);Q.push(p0);
            }
        }
    }
}

bool solve(){
    static int x,y,n;
    if(scanf("%d%d%d",&x,&y,&n)!=3||((x|y|n)==0))return false;
    x+=100;
    y+=100;
    static int ans,i;
    ans=100;
    for(i=0;i<3;i++)
        if(dp[x][y][n][i]!=-1&&(ans>dp[x][y][n][i]))
            ans=dp[x][y][n][i];
    printf("%d\n",ans);
    return true;
}

int main(){
    init();
    while(solve());
    return 0;
}
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

const int f[20][3]={{1,4,5},{2,0,6},{3,1,7},{4,2,8},{0,3,9},{0,10,11},{1,11,12},{2,12,13},{3,13,14},{4,14,10},{5,9,15},{6,5,16},{7,6,17},{8,7,18},{9,8,19},{10,19,16},{11,15,17},{12,16,18},{13,17,19},{14,18,15}};

int dp[202][202][20][3];

void A_up(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][p];
    if(f[n][0]==tmp)p=0;
    else if(f[n][1]==tmp)p=1;
    else p=2;
}

void A_left(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+2)%3];
    if(f[n][0]==tmp)p=1;
    else if(f[n][1]==tmp)p=2;
    else p=0;
}

void A_right(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+1)%3];
    if(f[n][0]==tmp)p=2;
    else if(f[n][1]==tmp)p=0;
    else p=1;
}

void B_down(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][p];
    if(f[n][0]==tmp)p=0;
    else if(f[n][1]==tmp)p=1;
    else p=2;
}

void B_left(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+1)%3];
    if(f[n][0]==tmp)p=2;
    else if(f[n][1]==tmp)p=0;
    else p=1;
}

void B_right(int &n,int &p){
    static int tmp;
    tmp=n;
    n=f[n][(p+2)%3];
    if(f[n][0]==tmp)p=1;
    else if(f[n][1]==tmp)p=2;
    else p=0;
}

void init(){
    static int x,y,n,p,n0,p0;
    static queue<int> Q;
    memset(dp,-1,sizeof(dp));
    dp[100][100][0][2]=0;
    while(!Q.empty())Q.pop();
    Q.push(100);Q.push(100);Q.push(0);Q.push(2);
    while(!Q.empty()){
        x=Q.front();Q.pop();
        y=Q.front();Q.pop();
        n=Q.front();Q.pop();
        p=Q.front();Q.pop();
        if(dp[x][y][n][p]==100)return;
        if((x+y)%2){
            n0=n,p0=p;
            B_down(n0,p0);
            if(dp[x][y-1][n0][p0]==-1){
                dp[x][y-1][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x);Q.push(y-1);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            B_left(n0,p0);
            if(dp[x-1][y][n0][p0]==-1){
                dp[x-1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x-1);Q.push(y);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            B_right(n0,p0);
            if(dp[x+1][y][n0][p0]==-1){
                dp[x+1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x+1);Q.push(y);Q.push(n0);Q.push(p0);
            }
        }
        else{
            n0=n,p0=p;
            A_up(n0,p0);
            if(dp[x][y+1][n0][p0]==-1){
                dp[x][y+1][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x);Q.push(y+1);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            A_left(n0,p0);
            if(dp[x-1][y][n0][p0]==-1){
                dp[x-1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x-1);Q.push(y);Q.push(n0);Q.push(p0);
            }
            n0=n,p0=p;
            A_right(n0,p0);
            if(dp[x+1][y][n0][p0]==-1){
                dp[x+1][y][n0][p0]=dp[x][y][n][p]+1;
                Q.push(x+1);Q.push(y);Q.push(n0);Q.push(p0);
            }
        }
    }
}

bool solve(){
    static int x,y,n;
    if(scanf("%d%d%d",&x,&y,&n)!=3||((x|y|n)==0))return false;
    x+=100;
    y+=100;
    static int ans,i;
    ans=100;
    for(i=0;i<3;i++)
        if(dp[x][y][n][i]!=-1&&(ans>dp[x][y][n][i]))
            ans=dp[x][y][n][i];
    printf("%d\n",ans);
    return true;
}

int main(){
    init();
    while(solve());
    return 0;
}

*/
accuracy: 1
time_ms: 40
memory_kb: 11068
final_rank: 1

code_snippet: #include<iostream>
#include<queue>
#include<algorithm>
#include<cassert>
using namespace std;
#define rep(i, n) for ( int i = 0; i < n; i++)
static int C[20][3] = {{5,4,1},{6,0,2},{7,1,3},{8,2,4},{9,3,0},{0,11,10},{1,12,11},{2,13,12},{3,14,13},{4,10,14},{15,9,5},{16,5,6},{17,6,7},{18,7,8},{19,8,9},{10,16,19},{11,17,15},{12,18,16},{13,19,17},{14,15,18}};
static const int dx[3] = {0, -1, 1};
static const int dy[3] = {1, 0, 0};

static const int MAX = 305;
static const int SHIFT = 150;

class State{
public:
  int x, y, b, r, c;
  State(){}
  State(int x, int y, int b, int r):x(x), y(y), b(b), r(r){ c = 0;}
};

State getNext(State u, int r){
  int nx = dx[r];
  int ny = dy[r];
  
  if ( (u.x + u.y)%2  ){ nx *= (-1); ny *= (-1); }
  nx += u.x;
  ny += u.y;

  int bt[3], nt[3];

  rep(i, 3) bt[i] = C[u.b][i];
  rotate(bt, bt + u.r, bt + 3);
  int nb = bt[r]; // next bottom

  rep(i, 3) nt[i] = C[nb][i];
  int nr = -1; // next rotate
  rep(i, 3){
    if ( u.b == nt[r] ){ nr = i; break; }
    rotate(nt, nt+1, nt+3);
  }

  return State(nx, ny, nb, nr);
}

bool vis[MAX][MAX][20][3];

int bfs(int gx, int gy, int gb){
  queue<State> Q;
  rep(i, MAX)rep(j, MAX) rep(k,20) rep(l,3) vis[i][j][k][l] = false;

  Q.push(State(SHIFT, SHIFT, 0, 0));
  vis[SHIFT][SHIFT][0][0] = true;
  State u, v;

  while(1){
    u = Q.front(); Q.pop();
    if ( u.x == gx && u.y == gy && u.b == gb ) return u.c;
    rep(r, 3){
      v = getNext(u, r);
      if ( !vis[v.x][v.y][v.b][v.r] ){
	vis[v.x][v.y][v.b][v.r] = true;
	v.c = u.c + 1;
	Q.push(v);
      }
    }
  }
}

main(){
  int gx, gy, gb;
  while(1){
    cin >> gx >> gy >> gb;
    if ( gx == 0 && gy == 0 && gb == 0 ) break;
    gx += SHIFT;
    gy += SHIFT;
    cout << bfs(gx, gy, gb) << endl;
  }
}
accuracy: 1
time_ms: 2060
memory_kb: 6996
final_rank: 10

code_snippet: #include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
#define rep(i, n) for ( int i = 0; i < n; i++)
static int C[20][3] = {{5,4,1},{6,0,2},{7,1,3},{8,2,4},{9,3,0},{0,11,10},{1,12,11},{2,13,12},{3,14,13},{4,10,14},{15,9,5},{16,5,6},{17,6,7},{18,7,8},{19,8,9},{10,16,19},{11,17,15},{12,18,16},{13,19,17},{14,15,18}};
static const int dx[3] = {0, -1, 1};
static const int dy[3] = {1, 0, 0};
static const int MAX = 205;
static const int SHIFT = 102;

class State{
public:
  int x, y, b, r, c;
  State(){}
  State(int x, int y, int b, int r):x(x), y(y), b(b), r(r){ c = 0;}
};

State getNext(State u, int r){
  int bt[3], nt[3], nb, nr = -1;
  int nx = dx[r];
  int ny = dy[r];
  if ( (u.x + u.y)%2  ){ nx *= (-1); ny *= (-1); }
  nx += u.x;
  ny += u.y;

  rep(i, 3) bt[i] = C[u.b][i];
  rotate(bt, bt + u.r, bt + 3);
  nb = bt[r]; // next bottom
  rep(i, 3) nt[i] = C[nb][i];

  rep(i, 3){
    if ( u.b == nt[r] ){ nr = i; break; }
    rotate(nt, nt+1, nt+3);
  }

  return State(nx, ny, nb, nr);
}

int bfs(int gx, int gy, int gb){
  bool vis[MAX][MAX][20][3];
  queue<State> Q;
  rep(i, MAX)rep(j, MAX) rep(k,20) rep(l,3) vis[i][j][k][l] = false;
  Q.push(State(SHIFT, SHIFT, 0, 0));
  vis[SHIFT][SHIFT][0][0] = true;
  State u, v;
  while(1){
    u = Q.front(); Q.pop();
    if ( u.x == gx && u.y == gy && u.b == gb ) return u.c;
    rep(r, 3){
      v = getNext(u, r);
      if ( !vis[v.x][v.y][v.b][v.r] ){
	vis[v.x][v.y][v.b][v.r] = true;
	v.c = u.c + 1;
	Q.push(v);
      }
    }
  }
}

main(){
  int gx, gy, gb;
  while(1){
    cin >> gx >> gy >> gb;
    if ( gx == 0 && gy == 0 && gb == 0 ) break;
    gx += SHIFT;
    gy += SHIFT;
    cout << bfs(gx, gy, gb) << endl;
  }
}
accuracy: 1
time_ms: 2020
memory_kb: 4008
final_rank: 8

code_snippet: #include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
#define rep(i, n) for ( int i = 0; i < n; i++)
static int C[20][3] = {{5,4,1},{6,0,2},{7,1,3},{8,2,4},{9,3,0},{0,11,10},{1,12,11},{2,13,12},{3,14,13},{4,10,14},{15,9,5},{16,5,6},{17,6,7},{18,7,8},{19,8,9},{10,16,19},{11,17,15},{12,18,16},{13,19,17},{14,15,18}};
static const int dx[3] = {0, -1, 1};
static const int dy[3] = {1, 0, 0};
static const int MAX = 205;
static const int SHIFT = 102;

class State{
public:
  int x, y, b, r, c;
  State(){}
  State(int x, int y, int b, int r):x(x), y(y), b(b), r(r){ c = 0;}
};

State getNext(State u, int r){
  int nx = dx[r];
  int ny = dy[r];
  if ( (u.x + u.y)%2  ){ nx *= (-1); ny *= (-1); }
  nx += u.x;
  ny += u.y;

  int bt[3], nt[3], nb, nr;

  rep(i, 3) bt[i] = C[u.b][i];
  rotate(bt, bt + u.r, bt + 3);
  nb = bt[r]; // next bottom
  rep(i, 3) nt[i] = C[nb][i];
  nr = -1; // next rotate
  rep(i, 3){
    if ( u.b == nt[r] ){ nr = i; break; }
    rotate(nt, nt+1, nt+3);
  }

  return State(nx, ny, nb, nr);
}

int bfs(int gx, int gy, int gb){
  bool vis[MAX][MAX][20][3];
  queue<State> Q;
  rep(i, MAX)rep(j, MAX) rep(k,20) rep(l,3) vis[i][j][k][l] = false;

  Q.push(State(SHIFT, SHIFT, 0, 0));
  vis[SHIFT][SHIFT][0][0] = true;
  State u, v;

  while(1){
    u = Q.front(); Q.pop();
    if( u.c > 150 ) return -1;
    if ( u.x == gx && u.y == gy && u.b == gb ) return u.c;
    rep(r, 3){
      v = getNext(u, r);
      if ( !vis[v.x][v.y][v.b][v.r] ){
	vis[v.x][v.y][v.b][v.r] = true;
	v.c = u.c + 1;
	Q.push(v);
      }
    }
  }
}

main(){
  int gx, gy, gb;
  while(1){
    cin >> gx >> gy >> gb;
    if ( gx == 0 && gy == 0 && gb == 0 ) break;
    gx += SHIFT;
    gy += SHIFT;
    cout << bfs(gx, gy, gb) << endl;
  }
}
accuracy: 1
time_ms: 2020
memory_kb: 4008
final_rank: 8

code_snippet: #include<cstdio>
#include<algorithm>

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

using namespace std;

const int INF=1<<29;

const int dx[2][3]={{0,-1,1},{0,1,-1}},dy[2][3]={{1,0,0},{-1,0,0}};

struct state{ int x,y,face,dir; };

int main(){
	// table[i][j] := ( 底面が i で方向 j に転がったとき, 次に底面になる面の番号 )
	// 問題文の図で上(or下)の方向を方向 0 として, 反時計回りに方向 1, 2 を定めている.
	// 方向は正二十面体の上に定められていて, 座標とは関係ない!!!
	// 正二十面体を実際にグリッド上に置くと, 展開図と面の位置関係が反転するので注意.
	int table[20][3]={
		{ 5, 4, 1},
		{ 6, 0, 2},
		{ 7, 1, 3},
		{ 8, 2, 4},
		{ 9, 3, 0},
		{ 0,11,10},
		{ 1,12,11},
		{ 2,13,12},
		{ 3,14,13},
		{ 4,10,14},
		{15, 9, 5},
		{16, 5, 6},
		{17, 6, 7},
		{18, 7, 8},
		{19, 8, 9},
		{10,16,19},
		{11,17,15},
		{12,18,16},
		{13,19,17},
		{14,15,18}
	};

	// dist[y][x][i][j] := ( 座標が (x,y) で, 底面が i で, 北(or南)が方向 j である状態に達するための最短距離 )
	static int dist[201][201][20][3];
	rep(y,201) rep(x,201) rep(i,20) rep(j,3) dist[y][x][i][j]=INF;
	dist[100][100][0][0]=0;

	static state Q[201*201*20*3];
	int head=0,tail=0;
	Q[tail++]=(state){100,100,0,0};
	while(head<tail){
		state S=Q[head++];
		if(dist[S.y][S.x][S.face][S.dir]==100) continue;

		rep(k,3){
			int xx=S.x+dx[(S.x+S.y)%2][k];
			int yy=S.y+dy[(S.x+S.y)%2][k];
			int ff=table[S.face][(S.dir+k)%3];
			int dd;
			if(S.face<5 || 15<=S.face){
				if     (S.dir==0 && k==0) dd=0;
				else if(S.dir==0 && k==1) dd=1;
				else if(S.dir==0 && k==2) dd=2;
				else if(S.dir==1 && k==0) dd=2;
				else if(S.dir==1 && k==1) dd=0;
				else if(S.dir==1 && k==2) dd=1;
				else if(S.dir==2 && k==0) dd=1;
				else if(S.dir==2 && k==1) dd=2;
				else if(S.dir==2 && k==2) dd=0;
			}
			else{ // 5<=S.face && S.face<15
				if     (S.dir==0 && k==0) dd=0;
				else if(S.dir==0 && k==1) dd=0;
				else if(S.dir==0 && k==2) dd=0;
				else if(S.dir==1 && k==0) dd=1;
				else if(S.dir==1 && k==1) dd=1;
				else if(S.dir==1 && k==2) dd=1;
				else if(S.dir==2 && k==0) dd=2;
				else if(S.dir==2 && k==1) dd=2;
				else if(S.dir==2 && k==2) dd=2;
			}

			if(dist[yy][xx][ff][dd]>dist[S.y][S.x][S.face][S.dir]+1){
				dist[yy][xx][ff][dd]=dist[S.y][S.x][S.face][S.dir]+1;
				Q[tail++]=(state){xx,yy,ff,dd};
			}
		}
	}

	for(int gx,gy,f;scanf("%d%d%d",&gx,&gy,&f),gx||gy||f;){
		gx+=100;
		gy+=100;
		printf("%d\n",min(min(dist[gy][gx][f][0],dist[gy][gx][f][1]),dist[gy][gx][f][2]));
	}

	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 25596
final_rank: 6