Problem:
Problem D: Twirling Robot

Let's play a game using a robot on a rectangular board
covered with a square mesh (Figure D-1).
The robot is initially set at the start square in the northwest corner
facing the east direction.
The goal of this game is to lead the robot
to the goal square in the southeast corner.

Figure D-1: Example of a board

The robot can execute the following five types of commands.

"Straight":

Keep the current direction of the robot, and move forward to the next square.

"Right":

Turn right with 90 degrees from the current direction, and move forward to the next square.

"Back":

Turn to the reverse direction, and move forward to the next square.

"Left":

Turn left with 90 degrees from the current direction, and move forward to the next square.

"Halt":

Stop at the current square to finish the game.

Each square has one of these commands assigned as shown in Figure D-2.
The robot executes the command assigned to the square where it resides,
unless the player gives another command to be executed instead.
Each time the player gives an explicit command,
the player has to pay the cost that depends on the command type.

Figure D-2: Example of commands assigned to squares

The robot can visit the same square several times during a game.
The player loses the game when the robot goes out of the board
or it executes a "Halt" command before arriving at the goal square.

Your task is to write a program that calculates the minimum cost to lead the robot
from the start square to the goal one.

Input

The input is a sequence of datasets.
The end of the input is indicated by a line containing two zeros separated by a space.
Each dataset is formatted as follows.

w h

s(1,1) ... s(1,w)

s(2,1) ... s(2,w)

...

s(h,1) ... s(h,w)

c0 c1 c2 c3

The integers h and w are the numbers of rows and columns of the board,
respectively.
You may assume 2 ≤ h ≤ 30 and 2 ≤ w ≤ 30.
Each of the following h lines consists of w numbers delimited by a space.
The number s(i, j) represents the command assigned to the square
in the i-th row and the j-th column as follows.

0: "Straight"

1: "Right"

2: "Back"

3: "Left"

4: "Halt"

You can assume that a "Halt" command is assigned to the goal square.
Note that "Halt" commands may be assigned to other squares, too.

The last line of a dataset contains four integers
c0, c1, c2, and c3, delimited by a space,
indicating the costs that the player has to pay
when the player gives
"Straight", "Right", "Back", and "Left" commands
respectively.
The player cannot give "Halt" commands.
You can assume that all the values of
c0, c1, c2, and c3
are between 1 and 9, inclusive.

Output

For each dataset, print a line only having a decimal integer indicating the minimum cost
required to lead the robot to the goal.
No other characters should be on the output line.

Sample Input

8 3
0 0 0 0 0 0 0 1
2 3 0 1 4 0 0 1
3 3 0 0 0 0 0 4
9 9 1 9
4 4
3 3 4 0
1 2 4 4
1 1 1 0
0 2 4 4
8 7 2 1
2 8
2 2
4 1
0 4
1 3
1 0
2 1
0 3
1 4
1 9 3 1
0 0

Output for the Sample Input

1
11
6

Solution in C++ (judge verdict: Accepted)

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<n;++i)

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 31;

struct edge{
    int to;
    int cost;
};

int a[MAX_N][MAX_N];
vector<edge> G[4*MAX_N*MAX_N];
int c[5];
int w,h;
int d[4*MAX_N*MAX_N];


void dijkstra(int s)
{
	priority_queue<P,vector<P>,greater<P> > que;
	d[s] = 0;
	que.push(P(0,s));
	while(!que.empty()){
		P p = que.top();
		que.pop();
		int v = p.second;
		if(d[v] < p.first) continue;
		vector<edge>::iterator it = G[v].begin();
		rep(i,G[v].size()){
			if(d[G[v][i].to] > d[v] + G[v][i].cost){
				d[G[v][i].to] = d[v] + G[v][i].cost;
				que.push(P(d[G[v][i].to],G[v][i].to));
			}
		}
	}
}

int main()
{
    while(1){
        scanf("%d%d",&w,&h);
        if(w == 0 && h == 0){
            break;
        }
        rep(i,4*w*h){
            G[i].clear();
            d[i] = INF;
        }
    	rep(i,h){
    		rep(j,w){
                scanf("%d",&a[i][j]);
            }
    	}
        rep(i,4){
            scanf("%d",&c[i]);
        }
        rep(i,h){
            rep(j,w){
                if(a[i][j] == 0 || a[i][j] == 4){
                    if(a[i][j] == 4){
                        if(i < h-1){
                            G[4*(w*i + j)].push_back((edge){4*(w*(i+1) + j),c[0]});
                            G[4*(w*i + j)+1].push_back((edge){4*(w*(i+1) + j),c[1]});
                            G[4*(w*i + j)+3].push_back((edge){4*(w*(i+1) + j),c[2]});
                            G[4*(w*i + j)+2].push_back((edge){4*(w*(i+1) + j),c[3]});
                        }
                        if(j < w-1){
                            G[4*(w*i + j)+1].push_back((edge){4*(w*i + j+1)+1,c[0]});
                            G[4*(w*i + j)+3].push_back((edge){4*(w*i + j+1)+1,c[1]});
                            G[4*(w*i + j)+2].push_back((edge){4*(w*i + j+1)+1,c[2]});
                            G[4*(w*i + j)].push_back((edge){4*(w*i + j+1)+1,c[3]});
                        }
                        if(0 < j){
                            G[4*(w*i + j)+2].push_back((edge){4*(w*i + j-1)+2,c[0]});
                            G[4*(w*i + j)].push_back((edge){4*(w*i + j-1)+2,c[1]});
                            G[4*(w*i + j)+1].push_back((edge){4*(w*i + j-1)+2,c[2]});
                            G[4*(w*i + j)+3].push_back((edge){4*(w*i + j-1)+2,c[3]});
                        }
                        if(0 < i){
                            G[4*(w*i + j)+3].push_back((edge){4*(w*(i-1) + j)+3,c[0]});
                            G[4*(w*i + j)+2].push_back((edge){4*(w*(i-1) + j)+3,c[1]});
                            G[4*(w*i + j)].push_back((edge){4*(w*(i-1) + j)+3,c[2]});
                            G[4*(w*i + j)+1].push_back((edge){4*(w*(i-1) + j)+3,c[3]});
                        }
                    }else{
                        if(i < h-1){
                            G[4*(w*i + j)].push_back((edge){4*(w*(i+1) + j),0});
                            G[4*(w*i + j)+1].push_back((edge){4*(w*(i+1) + j),c[1]});
                            G[4*(w*i + j)+3].push_back((edge){4*(w*(i+1) + j),c[2]});
                            G[4*(w*i + j)+2].push_back((edge){4*(w*(i+1) + j),c[3]});
                        }
                        if(j < w-1){
                            G[4*(w*i + j)+1].push_back((edge){4*(w*i + j+1)+1,0});
                            G[4*(w*i + j)+3].push_back((edge){4*(w*i + j+1)+1,c[1]});
                            G[4*(w*i + j)+2].push_back((edge){4*(w*i + j+1)+1,c[2]});
                            G[4*(w*i + j)].push_back((edge){4*(w*i + j+1)+1,c[3]});
                        }
                        if(0 < j){
                            G[4*(w*i + j)+2].push_back((edge){4*(w*i + j-1)+2,0});
                            G[4*(w*i + j)].push_back((edge){4*(w*i + j-1)+2,c[1]});
                            G[4*(w*i + j)+1].push_back((edge){4*(w*i + j-1)+2,c[2]});
                            G[4*(w*i + j)+3].push_back((edge){4*(w*i + j-1)+2,c[3]});
                        }
                        if(0 < i){
                            G[4*(w*i + j)+3].push_back((edge){4*(w*(i-1) + j)+3,0});
                            G[4*(w*i + j)+2].push_back((edge){4*(w*(i-1) + j)+3,c[1]});
                            G[4*(w*i + j)].push_back((edge){4*(w*(i-1) + j)+3,c[2]});
                            G[4*(w*i + j)+1].push_back((edge){4*(w*(i-1) + j)+3,c[3]});
                        }
                    }
                }else if(a[i][j] == 1){
                    if(0 < j){
                        G[4*(w*i + j)].push_back((edge){4*(w*i + j-1)+2,0});
                        G[4*(w*i + j)+2].push_back((edge){4*(w*i + j-1)+2,c[0]});
                        G[4*(w*i + j)+1].push_back((edge){4*(w*i + j-1)+2,c[2]});
                        G[4*(w*i + j)+3].push_back((edge){4*(w*i + j-1)+2,c[3]});
                    }
                    if(i < h-1){
                        G[4*(w*i + j)+1].push_back((edge){4*(w*(i+1) + j),0});
                        G[4*(w*i + j)].push_back((edge){4*(w*(i+1) + j),c[0]});
                        G[4*(w*i + j)+3].push_back((edge){4*(w*(i+1) + j),c[2]});
                        G[4*(w*i + j)+2].push_back((edge){4*(w*(i+1) + j),c[3]});
                    }
                    if(0 < i){
                        G[4*(w*i + j)+2].push_back((edge){4*(w*(i-1) + j)+3,0});
                        G[4*(w*i + j)+3].push_back((edge){4*(w*(i-1) + j)+3,c[0]});
                        G[4*(w*i + j)].push_back((edge){4*(w*(i-1) + j)+3,c[2]});
                        G[4*(w*i + j)+1].push_back((edge){4*(w*(i-1) + j)+3,c[3]});
                    }
                    if(j < w-1){
                        G[4*(w*i + j)+3].push_back((edge){4*(w*i + j+1)+1,0});
                        G[4*(w*i + j)+1].push_back((edge){4*(w*i + j+1)+1,c[0]});
                        G[4*(w*i + j)+2].push_back((edge){4*(w*i + j+1)+1,c[2]});
                        G[4*(w*i + j)].push_back((edge){4*(w*i + j+1)+1,c[3]});
                    }
                }else if(a[i][j] == 2){
                    if(0 < i){
                        G[4*(w*i + j)].push_back((edge){4*(w*(i-1) + j)+3,0});
                        G[4*(w*i + j)+3].push_back((edge){4*(w*(i-1) + j)+3,c[0]});
                        G[4*(w*i + j)+2].push_back((edge){4*(w*(i-1) + j)+3,c[1]});
                        G[4*(w*i + j)+1].push_back((edge){4*(w*(i-1) + j)+3,c[3]});
                    }
                    if(0 < j){
                        G[4*(w*i + j)+1].push_back((edge){4*(w*i + j-1)+2,0});
                        G[4*(w*i + j)+2].push_back((edge){4*(w*i + j-1)+2,c[0]});
                        G[4*(w*i + j)].push_back((edge){4*(w*i + j-1)+2,c[1]});
                        G[4*(w*i + j)+3].push_back((edge){4*(w*i + j-1)+2,c[3]});
                    }
                    if(j < w-1){
                        G[4*(w*i + j)+2].push_back((edge){4*(w*i + j+1)+1,0});
                        G[4*(w*i + j)+1].push_back((edge){4*(w*i + j+1)+1,c[0]});
                        G[4*(w*i + j)+3].push_back((edge){4*(w*i + j+1)+1,c[1]});
                        G[4*(w*i + j)].push_back((edge){4*(w*i + j+1)+1,c[3]});
                    }
                    if(i < h-1){
                        G[4*(w*i + j)+3].push_back((edge){4*(w*(i+1) + j),0});
                        G[4*(w*i + j)].push_back((edge){4*(w*(i+1) + j),c[0]});
                        G[4*(w*i + j)+1].push_back((edge){4*(w*(i+1) + j),c[1]});
                        G[4*(w*i + j)+2].push_back((edge){4*(w*(i+1) + j),c[3]});
                    }
                }else if(a[i][j] == 3){
                    if(j < w-1){
                        G[4*(w*i + j)].push_back((edge){4*(w*i + j+1)+1,0});
                        G[4*(w*i + j)+1].push_back((edge){4*(w*i + j+1)+1,c[0]});
                        G[4*(w*i + j)+3].push_back((edge){4*(w*i + j+1)+1,c[1]});
                        G[4*(w*i + j)+2].push_back((edge){4*(w*i + j+1)+1,c[2]});
                    }
                    if(0 < i){
                        G[4*(w*i + j)+1].push_back((edge){4*(w*(i-1) + j)+3,0});
                        G[4*(w*i + j)+3].push_back((edge){4*(w*(i-1) + j)+3,c[0]});
                        G[4*(w*i + j)+2].push_back((edge){4*(w*(i-1) + j)+3,c[1]});
                        G[4*(w*i + j)].push_back((edge){4*(w*(i-1) + j)+3,c[2]});
                    }
                    if(i < h-1){
                        G[4*(w*i + j)+2].push_back((edge){4*(w*(i+1) + j),0});
                        G[4*(w*i + j)].push_back((edge){4*(w*(i+1) + j),c[0]});
                        G[4*(w*i + j)+1].push_back((edge){4*(w*(i+1) + j),c[1]});
                        G[4*(w*i + j)+3].push_back((edge){4*(w*(i+1) + j),c[2]});
                    }
                    if(0 < j){
                        G[4*(w*i + j)+3].push_back((edge){4*(w*i + j-1)+2,0});
                        G[4*(w*i + j)+2].push_back((edge){4*(w*i + j-1)+2,c[0]});
                        G[4*(w*i + j)].push_back((edge){4*(w*i + j-1)+2,c[1]});
                        G[4*(w*i + j)+1].push_back((edge){4*(w*i + j-1)+2,c[2]});
                    }
                }
            }
        }
        int res = INF;
        dijkstra(1);
        rep(i,4){
            res = min(res,d[4*(w*(h-1) + w-1)+i]);
        }
        printf("%d\n",res);
    }
	return 0;
}