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: 
System Crawler  (2015-08-11)

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 到死这种傻逼事了。。。。。

/*************************************************************************
    > File Name: code/loj/1066.cpp
    > Author: 111qqz
    > Email: [email protected]
    > Created Time: 2015年08月11日 星期二 18时30分39秒
 ************************************************************************/

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#define y0 abc111qqz
#define y1 hust111qqz
#define yn hez111qqz
#define j1 cute111qqz
#define tm crazy111qqz
#define lr dying111qqz
using namespace std;
#define REP(i, n) for (int i=0;i<int(n);++i)
typedef long long LL;
typedef unsigned long long ULL;
const int inf = 0x7fffffff;
const int N=13;
int sx,sy;
int n;
char maze[N][N];
int d[N][N];
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
int ans;
int seek[N][N];
int cnt;
bool v[N][N];

int bfs(int xx,int yy,char ch)
{
    memset(d,-1,sizeof(-1));
    queue<int>x;
    queue<int>y;
    x.push(xx);
    y.push(yy);
    d[xx][yy] = 0;
    maze[xx][yy]=‘.‘;
    while (!x.empty())
    {

    int px = x.front();x.pop();
    int py = y.front();y.pop();
//    cout<<"px:"<<px<<" py:"<<py<<endl;
    if (maze[px][py]==ch)
    {
        sx = px;
        sy = py;
        return d[px][py];
    }
    for ( int i = 0 ; i< 4 ; i++ )
    {
        int nx = px + dx[i];
        int ny = py + dy[i];
        if (nx>=0&&nx<n&&ny>=0&&ny<n&&d[nx][ny]==-1&&(maze[nx][ny]==‘.‘||maze[nx][ny]==ch))
        {

        d[nx][ny] =  d[px][py] + 1;
        x.push(nx);
        y.push(ny);

        }
    }
    }
    return -1;
}
int main()
{
    int T;
    cin>>T;
    int cas = 0 ;
    while ( T-- )
    {
    cas++;
    ans = 0;
    scanf("%d",&n);
    for ( int i = 0 ; i  < n ; i++ )
    {
        scanf("%s",maze[i]);
    }
     cnt = 0 ;// 统计食物的数量
    for ( int i = 0 ; i < n ; i++ )
    {
        for ( int j = 0 ; j < n ; j ++)
        {
        if (maze[i][j]==‘A‘)
        {
            sx = i ;
            sy = j;
        }
        if (maze[i][j]!=‘#‘&&maze[i][j]!=‘.‘)
        {
            cnt++;
        }
        }
    }
    memset(d,-1,sizeof(d));
    d[sx][sy]  = 0;
    bool flag = false;
    for ( int i = 0 ; i < cnt ; i++ )
    {
        int tmp = bfs(sx,sy,char(i+‘A‘));
     //  cout<<"tmp:"<<tmp<<endl;
        if (tmp==-1)
        {
        flag = true;
        break;
        }
        ans += tmp;
//        cout<<"tmp:"<<tmp<<" ans:"<<ans<<endl;

    }
//    for ( int i = 0 ; i < n ; i++ )
//    {
//        for ( int j = 0 ; j < n ; j++ )
//        {
//        cout<<d[i][j]<<" ";
//        }
//        cout<<endl;
//    }
    if (flag)
    {

        printf("Case %d: %d\n", cas, ans);
    }
    else
    {

            printf("Case %d: Impossible\n", cas);
    }
    }

    return 0;
} 
时间: 2024-10-13 05:36:52

light oj 1066- Gathering Food (bfs)的相关文章

Light OJ 1141--BFS--(隐蔽的BFS)

题意:求数字A变换到B 的最小步数.变换方法是每次加A的素因数 分析:BFS 代码: #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; int s,t,n,vis[1009],prim[1009],fac[1009]; struct h{ int x,tot; }; queue<h> q; int cnt; void

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 plac

Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖

题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走完全图 可以重复走 有向图 思路:如果是DAG图并且每个点不能重复走 那么就是裸的最小路径覆盖 现在不是DAG 可能有环 并且每个点可能重复走 对于有环 可以缩点 缩点之后的图是DAG图 另外点可以重复走和POJ 2594一样 先预处理连通性 #include <cstdio> #include <cstring> #include <vector> #include &

light oj 1236 【大数分解】

给定一个大数,分解质因数,每个质因子的个数为e1,e2,e3,--em, 则结果为((1+2*e1)*(1+2*e2)--(1+2*em)+1)/2. //light oj 1236 大数分解素因子 #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <math.h> #include <ctype.h> #i

[2016-04-21][light]OJ[1234][Harmonic Number]

时间:2016-04-21 22:18:26 星期四 题目编号:[2016-04-21][light]OJ[1234][Harmonic Number] 题目大意:求∑nk=11kn∈(1,108),精确到10?8求∑k=1n1kn∈(1,108),精确到10?8 分析: 想法是打表,然后输出,但是直接打表会爆内存 解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值 对应的整百就是n

Light OJ 1411 Rip Van Winkle`s Code 线段树成段更新

题目来源:Light OJ 1411 Rip Van Winkle`s Code 题意:3中操作 1种查询 求区间和 其中每次可以把一段区间从左到右加上1,2,3,...或者从右到左加上...3,2,1 或者把某个区间的数都置为v 思路:我是加了6个域 add是这段区间每个数都要加上add  add是这么来的 对与123456...这个等差数列 可能要分为2个区间 那么我就分成123和123 两个右边的等差数列每个数还应该加上3 所以右区间add加3 v是这个区间都要置为v 他的优先级最高 b是

Light OJ 1168 Wishing Snake 强连通缩点+哈密顿通路

题目来源:Light OJ 1168 Wishing Snake 题意:有点难看懂题意 看了一个小时再加别人的代码才懂意思 从0开始 输入的那些每一对u v 都要经过 就是从0到到达那些点 思路:首先缩点 每一个强连通分量里面的点都是可达的 缩点后的图是有向无环图 如果从0这个强连通分量可以出去到2个强连通分量 那么这两个强连通分量是不可能相互可达 所以可行的方案就是所有的强连通分量连成一线 一个一个串起来 除了第一个 出度是1入度是0和最后一个出度是0入度是1 其他都是入度等于出度是1 特判只

Jan&#39;s light oj 01--二分搜索篇

碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计数问题,利用二分直接枚举可能的结果,再检查是否符合题目要求. 4.区间求解,即利用两次二分分别查找有序序列左右上下限,再求差算出总个数. 题型知识补充: 1. 三分的一般写法: 1 double thfind(double left,double right) 2 { 3 double midmid

light oj 1422 - Halloween Costumes (区间dp)

1422 - Halloween Costumes PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is planning to attend as many parties as he can. Since it's Ha

Light OJ 1341 Aladdin and the Flying Carpet

It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery. Aladdin was about to enter to a magical cave, led by the evil sorcerer who disguised hi