#include#include#include#include#include#include#define maxn 9 #define inf 1000000005 #define mem(a, b) memset(a, b, sizeof(a)) using namespace std; int sx, sy, ex, ey, nn, num[6], dis[maxn][maxn][6][6], pathx[maxn][maxn][6][6], pathy[maxn][maxn][6][6], pathd[maxn][maxn][6][6], pathz[maxn][maxn][6][6], js[maxn][maxn], dir[6][6][4] = {{{0, 0, 0, 0}, {0, 0, 0, 0}, {4, 3, 2, 5}, {5, 4, 3, 2}, {2, 5, 4, 3}, {3, 2, 5, 4}}, {{0, 0, 0, 0}, {0, 0, 0, 0}, {4, 5, 2, 3}, {5, 2, 3, 4}, {2, 3, 4, 5}, {3, 4, 5, 2}}, {{1, 5, 0, 3}, {0, 3, 1, 5}, {0, 0, 0, 0}, {5, 0, 3, 1}, {0, 0, 0, 0}, {3, 1, 5, 0}}, {{1, 2, 0, 4}, {0, 4, 1, 2}, {4, 1, 2, 0}, {0, 0, 0, 0}, {2, 0, 4, 1}, {0, 0, 0, 0}}, {{1, 3, 0, 5}, {0, 5, 1, 3}, {0, 0, 0, 0}, {5, 1, 3, 0}, {0, 0, 0, 0}, {3, 0, 5, 1}}, {{1, 4, 0, 2}, {0, 2, 1, 4}, {4, 0, 2, 1}, {0, 0, 0, 0}, {2, 1, 4, 0}, {0, 0, 0, 0}}}, ox[4] = {-1, 0, 1, 0}, oy[4] = {0, -1, 0, 1}, mins, fan[6] = {1, 0, 4, 5, 2, 3}; struct node { int x, y; }nd[260]; void res(int a, int b, int c, int d) { int x = pathx[a][b][c][d]; int y = pathy[a][b][c][d]; int di = pathd[a][b][c][d]; int z = pathz[a][b][c][d]; if(x == sx&&y == sy&&di == 3&&z == 0) { nd[nn].x = x; nd[nn++].y = y; nd[nn].x = a; nd[nn++].y = b; return; } res(x, y, di, z); nd[nn].x = a; nd[nn++].y = b; } struct cube { int x, y, di, z, v, pre; }; void bfs(int x, int y, int p, int di, int z, int v) { queueq; struct cube tt; dis[x][y][di][z] = v; tt.x = x, tt.y = y, tt.di = di; tt.z = z, tt.v = v, tt.pre = p; q.push(tt); while(!q.empty()) { struct cube tmp = q.front(); q.pop(); if(tmp.v >= mins) continue; if(tmp.x == ex&&tmp.y == ey&&mins > tmp.v) { mins = tmp.v; nn = 0; res(tmp.x, tmp.y, tmp.di, tmp.z); continue; } int nowdi = tmp.di, nowz = tmp.z; int nowx = tmp.x, nowy = tmp.y; int nowv = tmp.v, pre = tmp.pre; for(int i = 0;i= 0&&nowx + ox[i]= 0&&nowy + oy[i]nowv + num[dir[nowdi][nowz][i]]) { dis[nowx + ox[i]][nowy + oy[i]][dir[nowdi][nowz][i]][tz] = nowv + num[dir[nowdi][nowz][i]]; pathx[nowx + ox[i]][nowy + oy[i]][dir[nowdi][nowz][i]][tz] = nowx; pathy[nowx + ox[i]][nowy + oy[i]][dir[nowdi][nowz][i]][tz] = nowy; pathd[nowx + ox[i]][nowy + oy[i]][dir[nowdi][nowz][i]][tz] = nowdi; pathz[nowx + ox[i]][nowy + oy[i]][dir[nowdi][nowz][i]][tz] = nowz; struct cube jj; jj.di = dir[nowdi][nowz][i], jj.pre = nowdi, jj.v = nowv + num[dir[nowdi][nowz][i]]; jj.x = nowx + ox[i], jj.y = nowy + oy[i], jj.z = tz; q.push(jj); } } } return; } int main() { char a, b; int c, d; while(~scanf("%c%d%*c%c%d%d%d%d%d%d%d%*c", &a, &c, &b, &d, &num[0], &num[1], &num[5], &num[4], &num[3], &num[2])) { sx = 8 - c; sy = a - 'a'; ex = 8 - d; ey = b - 'a'; mins = inf; for(int i = 0;i