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 Python3 (judge verdict: Accepted)

#!/usr/bin/env python3
#AOJ F
from collections import deque
from itertools import permutations

dx = [1,0,-1,0]
dy = [0,1,0,-1]

def bfs(sy,sx):
    dist = [[-1]*w for _ in range(h)]#初期化
    que = deque()
    que.append((sy,sx))
    dist[sy][sx] = 0
    while que:
        y,x = que.popleft()
        for i in range(4):
            nx,ny = x+dx[i],y+dy[i]
            if 0 <= nx < w and 0 <= ny < h:
                if dist[ny][nx] < 0 and M[ny][nx] != "x":
                    dist[ny][nx] = dist[y][x] + 1#移動できるなら移動先の距離+1
                    que.append((ny,nx))

    return dist#引数をスタートにした距離のリストを返す

while 1:
    w,h = map(int,input().split())
    if w == 0 and h == 0:
        break
    M = [list(input()) for _ in range(h)]
    p = []
    for i in range(h):
        for j in range(w):
            if M[i][j] == "o":
                p.append((i,j))
                break
    for i in range(h):
        for j in range(w):
            if M[i][j] == "*":
                p.append((i,j))
    #p[0]はスタート位置,その他は汚れの位置
    n = len(p)
    dist = [[float("inf")]*n for _ in range(n)]#スタート及び各汚れ間の最短路
    f = 0
    for i in range(n):
        di = bfs(*p[i])#*listとすると、list[0],list[1],list[2],…と同値
        for j in range(n):
            dist[i][j] = di[p[j][0]][p[j][1]]#dist[i][j]はp[i]をスタートにした場合の汚れまたはスタート間の最短距離
            if dist[i][j] < 0:
                f = 1#もしたどりつけない点があったら終了
                break
    #print(dist)
    if f:
        print(-1)
        continue

    #bitDP
    po = [1<<i for i in range(11)]
    ma = po[n]#ma=11...1 + 1
    dp = [[float("inf")]*(n) for _ in range(ma)]#最後に消したところからの最短路を見る必要があるため2次元(dp[b][i] := 汚れを消した状態がbかつ最後に消した汚れが汚れiの時の最短路)
    dp[1][0] = 0#スタート位置は0
    for b in range(ma):#bは汚れを消した部分を表す
        for j in range(n):
            if b&po[j]:#汚れjが消えていたら終了
                continue
            nb = b|po[j]#新たに汚れjを削除
            for i in range(n):
                if not b&po[i]:#汚れiが消えていないなら終了
                    continue
                #汚れiを最後に消したときb,nbについて比較
                if dp[nb][j] > dp[b][i] + dist[i][j]:#iの後にjを消すほうが最短なら更新
                    dp[nb][j] = dp[b][i] + dist[i][j]
    print(min(dp[ma - 1]))#全部消したときの最短距離を出力