Problem:
Problem F: Cleaning Robot

Here, we want to solve path planning for a mobile robot cleaning a
rectangular room floor with furniture.

Consider the room floor paved with square tiles whose size fits
the cleaning robot (1 × 1).
There are 'clean tiles' and 'dirty tiles', and the robot can change
a 'dirty tile' to a 'clean tile' by visiting the tile.
Also there may be some obstacles (furniture) whose size fits a
tile in the room. If there is an obstacle on a tile, the robot cannot visit
it.
The robot moves to an adjacent tile with one move.
The tile onto which the robot moves must be one of four tiles
(i.e., east, west, north or south) adjacent to the tile where the
robot is present. The robot may visit a tile twice or more.

Your task is to write a program which computes the minimum number of
moves for the robot to change all 'dirty tiles' to 'clean tiles',
if ever possible.

Input

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

w h

c11 c12 c13 ... c1w

c21 c22 c23 ... c2w

...

ch1 ch2 ch3 ... chw

The integers w and h are the lengths of the two sides
of the floor of the room in terms of widths of floor tiles. w and h
are less than or equal to 20. The character cyx represents
what is initially on the tile with coordinates (x, y) as follows.

'.' : a clean tile

'*' : a dirty tile

'x' : a piece of furniture (obstacle)

'o' : the robot (initial position)

In the map the number of 'dirty tiles' does not exceed 10.
There is only one 'robot'.

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

Output

For each map, your program should output a line containing the minimum
number of moves. If the map includes 'dirty tiles' which the robot cannot reach,
your program should output -1.

Sample Input

7 5
.......
.o...*.
.......
.*...*.
.......
15 13
.......x.......
...o...x....*..
.......x.......
.......x.......
.......x.......
...............
xxxxx.....xxxxx
...............
.......x.......
.......x.......
.......x.......
..*....x....*..
.......x.......
10 10
..........
..o.......
..........
..........
..........
.....xxxxx
.....x....
.....x.*..
.....x....
.....x....
0 0

Output for the Sample Input

8
49
-1

Solution in JAVA (judge verdict: Wrong Answer)

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

public class Main {

	static int w,h,startx,starty;
	static int[][]field;
	static int[][]kyori;
	static List<int[]> list;
	static byte[] v1={1,0,-1,0};
	static byte[] v2={0,-1,0,1};
	public static void main(String[] args) {
		Scanner cin=new Scanner(System.in);
		label:while(true){
			w=cin.nextInt();
			h=cin.nextInt();
			if(w+h==0)break;
			field=new int[h+2][w+2];
			list=new ArrayList<int[]>();
			int cnt=0;
			for(int i=0;i<=h+1;i++){
				Arrays.fill(field[i], -1);
			}
			for(int i=1;i<=h;i++){
				char[] s=cin.next().toCharArray();
				for(int j=1;j<=w;j++){
					if(s[j-1]=='.'){
						field[i][j]=-2;
					}
					else if(s[j-1]=='x'){
						field[i][j]=-1;
					}
					else if(s[j-1]=='*'){
						field[i][j]=++cnt;

						list.add(new int[]{i,j});
					}
					else{
						field[i][j]=0;
						startx=i;
						starty=j;
					}
				}
			}
			for(int i=1;i<=h;i++){
				for(int j=1;j<=w;j++){
//					System.out.print(field[i][j]);
				}
//				System.out.println();
			}
			//0,0がスタート地点
			kyori=new int[cnt+1][cnt+1];
			a(startx,starty,0);
			cnt=1;
			int[][] b=new int[list.size()][2];
			for(int i=0;i<list.size();i++){
				int[] array=list.get(i);
				b[i][0]=array[0];
				b[i][1]=array[1];
			}
			list=null;
			for(int i=0;i<b.length;i++){
				a(b[i][0],b[i][1],cnt++);
			}
			for(int i=0;i<kyori[0].length;i++){
				for(int j=0;j<kyori[0].length;j++){
					if(i==j)continue;
					if(kyori[i][j]==0){
						System.out.println(-1);
						continue label;
					}
//					System.out.println(i+" "+j+" "+kyori[i][j]);
				}
			}
			sum=0;
			ans=1<<30;
			bt(0,0);
			
			System.out.println(ans);
		}
	}
	static int sum,ans;
	static void bt(int a,int step){
//		System.out.println(a+" "+step+" "+sum);
		if(ans<=sum)return;
		if(step==kyori[0].length-1){
			ans=Math.min(ans,sum);
		}
		for(int i=0;i<kyori[0].length;i++){
			if(a==i)continue;
			if(kyori[a][i]==0)continue;
			int tmp=kyori[a][i];
			kyori[a][i]=kyori[i][a]=0;
			sum+=tmp;
			bt(i,step+1);
			kyori[a][i]=kyori[i][a]=tmp;
			sum-=tmp;
		}
	}
	static void a(int x,int y,int cnt){
		int[][] f=new int[h+2][w+2];
		for(int i=0;i<h+2;i++){
			for(int j=0;j<w+2;j++){
				f[i][j]=field[i][j];
			}
		}
		Queue<byte[]> q=new LinkedList<byte[]>();
		q.add(new byte[]{(byte)x,(byte)y});
		int steps=0;
		
		while(!q.isEmpty()){
			int size=q.size();
			for(int i=0;i<size;i++){
				byte[] aaa=q.poll();
				byte xx=aaa[0];
				byte yy=aaa[1];
				if(f[xx][yy]==-1)continue;
				if(f[xx][yy]>=0){
					kyori[cnt][f[xx][yy]]=steps;kyori[f[xx][yy]][cnt]=steps;
				}
				f[xx][yy]=-1;
				for(int j=0;j<4;j++){
					byte x1=(byte) ((byte)xx+v1[j]);
					byte y1=(byte) ((byte)yy+v2[j]);
					if(f[x1][y1]==-1)continue;
					q.add(new byte[]{x1,y1});
				}				
			}
			steps++;
		}		
	}

}