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 <vector>
#include <cmath>
#include <queue>
using namespace std;

char map[21][21];
int path[12][12];
int dp[4500];

struct Point {
	int x, y;
	Point() : x(0), y(0) {}
	Point(int x, int y) : x(x), y(y) {}
};
Point dirtyTiles[11]; //dirtyTiles[0]ツづ債δ債ボツッツトツ渉可甘コツ暗環置

bool visited[21][21];

int w = 0;
int h = 0;

int getShortestPathLength(Point from, Point to) {
	//memset(visited, 0, sizeof(visited));
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			visited[i][j] = map[i][j] == 'x';
		}
	}

	queue<Point> que;
	que.push(from);

	int length = -1;

	while(!que.empty()) {
		int curLength = que.size();
		length++;
		for (int i = 0; i < curLength; i++) {
			Point point = que.front();
			que.pop();

			if (visited[point.y][point.x]) {
				continue;
			}
			visited[point.y][point.x] = true;
			if(point.x == to.x && point.y == to.y) {
				//cout << "ret:" << length << endl;
				return length;
			}
			if(point.x > 0) {
				Point left(point.x-1, point.y);
				que.push(left);
			}
			if(point.x < w-1) {
				Point right(point.x+1, point.y);
				que.push(right);
			}
			if(point.y > 0) {
				Point up(point.x, point.y-1);
				que.push(up);
			}
			if(point.y < h-1) {
				Point down(point.x, point.y+1);
				que.push(down);
			}
		}
	}
	//cout << "ret:-1" << endl;
	return -1;
}

int main() {
	while(cin >> w >> h, w || h) {

		int dirtyTileSize = 1;

		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				char c;
				cin >> c;
				map[i][j] = c;
				if(c == 'o') {
					dirtyTiles[0].x = j; dirtyTiles[0].y = i;
				}
				else if(c == '*') {
					dirtyTiles[dirtyTileSize].x = j;
					dirtyTiles[dirtyTileSize++].y = i;
				}
			}
		}

//		for (int i = 0; i < dirtyTileSize; i++) {
//			for (int j = 0; j < dirtyTileSize; j++) {
//				path[i][j] = 0;
//			}
//		}

		for (int i = 0; i < dirtyTileSize; i++) {
			for (int j = i + 1; j < dirtyTileSize; j++) {
				path[i][j] = path[j][i] = getShortestPathLength(dirtyTiles[i], dirtyTiles[j]);
				if(path[i][j] == -1)
					goto unreachable;
			}
			path[i][i] = 0;
		}

		goto reachable;
		unreachable: {
			cout << -1 << endl;
			continue;
		}
		reachable:

		//memset(dp, 0, sizeof(dp));

		int bitMax = 1 << dirtyTileSize;
		int INF = 10000000;
		for (int i = 0; i < bitMax; i++) {
			dp[i] = INF;
		}

		for (int i = 1; i < dirtyTileSize; i++) {
			dp[1 << i] = path[0][i];
		}
		for (int i = 1; i < dirtyTileSize; i++) {
			for (int j = 1; j < dirtyTileSize; j++) {
				if(path[i][j] == 0) continue;
				for (int k = 0; k < bitMax; k++) {
					if((k & (1 << i)) > 0 && (k & (1 << j)) == 0) {
						if(dp[k] != INF && (dp[k | (1 << j)] == INF || dp[k] + path[i][j] < dp[k | (1 << j)])) {
							dp[k | (1 << j)] = dp[k] + path[i][j];
						}
					}
				}
			}
		}

		cout << dp[(1 << dirtyTileSize) - 2] << endl;
		continue;

	}
	return 0;
}