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<queue>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
#define INF 10000000
string ch[20];
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int w,h,node;
int vd[20][20],u[11][11],px[11],py[11];
int t[11][11];
int dp[2048][11];
int compute(int,int);
void solve();
int main(){
  while(cin>>w>>h&&w){
    node=1;
    for(int i=0;i<h;i++){
      cin>>ch[i];
      for(int j=0;j<w;j++){
	if(ch[i][j]=='*'){
	  px[node]=j;
	  py[node++]=i;
	}else if(ch[i][j]=='o'){
	  px[0]=j;
	  py[0]=i;
	}
      }
    }
   
    for(int i=0;i<11;i++)for(int j=0;j<11;j++)t[i][j]=INF;
    for(int i=0;i<node-1;i++)
      for(int j=i+1;j<node;j++)
	t[i][j]=t[j][i]=compute(i,j);
    /*
    for(int i=0;i<node;i++){
      for(int j=0;j<node;j++){
	if(t[i][j]==INF){
	  printf("%2d ",-1);
	}else{
	  printf("%2d ",t[i][j]);
	}
      }
      cout<<endl;
    }
    */
    solve();
    
  }
}
int compute(int a,int b){
  for(int i=0;i<20;i++)for(int j=0;j<20;j++)vd[i][j]=0;
  queue<int>qx,qy,qz;
  int x,y,z,ny,nx;
  qx.push(px[a]);qy.push(py[a]),qz.push(0);
  vd[py[a]][px[a]]=1;
  while(!qx.empty()){
    x=qx.front();qx.pop();
    y=qy.front();qy.pop();
    z=qz.front();qz.pop();
    if(x==px[b]&&y==py[b])return z;
    for(int i=0;i<4;i++){
      ny=y+dy[i];
      nx=x+dx[i];
      if(ny<0||nx<0||ny>=h||nx>=w){
	continue;
      }
      
      if(ch[ny][nx]=='x')continue;
      if(!vd[ny][nx]){
	qy.push(ny);
	qx.push(nx);
	qz.push(z+1);
	vd[ny][nx]=1;
      }
    }
  }
  return INF;
}

int rec(int S,int v){
  if(dp[S][v]>=0){
    return dp[S][v];
  }
  if(S==(1<<node)-1){
    return dp[S][v]=0;
  }
  int res = INF;
  for(int u=0;u<node;u++){
    
    if(!(S>>u&1)){
      res=min(res,rec(S|(1<<u),u)+t[v][u]);
    }
    
  }
  return dp[S][v]=res;
}
void solve(){
  for(int i=0;i<2048;i++)
    for(int j=0;j<11;j++)
      dp[i][j]=-1;
  int out=rec(1,0);
  if(out!=INF)cout<<out<<endl;
  else cout<<-1<<endl;
}