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++11 (judge verdict: Wrong Answer)

#include <queue>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

#define INF (1 << 29)

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

int main()
{
	while (true)
	{
		// ------ Variable, Input ------ //

		int H, W; vector<string> MAP;

		cin >> W >> H;

		if (W == 0 && H == 0) { break; }

		MAP = vector<string>(H);

		for (int i = 0; i < H; i++)
		{
			cin >> MAP[i];
		}


		// ------ Graph Construction ------ //

		vector<pair<int, int> > Location(1);

		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				if (MAP[i][j] == 'o')
				{
					Location[0] = make_pair(j, i);
				}

				if (MAP[i][j] == '*')
				{
					Location.push_back(make_pair(j, i));
				}
			}
		}

		vector<vector<int> > Graph(Location.size(), vector<int>(Location.size(), INF));

		for (int i = 0; i < Location.size(); i++)
		{
			vector<vector<int> > distance_(H, vector<int>(W, INF)); distance_[Location[i].second][Location[i].first] = 0;

			queue<pair<int, int> > que; que.push(Location[i]);

			while (!que.empty())
			{
				pair<int, int> p1 = que.front(); que.pop();

				for (int dir = 0; dir < 4; dir++)
				{
					int px = p1.first + dx[dir];
					int py = p1.second + dy[dir];

					if (0 <= px && px < W && 0 <= py && py < H && MAP[py][px] != 'x' && distance_[py][px] == INF)
					{
						distance_[py][px] = distance_[p1.second][p1.first] + 1;

						que.push(make_pair(px, py));
					}
				}
			}

			for (int j = 0; j < Location.size(); j++)
			{
				if (i != j)
				{
					int dist = distance_[Location[j].second][Location[j].first];

					Graph[i][j] = dist;
				}
			}
		}

		// ------ Traveling Salesman Problem ------ //

		vector<int> dp(1 << Graph.size(), INF);

		dp[1] = 0;

		for (int i = 2; i < (1 << Graph.size()); i++)
		{
			for (int j = 0; j < Graph.size(); j++)
			{
				for (int k = 0; k < Graph.size(); k++)
				{
					if (i & (1 << j) && i & (1 << k))
					{
						dp[i] = min(dp[i - (1 << j)] + Graph[j][k], dp[i]);
					}
				}
			}
		}

		cout << dp[(1 << Graph.size()) - 1] << endl;
	}

	return 0;
}