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<vector>
#include<string>
#include<cmath>


using namespace std;

int d[400][400] = {};
int _d[11][11] = {};
int dp[1 << 11][11] = {};
int n;

int rec(int s, int v) {
    if(dp[s][v] >= 0) {
        return dp[s][v];
    }

    if(s == (1<<n) - 1 && v== 0) {
        return dp[s][v];
    }

    int res = 500000;
    for(int u = 0; u < n; u++) {
        if(!(s >> u & 1)) {
            res = min(res, rec(s | 1<<u, u) + _d[v][u]);
        }
    }
    return dp[s][v] = res;
}

int main() {
    while(true) {
        string c[20];
        int w,h;
        cin >> w >> h;
        if(w == 0 && h == 0) {
            break;
        }
        for(int i = 0; i < h; i++) {
            cin >> c[i];
        }

        int size = w*h;

        for(int i = 0; i < size; i++) {
            for(int j = 0; j < size; j++) {
                int ix = i%w, iy = i/w, jx = j%w, jy = j/w;
                if(i == j) d[i][j] = 0;
                else if(abs(ix - jx) +abs(iy - jy) == 1 && c[iy][ix] != 'x' && c[jy][jx] != 'x') d[i][j] = 1; 
                else d[i][j] = 50000; 
            }
        }
        for(int k = 0; k < size; k++) {
            for(int i = 0; i < size; i++) {
                for(int j = 0; j < size; j++) {
                    d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
                }
            }
        }

        vector<int> v;
        int rob;
        for(int i = 0; i < h; i++) {
            for(int j = 0; j < w; j++) {
                if(c[i][j] == 'o') rob = j + (i*w);
                if(c[i][j] == '*') v.push_back(j + (i*w));
            }
        }

        bool unreached = false;

        for(int i = 0; i < v.size(); i++) {
            if(d[rob][v[i]] >= 50000) {
                cout << -1 << endl;
                unreached = true;
                break;
            }
            _d[0][i+1] = d[rob][v[i]];
            for(int j = 0; j < v.size(); j++) {
                _d[i+1][j+1] = d[v[i]][v[j]];
            }
        }
        if(unreached) {
            continue;
        }

        n = v.size() + 1;
        for(int i = 0; i <(1 << n); i++) {
            for(int j = 0; j < n; j++) {
                dp[i][j] = -1;
            }
        }

        cout << rec(0,0)+1 << endl;


    }

}