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<string>
#include<vector>
#include<string.h>
#include<map>
#include<queue>
using namespace std;

#define INF 100000


int w,h;
int dist[11][11];
vector<string> M;
map<pair<int,int>,int> Idx;
pair<int,int> point[11];
bool visit[400][400];
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};

void calc_dist(int num){
	for(int i=0;i<num;i++){
		queue<pair<pair<int,int>,int> > Q;
		Q.push(make_pair(point[i],0));
		memset(visit,false,sizeof(visit));
		while(!Q.empty()){
			pair<int,int> now=Q.front().first;
			int cost=Q.front().second;
			Q.pop();
			if(now.first>=w || now.second>=h || now.first<0 || now.second<0) continue;
			if(M[now.second][now.first]=='x') continue;
			if(visit[now.second][now.first]) continue;
			visit[now.second][now.first]=true;
			if(Idx.count(now))
				dist[i][Idx[now]]=cost;

			for(int r=0;r<4;r++)
				Q.push(make_pair(make_pair(now.first+dx[r],now.second+dy[r]),cost+1));
		}
	}
}

int main()
{
	while(cin>>w>>h && w!=0){
		M.clear();
		int start=0;
		Idx.clear();
		int count=1;
		for(int i=0;i<h;i++){
			string s; cin>>s; M.push_back(s);
			for(int n=0;n<s.size();n++){
				if(s[n]=='o'){
					Idx[make_pair(n,i)]=0;
					point[0]=make_pair(n,i);
				}
				else if(s[n]=='*'){
					Idx[make_pair(n,i)]=count;
					point[count]=make_pair(n,i);
					count++;
				}
			}
		}
		memset(dist,-1,sizeof(dist));
		calc_dist(count);
		int ans=INF;

		for(int i=0;i<count;i++){
			for(int j=0;j<count;j++){
				if(dist[i][j]<0){
					ans=-1;
				}
			}
		}

		int dp[1<<11][11];
		for(int i=0;i<11;i++)
			for(int b=0;b<(1<<11);b++)
				dp[b][i]=INF;

		dp[(1<<count)-1][0]=0;

		if(ans>=0){
			for(int S=(1<<count)-2;S>=0;S--){
				for(int v=0;v<count;v++){
					for(int u=0;u<count;u++){
						if(!(S>>u&1)){//à¤Â¢Äéæ
							dp[S][v]=min(dp[S][v],dp[S | 1<<u][u]+dist[v][u]);
						}
					}
				}
			}


			for(int i=0;i<count;i++)
				ans=min(ans,dp[0][i]);
		}
		cout<<ans<<endl;

	}
}