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>
using namespace std;
#define INF 100000

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

bool check[100][100][4] = { false };
int cost[100][100][4];

class Node{
public:
	int x;
	int y;
	int way;//0 -> up, 1 -> right, 2 -> down, 3-> left
	int num;

	Node(){ x = 0; y = 0; way = 0; num = 0; }
	Node(int x, int y, int way, int num) : x(x), y(y), way(way), num(num){}

};

bool operator< (const Node &n1, const Node &n2){
	return n1.num < n2.num;
}

bool operator>(const Node &n1, const Node &n2){
	return n1.num > n2.num;
}

int main(void){

	int w, h;
	int map[100][100];
	int c[4];

	while (cin >> w >> h, w, h){
		for (int i = 0; i < 100; i++){
			for (int j = 0; j < 100; j++){
				cost[j][i][3] = cost[j][i][2] = cost[j][i][1] = cost[j][i][0] = INF;
			}
		}

		for (int i = 0; i < 100; i++){
			for (int j = 0; j < 100; j++){
				check[j][i][3] = check[j][i][2] = check[j][i][1] = check[j][i][0] = false;
			}
		}

		for (int j = 0; j < h; j++){
			for (int i = 0; i < w; i++){
				cin >> map[j][i];
			}
		}

		cin >> c[0] >> c[1] >> c[2] >> c[3];

		priority_queue<Node, vector<Node>, greater<vector<Node>::value_type> > que;

		que.push(Node(0, 0, 1, 0));

		while (!que.empty()){
			Node buff = que.top();
			que.pop();

			if (check[buff.y][buff.x][buff.way]) continue;
			check[buff.y][buff.x][buff.way] = true;
			cost[buff.y][buff.x][buff.way] = min(cost[buff.y][buff.x][buff.way] , buff.num);

			if ((buff.x == w - 1) && (buff.y == h - 1)){
				cout << buff.num << endl;
				break;
			}

			if (map[buff.y][buff.x] != 4){
				int next_way = (buff.way + map[buff.y][buff.x]) % 4;
				int next_x = buff.x + dx[next_way];
				int next_y = buff.y + dy[next_way];
				if (0 <= next_x && next_x < w && 0 <= next_y && next_y < h)
					que.push(Node(next_x, next_y, next_way, buff.num));
			}

			for (int i = 0; i < 4; i++){
				int next_x = buff.x + dx[i];
				int next_y = buff.y + dy[i];
				if (0 <= next_x && next_x < w && 0 <= next_y && next_y < h)
					que.push(Node(next_x, next_y, i, buff.num + c[(4 + i - buff.way) % 4]));

			}


		}
		/*
		for (int j = 0; j < h; j++){
			for (int i = 0; i < w; i++){
				cout << min(cost[j][i][0], min(cost[j][i][1], min(cost[j][i][2], cost[j][i][3]))) << "\t";
			}
			cout << endl;
		}
		*/

	}

	return 0;
}