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: Wrong Answer)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#define INF 1<<30
using namespace std;
typedef pair<int,int> P;
int w,h;
char cmap[30][30];
int imap[30][30];
int bfsd[60][60];
int d[60][60];
int dd[60];
bool ddused[60];
P pointUsed[60];
int index;
const int dx[4] = {-1,0,1,0};
const int dy[4] = {0,1,0,-1}; 
int counter;
int ssx,ssy;
int sindex;

void bfs(int sf,int ss){
  queue<P> que;
  que.push(P(sf,ss));
  for(int i=0;i<h;i++)for(int j=0;j<w;j++)bfsd[i][j]=INF;
  bfsd[ss][sf]=0;

  while(que.size()){
    P p = que.front(); que.pop();

    for(int i=0;i<4;i++){
      int nx = p.first + dx[i],ny = p.second + dy[i];
      if(0<= nx && nx < w && 0<= ny && ny < h && bfsd[ny][nx] == INF && cmap[ny][nx] != 'x'){
	bfsd[ny][nx] = bfsd[p.second][p.first]+1;
	que.push(P(nx,ny));
	if(cmap[ny][nx] == '*' || cmap[ny][nx] == 'o'){
	  counter++;
	  //cout << imap[ss][sf]+50 << "  " << imap[ny][nx]+50 << endl;
	  d[ imap[ss][sf]+50 ][ imap[ny][nx]+50 ] = bfsd[ny][nx];
	  d[ imap[ny][nx]+50 ][ imap[ss][sf]+50 ] = bfsd[ny][nx];
	}

      }

    }



  }

}

int dijkstra(){ // let dijkstra be prim
  for(int i=0;i<index;i++){
    ddused[i]=false;
    dd[i]=INF;
  }    
  for(int i=0;i<index;i++){
    if(i != sindex){
      dd[i]=0;
      break;
    }
  }
  int res=0;

  while(true){
    int v = -1;
    for(int u=0;u<index;u++)if(u != sindex && !ddused[u] && ( v == -1 || dd[v] > dd[u]))v=u;
    if(v == -1)break;
    ddused[v]=true;
    res+=dd[v];
    

    for(int u=0;u<index;u++){
      if(u!=sindex)dd[u] = min(dd[u],d[v][u]);
    }
  }
  
  return res;

}




int main(){
  int sx,sy;
  int dirtyIndex;
  P dirtyPoint[500]; //let first be x and second be y
  while(true){
    cin >> w >> h;
    if(w + h == 0)break;
   
    index=0;
    dirtyIndex=-50;
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin >> cmap[i][j];
	switch(cmap[i][j]){
	case '.':
	  imap[i][j]=0;
	  break;
	case 'o':
	  imap[i][j]=dirtyIndex;
	  dirtyIndex++;
	  pointUsed[index] = P(j,i);
	  sindex=index;
	  ++index;
	  sy=i;
	  sx=j;
	  ssx = sx;
	  ssy = sy;
	  break;
	case '*':
	  dirtyPoint[index].first = j;
	  dirtyPoint[index].second = i;
	  imap[i][j] = dirtyIndex;
	  dirtyIndex++;
	  pointUsed[index] = P(j,i);
	  ++index;
	  break;
	case 'x':
	  imap[i][j]=-1;
	  break;
	default:
	  break;
	}
      }
    }
    bool fin=false;
    for(int i=0;i<index;i++){
      counter=1;
      bfs(pointUsed[i].first,pointUsed[i].second);
      if(i == 0 && counter != index){
	cout << "-1" << endl;
      fin=true;
      break;
      }

    }

    if(fin)continue;
   
    //int kotae = dijkstra();

    //int min=INF;
    //for(int i=0;i<index;i++){
    //if(i == sindex)continue;
    //if(d[sindex][i] < min)min = d[sindex][i];
    //}
    //cout << min+kotae << endl;

    int dp[1<<index][index];

    for(int S=0;S< (1<<index);S++){
      for(int i=0;i<=index+1;i++)dp[S][i] = INF;
    }
    dp[(1<<index)-1][0] = 0;
    for(int S = (1<<index)-2;S>=0;S--){
      for(int v=0;v<index;v++){
	for(int u=0;u<index;u++){
	  if(!(S>>u & 1)){
	    dp[S][v] = min(dp[S][v],dp[S|1<<u][u] + d[v][u]);
	  }
	}
      }
    }
  
    int Kotae=INF;
    for(int i=0;i<index;i++)if(Kotae > dp[0][i])Kotae = dp[0][i];
    cout << Kotae << endl;

  }

  return 0;
}