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. Suppose k = 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
//此题的坑为:摘过的食物就会变成‘.',也就是说还可以走
//方法:每走到一个新的食物时,把这里看成新的起点,visit清0,此点visit为1 
//注意:A点要首先变为'.';
 
自己样例
 
3
A..
.C#
..B
6
 
3
A.C
B#.
###
4
 
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cstring> using namespace std;
char data[][];
int ki,kj,si,sj,n;
int visit[][];
int to[][]={{,},{,-},{,},{-,}}; struct node
{
int x,y;
int step;
char a;
}; int go(int i,int j)
{
if(i>=&&i<=n&&j>=&&j<=n&&data[i][j]!='#')
return ;
return ;
} int bfs()
{
node st,ed;
queue <node> q;
st.x=ki;
st.y=kj;
st.step=;
st.a='A';
memset(visit,,sizeof(visit));
visit[ki][kj]=;
data[ki][kj]='.';
q.push(st);
while(!q.empty())
{
st=q.front();
q.pop();
if(st.x==si&&st.y==sj)
{
cout<<st.step<<endl;
return ;
}
for(int i=;i<;i++)
{
ed.x=st.x+to[i][];
ed.y=st.y+to[i][];
if(go(ed.x,ed.y)&&visit[ed.x][ed.y]==)
{
if((int)(st.a)+==(int)data[ed.x][ed.y])
{
ed.step=st.step+;
ed.a=data[ed.x][ed.y];
data[ed.x][ed.y]='.';
memset(visit,,sizeof(visit));
visit[ed.x][ed.y]=;
while(!q.empty()) q.pop();
q.push(ed);
break;
}
else if(data[ed.x][ed.y]=='.')
{
visit[ed.x][ed.y]=;
ed.step=st.step+;
ed.a=st.a;
q.push(ed);
}
}
}
}
cout<<"Impossible"<<endl;
return ;
} int main()
{
int t,k=;
cin>>t;
while(t--)
{
cin>>n;
k++;
char max='A';
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
cin>>data[i][j];
if(data[i][j]=='A')
{
ki=i;kj=j;
}
if(data[i][j]<='Z'&&data[i][j]>='A'&&data[i][j]>max)
{
si=i;sj=j;
max=data[i][j];
}
}
if(max=='A')
{
cout<<"Case "<<k<<": 0"<<endl;
continue;
}
cout<<"Case "<<k<<": ";
bfs();
}
return ;
}

Lightoj 1066 Gathering Food (bfs)的更多相关文章

  1. LightOJ——1066Gathering Food(BFS)

    1066 - Gathering Food   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB W ...

  2. LightOJ 1009 二分图染色+BFS/种类并查集

    题意:有两个阵营的人,他们互相敌对,给出互相敌对的人,问同个阵营的人最多有多少个. 思路:可以使用种类并查集写.也可以使用使用二分图染色的写法,由于给定的点并不是连续的,所以排序离散化一下,再进行BF ...

  3. lightoj 1099【dijkstra/BFS】

    题意: 求 1-N 的第二长路,一条路可以重复走 if two or more shortest paths exist, the second-shortest path is the one wh ...

  4. lightoj刷题日记

    提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...

  5. LightOJ 1012 简单bfs,水

    1.LightOJ 1012  Guilty Prince  简单bfs 2.总结:水 题意:迷宫,求有多少位置可去 #include<iostream> #include<cstr ...

  6. Lightoj 1174 - Commandos (bfs)

    题目链接: Lightoj  1174 - Commandos 题目描述: 有一军队秉承做就要做到最好的口号,准备去破坏敌人的军营.他们计划要在敌人的每一个军营里都放置一个炸弹.军营里有充足的士兵,每 ...

  7. 暑期训练狂刷系列——Lightoj 1084 - Winter bfs

    题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1084 题目大意: 有n个点在一条以零为起点的坐标轴上,每个点最多可以移动k, ...

  8. lightoj 1111 - Best Picnic Ever(dfs or bfs)

    题目链接 http://www.lightoj.com/volume_showproblem.php?problem=1111 题意:给你一个有向图再给你几个人的位置,问所有人可以在哪些点相聚. 简单 ...

  9. poj 1066 Treasure Hunt (Geometry + BFS)

    1066 -- Treasure Hunt 题意是,在一个金字塔中有一个宝藏,金字塔里面有很多的墙,要穿过墙壁才能进入到宝藏所在的地方.可是因为某些原因,只能在两个墙壁的交点连线的中点穿过墙壁.问最少 ...

随机推荐

  1. 问题记录1:The VMware Authorization Service is not running.

    问题 VMware Workstation cannot connect to the virtual machine. Make sure you have rights to run the pr ...

  2. request获取ip

    public static String getIp(HttpServletRequest request) { String ip = request.getHeader("x-forwa ...

  3. 改造vim

    1.安装Vim和Vim基本插件首先安装好Vim和Vim的基本插件.这些使用apt-get安装即可: lingd@ubuntu:~/arm$sudo apt-get install vim vim-sc ...

  4. sqlite导入后无法使用

    问题:sqlite导入后无法使用 解决方式:引入sqlite3 的libraries ,然后再在 projectName-Bridging-Header.h 中添加 #import "sql ...

  5. jquery一个简单的菜单小插件

    刚学会封装插件,先来封装一个小的菜单插件 html部分 <ul class="zong"> <li class="yiji"> < ...

  6. 5、sha1加密的一个坑

    OC语言写的sha1加密算法,在网上随手可以搜索到(如下便是),但是我不得不说有一些人不责任,没有提醒大家导入必要的系统头文件,从而导致错误 + (NSString *) sha1:(NSString ...

  7. 安装arcgis server完成,打开出现未关联错误怎么办

    在控制面板,默认程序-将文件类型或协议与程序关联-找到URL(manager右键属性)后缀名的文件双击,选择explorer即可

  8. ef code first 您没有所需权限

    在进行数据库更新操作时,update-database -force,反复提示没有改表或者没有所需权限,昨晚折腾了4个小时没有搞定,太晚了,今天Google了一下,这方面的错误,提到的都很少,在一篇文 ...

  9. Codeforces Round #346 (Div. 2) C Tanya and Toys

    C. Tanya and Toys 题目链接http://codeforces.com/contest/659/problem/C Description In Berland recently a ...

  10. SQLServer 重建索引前后对比

    在做维护项目的时,我们经常会遇到索引维护的问题,通过语句,我们就可以判断某个表的索引是否需要重建. 执行一下语句:先分析表的索引 分析表的索引建立情况:DBCC showcontig('Table') ...