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)

/*********************
AOJ_1140
Cleaning Robot
*********************/
#include <iostream>
#include <queue>
#include <string>
#include <cstring>
using namespace std;

//構造体宣言
struct pos{
	int x;		//座標
	int y;		
	int dis;	//距離
};

int main(){
	int w, h;
	while(1){
		cin >> w >> h;
		if(w == 0 || h == 0) break;

		//タイルの初期化
		char tile[30][30];
		for(int i = 0; i < 30; i ++){
			for(int j = 0; j < 30; j++){
				tile[i][j] = 'x';
			}
		}
		//タイルの入力
		int stain = 0;	//汚れの数
		queue<pos> que;
		for(int i = 1; i <= h; i++){
			string input;
			cin >> input;
			for(int j = 1; j <= w; j++){
				tile[i][j] = input[j-1];
				if(tile[i][j] == '*') stain++;
				if(tile[i][j] == 'o'){
					pos p;
					p.y = i; p.x = j;
					p.dis = 0;
					que.push(p);
					tile[i][j] = '.';
				}
			}
		}

		int count = 0;
		while(stain > 0){
			char bfs[30][30];
			memcpy(bfs, tile, sizeof(bfs));
			pos p = que.front();
			bfs[p.y][p.x] = 0;
			int x1[] = {-1, 0, 1, 0}, y1[] = {0, -1, 0, 1};
			int f = 1;
			while(f && !que.empty()){
				pos p1 = que.front();
				que.pop();
				for(int i = 0; i < 4; i++){
					p = p1;
					p.x += x1[i]; p.y += y1[i];
 					if(bfs[p.y][p.x] == '.'){
						p.dis++;
						bfs[p.y][p.x] = p.dis;
						que.push(p);
					}else if(bfs[p.y][p.x] == '*'){
						count += p.dis+1;
						stain--;
						tile[p.y][p.x] == '.';
						while(!que.empty()) que.pop();
						p.dis = 0;
						que.push(p);
						f = 0;
						break;
					}
				}
			}
			if(que.empty()) break;
		}

		if(stain > 0) cout << "-1" << endl;
		else cout << count << endl;
	}

	return 0;
}