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

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <string.h>

using namespace std;

struct Point {
  int y;
  int x;
  int cost;
  Point() {;}
  Point(int y1, int x1, int c) : y(y1), x(x1), cost(c) {;}
};

int width;
int height;

char str[100];
char tile[30][30];
int dist[11][10];
int dirty_y[10];
int dirty_x[10];
int map[30][30];
bool check[30][30];
int dirty_tile;
int memo[1024][10];

int calc(int depth, int visit, int where) {
  if (memo[visit][where] != -1) { return memo[visit][where]; }
  if (depth == dirty_tile) { return 0; }
  int ret = 0x0f0f0f0f;
  for (int i = 0; i < dirty_tile; i++) {
    if (visit & (1 << i)) { continue; }
    int next_visit = visit | (1 << i);
    ret = min(ret, calc(depth + 1, next_visit, i) + dist[where][i]);
  }
  return memo[visit][where] = ret;
}

int main() {
  while (scanf("%d %d", &width, &height), width != 0 && height != 0) {
    fgets(str, 99, stdin);
    for (int i = 0; i < height; i++) {
      fgets(tile[i], 29, stdin);
    }
    int start_y;
    int start_x;
    dirty_tile = 0;
    for (int y = 0; y < height; y++) {
      for (int x = 0; x < width; x++) {
        if (tile[y][x] == '*') {
          dirty_y[dirty_tile] = y;
          dirty_x[dirty_tile] = x;
          map[y][x] = dirty_tile;
          dirty_tile++;
        } else if (tile[y][x] == 'o') {
          start_y = y;
          start_x = x;
        }
      }
    }
    dirty_y[dirty_tile] = start_y;
    dirty_x[dirty_tile] = start_x;
    map[start_y][start_x] = dirty_tile;
    memset(dist, -1, sizeof(dist));
    static const int dy[4] = { 1, -1, 0, 0 };
    static const int dx[4] = { 0, 0, 1, -1 };
    for (int i = 0; i < dirty_tile + 1; i++) {
      queue<Point> que;
      que.push(Point(dirty_y[i], dirty_x[i], 0));
      memset(check, false, sizeof(check));
      while (!que.empty()) {
        Point now = que.front();
        que.pop();
        if (check[now.y][now.x]) { continue; }
        check[now.y][now.x] = true;
        if (tile[now.y][now.x] == '*') {
          dist[i][map[now.y][now.x]] = now.cost;
        }
        for (int i = 0; i < 4; i++) {
          int ny = now.y + dy[i];
          int nx = now.x + dx[i];
          if (ny < 0 || ny >= height || nx < 0 || nx >= width || check[ny][nx] || tile[ny][nx] == 'x') { continue; }
          que.push(Point(ny, nx, now.cost + 1));
        }
      }
    }
    for (int i = 0; i < dirty_tile; i++) {
      if (dist[dirty_tile][i] == -1) {
        puts("-1");
        goto next_loop;
      }
    }
    memset(memo, -1, sizeof(memo));
    printf("%d\n", calc(0, 0, dirty_tile));
next_loop:
    ;
  }
}