// ACM-ICPC国内予選2008 D. ちょろちょろロボット #include #include using namespace std; class Node{ public: int x, y, d, cost; Node(int x, int y, int d, int cost) : x(x), y(y), d(d), cost(cost) {} bool operator < (const Node &n) const { return cost > n.cost; } }; char dx[] = {0, 1, 0, -1}; char dy[] = {1, 0, -1, 0}; int main(){ int w, h; int op[4]; int b[30][30]; int c[30][30][4]; while(cin >> w >> h, w){ memset(c, -1, sizeof(c)); for(int i=0;i> b[i][j]; } for(int i=0;i<4;i++) cin >> op[i]; priority_queue qu; qu.push(Node(0,0,0,0)); int ans = 9999999; while(!qu.empty()){ Node nd = qu.top(); qu.pop(); int x = nd.x, y = nd.y, d = nd.d, cost = nd.cost; if(c[x][y][d]!=-1) continue; if(x==h-1&&y==w-1) { ans = cost; break; } c[x][y][d] = cost; for(int i=0;i<4;i++){ int nx = x + dx[i], ny = y + dy[i]; if(nx<0||nx>=h||ny<0||ny>=w) continue; if(b[x][y]!=4&&(b[x][y]+d)%4==i) qu.push(Node(nx,ny,i,cost)); else qu.push(Node(nx,ny,i,cost+op[(4+i-d)%4])); } } cout << ans << endl; } }