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: Accepted)

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


public class Main {
	
	public static final int WIDTH = 20;
	public static final int HEIGHT = 20;
	public static final int MAX_BIT = (1 << 10) - 1;
	
	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 w, int h){
		return 0 <= x && x < w && 0 <= y && y < h;
	}
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		boolean[][] is_wall = new boolean[HEIGHT][WIDTH];
		int[][] is_dirty = new int[HEIGHT][WIDTH];
		boolean[][][] visited = new boolean[HEIGHT][WIDTH][MAX_BIT];
		
		while(true){
			final int w = sc.nextInt();
			final int h = sc.nextInt();
					
			if(w == 0 && h == 0){
				break;
			}
		
			int start_x = -1;
			int start_y = -1;
			int dirty_id = 0;
			
			for(int i = 0; i < h; i++){
				char[] input = sc.next().toCharArray();
				
				//System.out.println(Arrays.toString(input));
				
				for(int j = 0; j < w; j++){
					final char in = input[j];
					
					for(int k = 0; k < MAX_BIT; k++){
						visited[i][j][k] = false;
					}
					
					if(in == '.'){
						is_wall[i][j] = false;
						is_dirty[i][j] = -1;
					}else if(in == '*'){
						is_wall[i][j] = false;
						is_dirty[i][j] = dirty_id;
						dirty_id++;
					}else if(in == 'x'){
						is_wall[i][j] = true;
						is_dirty[i][j] = -1;
					}else if(in == 'o'){
						is_wall[i][j] = false;
						is_dirty[i][j] = -1;
						start_x = j;
						start_y = i;
					}
				}
			}
			
			final int all_dirty = (1 << dirty_id) - 1;
			
			LinkedList<Integer> x_queue = new LinkedList<Integer>();
			LinkedList<Integer> y_queue = new LinkedList<Integer>();
			LinkedList<Integer> time_queue = new LinkedList<Integer>();
			LinkedList<Integer> dirty_queue = new LinkedList<Integer>();
			x_queue.add(start_x);
			y_queue.add(start_y);
			time_queue.add(0);
			dirty_queue.add(0);
			visited[start_y][start_x][0] = true;
			
			boolean flag = false;
			while(!time_queue.isEmpty()){
				final int x = x_queue.poll();
				final int y = y_queue.poll();
				final int time = time_queue.poll();
				final int dirty_bit = dirty_queue.poll();
				
				if(dirty_bit == all_dirty){
					flag = true;
					System.out.println(time);
					break;
				}
				
				for(int[] move : move_dir){
					final int nx = x + move[0];
					final int ny = y + move[1];
					final int ntime = time + 1;
					
					if(!is_ok(nx, ny, w, h)){
						continue;
					}else if(is_wall[ny][nx]){
						continue;
					}
					//System.out.println(nx + " " + ny);
					int next_dirty = dirty_bit;
					if(is_dirty[ny][nx] >= 0){
						next_dirty |= 1 << is_dirty[ny][nx];
					}
					
					if(visited[ny][nx][next_dirty]){
						continue;
					}
					visited[ny][nx][next_dirty] = true;
					
					x_queue.add(nx);
					y_queue.add(ny);
					time_queue.add(ntime);
					dirty_queue.add(next_dirty);
				}
			}
			
			if(!flag){
				System.out.println("-1");
			}
		}
	}
	
}