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
 
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <queue>
  5. #include <cstring>
  6.  
  7. using namespace std;
  8. char data[][];
  9. int ki,kj,si,sj,n;
  10. int visit[][];
  11. int to[][]={{,},{,-},{,},{-,}};
  12.  
  13. struct node
  14. {
  15. int x,y;
  16. int step;
  17. char a;
  18. };
  19.  
  20. int go(int i,int j)
  21. {
  22. if(i>=&&i<=n&&j>=&&j<=n&&data[i][j]!='#')
  23. return ;
  24. return ;
  25. }
  26.  
  27. int bfs()
  28. {
  29. node st,ed;
  30. queue <node> q;
  31. st.x=ki;
  32. st.y=kj;
  33. st.step=;
  34. st.a='A';
  35. memset(visit,,sizeof(visit));
  36. visit[ki][kj]=;
  37. data[ki][kj]='.';
  38. q.push(st);
  39. while(!q.empty())
  40. {
  41. st=q.front();
  42. q.pop();
  43. if(st.x==si&&st.y==sj)
  44. {
  45. cout<<st.step<<endl;
  46. return ;
  47. }
  48. for(int i=;i<;i++)
  49. {
  50. ed.x=st.x+to[i][];
  51. ed.y=st.y+to[i][];
  52. if(go(ed.x,ed.y)&&visit[ed.x][ed.y]==)
  53. {
  54. if((int)(st.a)+==(int)data[ed.x][ed.y])
  55. {
  56. ed.step=st.step+;
  57. ed.a=data[ed.x][ed.y];
  58. data[ed.x][ed.y]='.';
  59. memset(visit,,sizeof(visit));
  60. visit[ed.x][ed.y]=;
  61. while(!q.empty()) q.pop();
  62. q.push(ed);
  63. break;
  64. }
  65. else if(data[ed.x][ed.y]=='.')
  66. {
  67. visit[ed.x][ed.y]=;
  68. ed.step=st.step+;
  69. ed.a=st.a;
  70. q.push(ed);
  71. }
  72. }
  73. }
  74. }
  75. cout<<"Impossible"<<endl;
  76. return ;
  77. }
  78.  
  79. int main()
  80. {
  81. int t,k=;
  82. cin>>t;
  83. while(t--)
  84. {
  85. cin>>n;
  86. k++;
  87. char max='A';
  88. for(int i=;i<=n;i++)
  89. for(int j=;j<=n;j++)
  90. {
  91. cin>>data[i][j];
  92. if(data[i][j]=='A')
  93. {
  94. ki=i;kj=j;
  95. }
  96. if(data[i][j]<='Z'&&data[i][j]>='A'&&data[i][j]>max)
  97. {
  98. si=i;sj=j;
  99. max=data[i][j];
  100. }
  101. }
  102. if(max=='A')
  103. {
  104. cout<<"Case "<<k<<": 0"<<endl;
  105. continue;
  106. }
  107. cout<<"Case "<<k<<": ";
  108. bfs();
  109. }
  110. return ;
  111. }

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. Shell终端收听音乐--网易云音乐命令行版

    Musicbox:网易云音乐命令行版本 高品质网易云音乐命令行版本,简洁优雅,丝般顺滑,基于Python编写. 这款命令行的客户端使用 Python 构建,以 mpg123 作为播放后端: Vim 式 ...

  2. ios开发之简单实现loading动画效果

    最近有朋友问我类似微信语音播放的喇叭动画和界面图片加载loading界面是怎样实现的,是不是就是一个gif图片呢!我的回答当然是否定了,当然不排除也有人用gif图片啊!下面我就来罗列三种实现loadi ...

  3. OpenGL杂七杂八

    Projection Matrix 投影矩阵 3D -> 2D PFD_DOUBLEBUFFER 双缓冲 在图形图象处理编程过程中,双缓冲是一种基本的技术.我们知道,如果窗体在响应WM_PAIN ...

  4. #数论-模运算#POJ 1150、1284、2115

    1.POJ 1150 The Last Non-zero Digit #质因数分解+模运算分治# 先贴两份题解: http://www.hankcs.com/program/algorithm/poj ...

  5. Apache Storm简介

    Apache Storm简介 Storm是一个分布式的,可靠的,容错的数据流处理系统.Storm集群的输入流由一个被称作spout的组件管理,spout把数据传递给bolt, bolt要么把数据保存到 ...

  6. php获取url字符串截取路径的文件名和扩展名

    <?php //获取连接里边的id $url = 'http://www.rong123.com/cjbkscbsd/x_dfsdfs/24454_1_1.html'; function get ...

  7. git出错

    查询git的版本是否装对  32 位  64位

  8. spring mvc 实现文件上传下载

    /** * 文件上传 * @param pictureFile */ @RequestMapping("/reportupload") public ResponseInfo up ...

  9. 从P1到P7——我在淘宝这7年(转)

    作者: 赵超  发布时间: 2012-02-25 14:47  阅读: 114607 次  推荐: 153   [收藏] (一) 2011-12-08 [原文链接] 今天有同事恭喜我,我才知道自己在淘 ...

  10. .net导出不规则Excel

    using Hamp.App.BLL; using Hamp.App.Model; using Hamp.App.Model.QualityManagement; using System; usin ...