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<stdio.h>
int MIN(int a,int b){
  if(a==0||a>b)return b;
  return a;
}
int main(){
  int Y[]={-1,0,1,0};
  int X[]={0,1,0,-1};
  int w,h,i,j,k,l,ny,nx,ax[15],ay[15],x[900],y[900],r,t,rr,min;
  while(scanf("%d %d",&w,&h),w||h){
    char d[30][30]={0};
    int dp[10][1<<10]={0};
    int m[30][30]={0};
    int f[30][30]={0};
    int c[30][30]={0};
    r=0;
    for(i=rr=1;i<=h;i++){
      for(j=1;j<=w;j++){
	scanf(" %c",&d[i][j]);
	if(d[i][j]=='o')d[ay[0]=i][ax[0]=j]='.';
	if(d[i][j]=='*'){
	  d[ay[rr]=i][ax[rr]=j]='.';
	  rr++;
	}
      }
    }
    for(i=min=0;i<rr;i++){
      f[y[t=0]=ay[i]][x[t]=ax[i]]=i+1;
      c[y[t]][x[t]]=0;
      for(r=1;r-t;t++){
	for(j=0;j<4;j++){
	  ny=y[t]+Y[j];
	  nx=x[t]+X[j];
	  if(d[ny][nx]-'.'||f[ny][nx]==i+1)continue;
	  c[y[r]=ny][x[r]=nx]=c[y[t]][x[t]]+1;
	  f[ny][nx]=i+1;
	  r++;
	}
      }
      for(j=1;j<rr;j++)m[i][j]=c[ay[j]][ax[j]];
    }
    rr--;
    for(i=0;i<rr;i++){
      dp[i][1<<i]=m[0][i+1];
      if(m[0][i+1]==0)rr=0;
    }
    for(l=0;l<rr;l++){
      for(i=0;i<rr;i++){
	for(j=0;j<1<<rr;j++){
	  if(dp[i][j]==0)continue;
	  for(k=0;k<rr;k++){
	    if(j&1<<k)continue;
	    dp[k][j|1<<k]=MIN(dp[k][j|1<<k],dp[i][j]+m[i+1][k+1]);
	  }
	}
      }
    }
    for(i=min=0;i<rr;i++)min=MIN(min,dp[i][(1<<rr)-1]);
    if(min)printf("%d\n",min);
    else   printf("-1\n");
  }
  return 0;
}