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 <algorithm>
#include <vector>
#include <queue>
#include <climits>
#define INF INT_MAX>>1 //要climits#includ

using namespace std;

int dx[4] ={-1, 0, 0, 1};
int dy[4] ={0, -1, 1, 0};

int main(void){
	while(1){
		int w, h; cin >> w >> h;
		if(!w && !h) break;

		char c[21][21];
		pair <int, int> node[11];
		int count = 1;
		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				cin >> c[i][j];
				if(c[i][j] == 'o'){
					node[0].first = i;
					node[0].second = j;
				} else if(c[i][j] == '*'){
					node[count].first = i;
					node[count].second = j;
					count++;
				}
			}
		}

		int adj[11][11] ={0};
		for(int i=0; i<count; i++){
			queue <pair<int, int> > que;
			que.push(make_pair(node[i].first, node[i].second));
			int dist[21][21]={0};
			while(!que.empty()){
				int y = que.front().first;
				int x = que.front().second;
				que.pop();

				for(int j=0; j<4; j++){
					int ny = y+dy[j];
					int nx = x+dx[j];
					if(nx<0 || w<=nx || ny<0 || h<=ny) continue;
					if(c[ny][nx] == 'x') continue;

					if(dist[ny][nx] == 0 || dist[y][x]+1 < dist[ny][nx]){
						dist[ny][nx] = dist[y][x]+1;
						que.push(make_pair(ny, nx));
					} else{
						continue;
					}
					if(c[ny][nx] == 'o' || c[ny][nx] == '*'){
						for(int k=0; k<count; k++){
							if(ny == node[k].first && nx == node[k].second){
								adj[i][k] = adj[k][i] = dist[ny][nx];
								break;
							}
						}
					}
				}
			}
		}
		for(int k=0; k<count; k++){
			adj[k][k] = INF;
		}
		
		int noFlag = false;
		for(int i=0; i<count; i++){
			for(int j=0; j<count; j++){
				if(adj[i][j] == 0) noFlag = true;
			}
		}
		if(noFlag){
			cout << -1 << endl;
			continue;
		}

		int ans = INF;
		vector<int> v(count);
		for(int i=0; i<count; i++){
			v[i] = i;
		}
		do{
			int cost = 0;
			for(int i=0; i<count-1; i++){
				cost += adj[v[i]][v[i+1]];
			}
			ans = min(ans, cost);
		} while(next_permutation(v.begin()+1, v.end()));

		cout << ans << endl;
	}
	return 0;
}