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 Ruby (judge verdict: Accepted)

INF = 10000
$dx = [1,0,-1,0]
$dy = [0,1,0,-1]
$dp = []
$n = 100

def bfs(pos,queue,bb) 
	while(not queue.empty?) do
		pos = queue.shift
		cnt = bb[pos[0]][pos[1]]
		npos = []
		4.times{|i|
			npos[0] = pos[0] + $dy[i]
			npos[1] = pos[1] + $dx[i]
			if ((0..$h-1).include?(npos[0]) and (0..$w-1).include?(npos[1])) then
				if ($b[npos[0]][npos[1]] == 0 and bb[npos[0]][npos[1]] == -1) then
					bb[npos[0]][npos[1]] = cnt + 1
					queue.push([npos[0],npos[1]])
				end
			end
		}
	end
end

def rec(s,v)
	if $dp[s][v] >= 0 then
		return $dp[s][v]
	end
	
	if ( s == ((1 << $n) -1)) then
		return $dp[s][v] = $dist[0][v+1]
	end
	
	res = INF
	$n.times{|u|
		if ((s >> u & 1) == 0) then
			
			tmp = (rec(s | 1 << u,u) + $dist[v+1][u+1] )
			if tmp < res then
				res = tmp
			end
		end
	}
	
	return $dp[s][v] = res
end

loop{

	$w,$h =gets.split.map(&:to_i)
	if ($w==0 and $h == 0) then
		break
	end
	$b = Array.new($h).map{Array.new($w)}
	d = Array.new()
	dcost = Array.new()
	s = Array.new(2)
	
	$h.times{|hh|
		$b[hh] = gets.chomp.chars.each_with_index.map{|c,i|
			case c 
			when '.'
				0
			when '*'
				d.push([hh,i])
				0
			when 'o'
				s = [hh,i]
				0
			else
				-100
			end
		}
	}
	bb = Array.new($h).map{Array.new($w,-1)}
	queue = []
	queue.push(s)
	bb[s[0]][s[1]] = 0
	bfs(s,queue,bb)

	flag = true
	
	dsize = d.size()
	$n = dsize
	$dist = Array.new(dsize+1).map{Array.new(dsize+1,-1)}
	$dist[0][0] = 0
	d.each_with_index{|pos,i|
		$dist[0][i+1] = bb[pos[0]][pos[1]]
		$dist[i+1][0] = bb[pos[0]][pos[1]]
		
		if bb[pos[0]][pos[1]] == -1 then
			flag = false
		end
	}
	
	if (!flag) then
		print "-1\n"
		next
	end
	
	

	d.each_with_index{|pos,i|
		bb = Array.new($h).map{Array.new($w,-1)}
		queue = []
		queue.push(pos)
		bb[pos[0]][pos[1]] = 0
		bfs(pos,queue,bb)
		dsize.times{|j|
			pos2 = d[j]
			$dist[i+1][j+1] = bb[pos2[0]][pos2[1]]
		}
		
	}
	$dp = Array.new(1 << $n).map{Array.new($n,-1)}
	ans = INF
	$n.times{|i|
		tmp = rec(0,i)
		if ans > tmp then
			ans = tmp
		end
	}
	p ans
}