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)

### constant

INF = 1 << 30

DXYS = [[1, 0], [0, -1], [-1, 0], [0, 1]]

### main

loop do
  w, h = gets.split.map(&:to_i)
  break if (w | h) == 0

  wh = w * h

  tls = wh.times.map{0}
  ndtl = 0
  spos = nil

  for y in (0...h)
    line = gets.strip
    for x in (0...w)
      pos = y * w + x
      case line[x]
      when '*'
        ndtl += 1
        tls[pos] = ndtl
      when 'x'
        tls[pos] = -1
      when 'o'
        spos = pos
      end
    end
  end
  #p tls
  #p spos

  dbits = (1 << ndtl) - 1

  dists = wh.times.map{(dbits + 1).times.map{INF}}
  dists[spos][dbits] = 0
  q = [[spos, dbits]]

  min_dist = INF

  while ! q.empty?
    upos, ubits = q.shift
    ud = dists[upos][ubits]

    if ubits == 0
      min_dist = ud if min_dist > ud
      next
    end

    ux = upos % w
    uy = upos / w

    DXYS.each do |dx, dy|
      vx = ux + dx
      vy = uy + dy
      vpos = vy * w + vx

      if vx >= 0 && vx < w && vy >= 0 && vy < h && tls[vpos] >= 0
        vbits = ubits
        if tls[vpos] > 0
          vbits &= ~(1 << (tls[vpos] - 1))
        end

        vd = ud + 1
        if dists[vpos][vbits] > vd
          dists[vpos][vbits] = vd
          q << [vpos, vbits]
        end
      end
    end
  end

  puts (min_dist >= INF ? -1 : min_dist)
  #p dists
end