Problem:
Problem G: Walking Ant

Ants are quite diligent. They sometimes build their nests beneath flagstones.

Here, an ant is walking in a rectangular area tiled with square flagstones, seeking the only hole leading to her nest.

The ant takes exactly one second to move from one flagstone to another. That is, if the ant is on the flagstone with coordinates (x, y) at time t, she will be on one of the five flagstones with the following coordinates at time t + 1:

(x, y), (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1).

The ant cannot go out of the rectangular area. The ant can visit the same flagstone more than once.

Insects are easy to starve. The ant has to go back to her nest without starving. Physical strength of the ant is expressed by the unit "HP". Initially, the ant has the strength of 6 HP. Every second, she loses 1 HP. When the ant arrives at a flagstone with some food on it, she eats a small piece of the food there, and recovers her strength to the maximum value, i.e., 6 HP, without taking any time. The food is plenty enough, and she can eat it as many times as she wants.

When the ant's strength gets down to 0 HP, she dies and will not move anymore. If the ant's strength gets down to 0 HP at the moment she moves to a flagstone, she does not effectively reach the flagstone: even if some food is on it, she cannot eat it; even if the hole is on that stone, she has to die at the entrance of her home.

If there is a puddle on a flagstone, the ant cannot move there.

Your job is to write a program which computes the minimum possible time for the ant to reach the hole with positive strength from her start position, if ever possible.

Input

The input consists of multiple maps, each representing the size and the arrangement of the rectangular area. A map is given in the following format.

w h
d11 d12 d13 ... d1w
d2 d22 d23 ... d2w
...
dh1 dh2 dh3 ... dhw

The integers w and h are the numbers of flagstones in the x- and y-directions, respectively. w and h are less than or equal to 8. The integer dyx represents the state of the flagstone with coordinates (x, y) as follows.

0: There is a puddle on the flagstone, and the ant cannot move there.

1, 2: Nothing exists on the flagstone, and the ant can move there. '2' indicates where the ant initially stands.

3: The hole to the nest is on the flagstone.

4: Some food is on the flagstone.

There is one and only one flagstone with a hole. Not more than five flagstones have food on them.

The end of the input is indicated by a line with two zeros.

Integer numbers in an input line are separated by at least one space character.

Output

for each map in the input, your program should output one line containing one integer representing the minimum time. If the ant cannot return to her nest, your program should output -1 instead of the minimum time.

Sample Input

3 3
2 1 1
1 1 0
1 1 3
8 4
2 1 1 0 1 1 1 0
1 0 4 1 1 0 4 1
1 0 0 0 0 0 0 1
1 1 1 4 1 1 1 3
8 5
1 2 1 1 1 1 1 4
1 0 0 0 1 0 0 1
1 4 1 0 1 1 0 1
1 0 0 0 0 3 0 1
1 1 4 1 1 1 1 1
0 0

Output for the Sample Input

4
-1
13

Solution in JAVA (judge verdict: Accepted)

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {

	public static final int MAX_HP = 6;
	public static final int MAX = 8;
		
	public static final int[][] move_dir = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
	
	public static boolean is_ok(int x, int y, int h, int w){
		if(x < 0 || x >= w || y < 0 || y >= h){
			return false;
		}else{
			return true;
		}
	}
	
	public static void main(String[] args) {
		final Scanner sc = new Scanner(System.in);
		
		boolean[][] can_enter = new boolean[MAX][MAX];
		boolean[][] on_food = new boolean[MAX][MAX];
		boolean[][][] visited = new boolean[MAX][MAX][MAX_HP + 1];
		
		while (true) {
			final int w = sc.nextInt();
			final int h = sc.nextInt();
			
			if(w == 0 && h == 0){
				break;
			}
			
			int sx = -1, sy = -1, gx = -1, gy = -1;
			
			for(int i = 0; i < h; i++){
				for(int j = 0; j < w; j++){
					final int num = sc.nextInt();
					
					can_enter[i][j] = num != 0;
					
					if(num == 2){
						sx = j;
						sy = i;
					}else if(num == 3){
						gx = j;
						gy = i;
					}
					
					on_food[i][j] = num == 4;
					
					for(int k = 0; k <= MAX_HP; k++){
						visited[i][j][k] = false;
					}
				}
			}
			
			LinkedList<Integer> x_queue = new LinkedList<Integer>();
			LinkedList<Integer> y_queue = new LinkedList<Integer>();
			LinkedList<Integer> hp_queue = new LinkedList<Integer>();
			LinkedList<Integer> t_queue = new LinkedList<Integer>();
			
			x_queue.add(sx);
			y_queue.add(sy);
			hp_queue.add(MAX_HP);
			t_queue.add(0);
			visited[sy][sx][MAX_HP] = true;
			
			boolean flag = false;
			while(!x_queue.isEmpty()){
				final int x = x_queue.poll();
				final int y = y_queue.poll();
				final int hp = hp_queue.poll();
				final int time = t_queue.poll();
				
				if(hp <= 0){
					continue;
				}
				
				if(gx == x && gy == y){
					System.out.println(time);
					flag = true;
					break;
				}
				
				//System.out.println(x + " " + y + " " + hp + " " + time);
		
				final int next_hp = on_food[y][x] ? MAX_HP - 1 : hp - 1;
				
				if(next_hp <= 0){
					continue;
				}
				
				for(int[] move : move_dir){
					final int nx = x + move[0];
					final int ny = y + move[1];
					
					if(!is_ok(nx, ny, h, w)){
						continue;
					}else if(!can_enter[ny][nx]){
						continue;
					}else if(visited[ny][nx][next_hp]){
						continue;
					}
					
					visited[ny][nx][next_hp] = true;
					x_queue.add(nx);
					y_queue.add(ny);
					hp_queue.add(next_hp);
					t_queue.add(time + 1);
				}
			}
			
			if(!flag){
				System.out.println(-1);
			}
		}
	}

}