// 2011アジア地区予選 E : Driving an Icosahedral Rover #include #include #include #include using namespace std; class Node{ public: int x, y, face, dir; Node(int x, int y, int face, int dir) : x(x), y(y), face(face), dir(dir) {} }; int dx[2][3] = { {1, -1, 0}, {1, 0, -1} }; int dy[2][3] = { {0, 0, 1}, {0, -1, 0} }; int next[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 main(){ int n; static int res[3][20][201][201]; // [dir][face][x][y] memset(res, -1, sizeof(res)); queue qu; qu.push(Node(100, 100, 0, 0)); res[0][0][100][100] = 0; while(!qu.empty()){ Node nd = qu.front(); qu.pop(); int px = nd.x, py = nd.y, face = nd.face, dir = nd.dir; if(res[dir][face][px][py]==100) continue; for(int i=0;i<3;i++){ int x = px+dx[(px+py)%2][i], y = py+dy[(px+py)%2][i]; int nface = next[face][(i+dir)%3]; int ndir = 0; for(int j=0;j<3;j++){ int nx = x+dx[(x+y)%2][j], ny = y+dy[(x+y)%2][j]; if(nx==px && ny == py){ for(int k=0;k<3;k++) if(next[nface][k] == face) ndir = (k-j+3)%3; } } if(res[ndir][nface][x][y]==-1){ res[ndir][nface][x][y] = res[dir][face][px][py]+1; qu.push(Node(x, y, nface, ndir)); } } } int x, y, f; while(cin >> x >> y >> f, x|y|f){ int step = 100; for(int i=0;i<3;i++){ if(res[i][f][x+100][y+100]!=-1) step = min(step, res[i][f][x+100][y+100]); } cout << step << endl; } }