Lightoj 1066 Gathering Food (bfs)

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[12][12];
int ki,kj,si,sj,n;
int visit[12][12];
int to[4][2]={{0,1},{0,-1},{1,0},{-1,0}};

struct node
{
    int x,y;
    int step;
    char a;
};

int go(int i,int j)
{
    if(i>=1&&i<=n&&j>=1&&j<=n&&data[i][j]!='#')
    return 1;
    return 0;
}

int bfs()
{
    node st,ed;
    queue <node> q;
    st.x=ki;
    st.y=kj;
    st.step=0;
    st.a='A';
    memset(visit,0,sizeof(visit));
    visit[ki][kj]=1;
    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 0;
        }
        for(int i=0;i<4;i++)
        {
           ed.x=st.x+to[i][0];
           ed.y=st.y+to[i][1];
           if(go(ed.x,ed.y)&&visit[ed.x][ed.y]==0)
           {
                if((int)(st.a)+1==(int)data[ed.x][ed.y])
                {
                    ed.step=st.step+1;
                    ed.a=data[ed.x][ed.y];
                    data[ed.x][ed.y]='.';
                    memset(visit,0,sizeof(visit));
                    visit[ed.x][ed.y]=1;
                    while(!q.empty()) q.pop();
                    q.push(ed);
                    break;
                }
                else if(data[ed.x][ed.y]=='.')
                {
                    visit[ed.x][ed.y]=1;
                    ed.step=st.step+1;
                    ed.a=st.a;
                    q.push(ed);
                }
           }
        }
    }
    cout<<"Impossible"<<endl;
    return 0;
}

int main()
{
    int t,k=0;
    cin>>t;
    while(t--)
    {
        cin>>n;
        k++;
        char max='A';
        for(int i=1;i<=n;i++)
         for(int j=1;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 0;
}

 

posted @ 2016-08-10 14:52  邻家那小孩儿  阅读(233)  评论(0编辑  收藏  举报