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: Compile Error)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<deque>
#include<queue>

using namespace std;

int main(){
  int h,w;
  while(scanf("%d%d\n",&w,&h),w+h){
    char board[22][22]={{0}};
    deque<pair<int,int> > pos;
    int count=3;
    for(int i=1;i<=h;i++){
      gets(&board[i][1]);
      for(int j=1;j<=w;j++){
	if(board[i][j]=='x')      board[i][j]=0;
	else if(board[i][j]=='.') board[i][j]=1;
	else if(board[i][j]=='*'){
	  board[i][j]=count++;
	  pos.push_back(make_pair(j,i));
	}else if(board[i][j]=='o'){
	  board[i][j]=2;
	  pos.push_front(make_pair(j,i));
	}
      }
    }

    /*
    for(int i=0;i<h+2;i++){
      for(int j=0;j<w+2;j++){
	printf("%d ",board[i][j]);
      }
      puts("");
    }
    */

    //int darty=pos.size()-1;
    int dist[11][11]={{0}};
    bool possible=true;
    for(int i=0;i<pos.size()-1;i++){
      //dijkstra
      int flag[22][22]={{0}};
      int d[22][22];
      for(int x=0;x<22;x++)
	for(int y=0;y<22;y++)
	  d[y][x]=1000;
      count=0;
      priority_queue<pair<int,pair<int,int> > > q;
      q.push(make_pair(0,make_pair(pos[i].first,pos[i].second)));
      while(!q.empty()){
	if(count==pos.size()) break;
	int x=q.top().second.first,y=q.top().second.second,hy=q.top().first;
	q.pop();
	//printf("(%d,%d)=%d\n",x,y,-hy);
	if(flag[y][x]!=0) continue;
	flag[y][x]=1;
	if(board[y][x]>=2){//darty or initial
	  //printf("test %d\n",board[y][x]-2);
	  dist[i][board[y][x]-2]=-hy;
	  dist[board[y][x]-2][i]=-hy;
	  count++;
	}
	if(board[y][x-1]!=0 && d[y][x-1]>-hy+1){
	  d[y][x-1]=-hy+1;
	  q.push(make_pair(hy-1,make_pair(x-1,y)));
	}
	if(board[y][x+1]!=0 && d[y][x+1]>-hy+1) {
	  d[y][x+1]=-hy+1;
	  q.push(make_pair(hy-1,make_pair(x+1,y)));
	}
	if(board[y-1][x]!=0 && d[y-1][x]>-hy+1){
	  d[y-1][x]=-hy+1;
	  q.push(make_pair(hy-1,make_pair(x,y-1)));
	}
	if(board[y+1][x]!=0 && d[y+1][x]>-hy+1){
	  d[y+1][x]=-hy+1;
	  q.push(make_pair(hy-1,make_pair(x,y+1)));
	}
      }
      if(count!=pos.size()){
	possible=false;
	break;
      }
    }
    if(!possible){
      puts("-1");
      continue;
    }
    
    /*
    for(int i=0;i<pos.size();i++){
      for(int j=0;j<pos.size();j++){
	printf("%3d",dist[i][j]);
      }
      printf("\n");
    }
    */
    //traveling salesman problem
    vector<int> v(pos.size()-1);
    int ans=100000;
    for(int i=0;i<pos.size()-1;i++) v[i]=i+1;
    do{
      int tmp=dist[0][v[0]];
      for(int i=0;i<pos.size()-2;i++){
	tmp+=dist[v[i]][v[i+1]];
	if(tmp>ans){
	  sort(&v[i+1],&v[pos.size()-2],greater<int>());
	  break;
	}
      }
      if(ans>tmp) ans=tmp;
    }while(next_permutation(v.begin(),v.end()));
    printf("%d\n",ans);
  }
  return 0;
}