2640Tomb raiders

2640   Tomb raiders

题目描述

Fry is the charge of a tomb raiders family. One day he found a ancient tomb.

The tomb is a n*n matrix,Fry had already known the treasure's position.But when he step into the tomb, he found something was wrong. There is some monsters in the tomb. In normal, Fry cost 1 hp to move to the nearby place, and as the charge of a tomb raiders, if there is just one monster in the tomb, Fry could ignore it.But if there is more than one monster in the tomb,they will add the cost of any move to be the same to the number of monster. Fry can kill the monster if he move to the same grid of one master. Besides, there is obstacles in the tomb, which means Fry could not step into this grid.

Now Fry is at (1,1) of matrix, what is the minimum hp Fry cost to get the treasure?

输入格式:

There is a T at the begining of file, show that there is T case follow.

For every case, first line is a integer n, mean the size of the tomb, from 2nd to the (n+1)th line, each line contain n char, mean the whole tomb.

'.'for empty space ,'M' for monster, 'B' for obstacles, and 'D' for treasure.

2<=n<=10, and there is no more than 4 moster in the tomb.

You can assure that (1,1) is empty space.

输出格式:

For every case, first output"Case #x: ",x mean the number of case.

If Fry can get the treasure, output the least hp he will cost, otherwise output"Impossible".

See sample for detail.

输入样例 复制
3
2
.M
MD
2
.B
BD
6
.MMM..
......
......
......
......
D.....
输出样例 复制
Case #1: 3
Case #2: Impossible
Case #3: 12

说明

1
12
通过提交
时空限制1000ms/32mb
题目来源数据结构实验与竞赛训练
评测方式在线评测
题目类型
难        度