111qqz的小窝

老年咸鱼冲锋!

light oj 1066- Gathering Food (bfs)

H – Gathering Food

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Submit Status Practice LightOJ 1066
Appoint description: 

Description

Winter is approaching! The weather is getting colder and days are becoming shorter. The animals take different measures to adjust themselves during this season.

– Some of them “migrate.” This means they travel to other places where the weather is warmer.

– Few animals remain and stay active in the winter.

– Some animals “hibernate” for all of the winter. This is a very deep sleep. The animal’s body temperature drops, and its heartbeat and breathing slow down. In the fall, these animals get ready for winter by eating extra food and storing it as body fat.

For this problem, we are interested in the 3rd example and we will be focusing on ‘Yogi Bear’.

Yogi Bear is in the middle of some forest. The forest can be modeled as a square grid of size N x N. Each cell of the grid consists of one of the following.

.           an empty space

#         an obstacle

[A-Z]  an English alphabet

There will be at least 1 alphabet and all the letters in the grid will be distinct. If there are k letters, then it will be from the first k alphabets. Supposek = 3, that means there will be exactly one A, one B and one C.

The letters actually represent foods lying on the ground. Yogi starts from position ‘A’ and sets off with a basket in the hope of collecting all other foods. Yogi can move to a cell if it shares an edge with the current one. For some superstitious reason, Yogi decides to collect all the foods in order. That is, he first collects A, then B, then C and so on until he reaches the food with the highest alphabet value. Another philosophy he follows is that if he lands on a particular food he must collect it.

Help Yogi to collect all the foods in minimum number of moves so that he can have a long sleep in the winter.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case contains a blank line and an integer N (0 < N < 11), the size of the grid. Each of the next N lines contains N characters each.

Output

For each case, output the case number first. If it’s impossible to collect all the foods, output ‘Impossible’. Otherwise, print the shortest distance.

Sample Input

4

 

5

A….

####.

..B..

.####

C.DE.

 

2

AC

.B

 

2

A#

#B

 

3

A.C

##.

B..

Sample Output

Case 1: 15

Case 2: 3

Case 3: Impossible

Case 4: Impossible

 

bfs

有从A开始的k种食物,每种食物只有一份。

要求按照顺序吃,而且到达一个网格如果有食物必须吃掉,问能否按照顺序吃完,如果能,输出最短步数。

一开始只做了一遍 bfs,找到b之后找c,但是可能的情况是一开始我先遇到c,但是这个时候我还没有吃到b,等吃到b以后,c附近的路径已经做了标记不能被拓展到了。

取消标记? 当然不。。。会死循环。

每吃到一个食物取消全部标记? 

sounds good,但是我是直接用d数组判断是否访问过

取消标记的话会把距离也取消掉。

可以再开个布尔数组用来标记,d数组就专心记录距离,或者每次遇到食物把d数组的值累加到全局变量上也行。

终止条件是,找到最后一个字母。

我就不说因为把memset(d,-1,sizeof(d))写成memset(d,-1,sizeof(-1))wa 到死这种傻逼事了。。。。。

 

 

说点什么

您将是第一位评论人!

提醒
wpDiscuz
粤ICP备18103363