light oj 1066Gathering Food (bfs 稍微有点小坑)

1066 - Gathering Food
 

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

Output for Sample Input

4

 

5

A....

####.

..B..

.####

C.DE.

 

2

AC

.B

 

2

A#

#B

 

3

A.C

##.

B..

Case 1: 15

Case 2: 3

Case 3: Impossible

Case 4: Impossible

 

 

 

 

 

题目大意:其实题意挺好理解的,就是去摘果,然后需要从A点开始,然后摘到字符出现的最后一个字母。地图是n*n 然后字母一定是从A 开始顺序递增,而且不会超过26个。有个小坑点,就是摘过这个字母的时候,这条路是可以走的,比如你过了B这个点,那么B这个点就相当于变成'.'。  知道这个,,每次到达一个字母,就设为起始点,bfs 之后初始化 ,再BFs  知道走到最后一个字母,这样就OK了。


#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
char map[20][20];           //记录地图
int vis[20][20];            //访问标记
int dir[4][2] = {0,1,0,-1,1,0,-1,0};        //四个方向
int n;                         //行和列
int ans;                    //记录走过的字母个数
struct state
{
    int x,y;            //记录坐标
    int count;             //记录步数
    char c;             //字符
}sta[600];

int res = 0;            //记录最后的答案
int check(state s, char c)
{
    if ( vis[s.x][s.y] == 0 && s.x >= 0 &&s.x < n && s.y >= 0 && s.y < n && (s.c =='.'||(s.c <= c && s.c >= 'A' && s.c <= 'Z')))    //判断可以不可以走
    {
        return 1;
    }
    else 
    {
        return 0;
    }
}

void bfs(state s,char c)            //bfs算法
{
    queue <state> q;            //定义队列
    state now,next;             //现在的 和下一个
    s.count = 0;                //初始化步数
    q.push(s);                  //入队
    vis[s.x][s.y] = 1;
    while (!q.empty())
    {
        now = q.front();           //队首元素
        q.pop();                    //出队
        if (now.c == c )            //如果走到目标字母
        {
            ans++;
                res += now.count;       //结果加上走的步数
            map[now.x][now.y] = '.';        //然后走过的字母变为'.'
            now.count = 0;          
            next.count = 0;
            memset(vis,0,sizeof(vis));          //初始化走过的路
            return ;
        }
        for ( int i = 0 ; i < 4 ;i++)
        {
            next.x = now.x + dir[i][0];
            next.y = now.y + dir[i][1];
            next.count = now.count + 1;         //四个方向
            next.c = map[next.x][next.y];
            if (check(next , c))
            {
                q.push(next);                   //如果符合条件,入队
                vis[next.x][next.y] = 1;        //访问过了 不再访问
            }
        }
        
    }
    return;                 //如果无路可走 退出
}

int main()
{
    int t;
    scanf("%d",&t);
    int temp[26];
    for (int cas = 1 ; cas <= t ; cas ++ )
    {
        memset(vis,0,sizeof(vis));
        res = 0;
        ans = 1;
        scanf("%d",&n);
        getchar();
        int k = 0;
        int l = 0;
        for (int  i = 0 ; i < n ; i++ )
        {
            for ( int j = 0 ; j < n ; j++ )
            {
                scanf("%c",&sta[l].c);
                map[i][j] = sta[l].c;       //结构数组的字符
                sta[l].x = i;
                sta[l].y = j;
                if (sta[l].c >= 'A' && sta[l].c <= 'Z')
                {
                    temp[sta[l].c-'A'] = l;
                    k++;            //字母总个数
                }
                l++;                //结构数组的下标
            }
            getchar();
        }
        for (int  i = 1 ; i < k ; i++ )
        {
            //printf("%c\n",sta[temp[i-1]].c);
            bfs(sta[temp[i-1]],('A' + i));          //记录A 到B的路径  然后再B到C 的路径,直到最后一个
            if ( ans < i )
                break;
        }
        if (ans == k && k > 0)                  //如果字母全部走过了
        {
            printf("Case %d: %d\n",cas, res);
        }
        else 
        {
            printf("Case %d: Impossible\n",cas);        //没有走遍所有的字母
        }
        if ( cas != t)
            getchar();
    }
    return 0;   
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值