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 JAVA (judge verdict: Memory Limit Exceeded)

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {

	Scanner sc = new Scanner(System.in);

	int w;
	int h;
	int[][] s;
	short[][][] visited;

	int cs[];
	int[] dx = { 1, 0, -1, 0 };
	int[] dy = { 0, 1, 0, -1 };
	final short INF = 1 << 14;

	boolean init() {
		w = sc.nextInt();
		h = sc.nextInt();
		if ((h | w) == 0)
			return false;
		s = new int[w][h];
		visited = new short[w][h][4];
		for (int i = 0; i < w; i++) {
			for (int j = 0; j < h; j++) {
				Arrays.fill(visited[i][j], INF);
			}
		}
		cs = new int[4];
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				s[j][i] = sc.nextInt();
			}
		}
		for (int i = 0; i < 4; i++) {
			cs[i] = sc.nextInt();
		}
		return true;
	}

	void run() {
		while (init()) {
			Queue<Node> q = new LinkedList<Node>();
			q.add(new Node(0, 0, 0, (short) 0));
			int ans = INF;
			while (!q.isEmpty()) {
				Node n = q.poll();
				int x = n.x;
				int y = n.y;
				// System.out.println(n);
				int d = n.d;
				short c = n.c;
				visited[x][y][d] = c;
				if (x == w - 1 && y == h - 1) {
					ans = Math.min(ans, c);
					continue;
				}
				for (int i = 0; i < 4; i++) {
					int nx = x + dx[(d + i) % 4];
					int ny = y + dy[(d + i) % 4];
					if (nx < 0 || nx >= w || ny < 0 || ny >= h) {
						continue;
					}
					if (visited[nx][ny][(d + i) % 4] <= c + cs[i])
						continue;
					Node nn = new Node(nx, ny, (d + i) % 4, (short) (c + cs[i]));
					q.add(nn);
				}
				if (s[x][y] == 4)
					continue;
				int dd = (d + s[x][y]) % 4;
				int nx = x + dx[dd];
				int ny = y + dy[dd];
				if (nx < 0 || nx >= w || ny < 0 || ny >= h) {
					continue;
				}
				q.add(new Node(nx, ny, dd, c));
			}
			System.out.println(ans);
		}
	}

	public static void main(String[] args) {
		new Main().run();

	}
}

class Node {
	public int x;
	public int y;
	public int d;
	public short c;

	Node(int x, int y, int d, short c) {
		this.x = x;
		this.y = y;
		this.d = d;
		this.c = c;
	}

	public String toString() {
		return "( " + x + " " + y + " " + d + " " + c + ") ";
	}

}