LightOJ——1066Gathering Food(BFS)
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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 sizeN 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 arek letters, then it will be from the firstk alphabets. Supposek = 3, that means there will be exactly oneA, oneB 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 collectsA, thenB, thenC 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 nextN lines containsN 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 |
最近找回上学期遗留的题目做做,发现BFS果然比DFS简单。很多都是无脑拓展……,这题有一个坑点就是取过字母的地方还是可以走的,然而就看成一个点 "." 即可,因为这个WA数次……
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
#define MMINF(x) memset(x,INF,sizeof(x))
typedef long long LL;
const double PI=acos(-1.0);
const int N=13;
char pos[N][N];
int vis[N][N];
struct info
{
int x;
int y;
int step;
info(){}
info(int xx,int yy):x(xx),y(yy){}
};
info direct[4];
info operator+(info a,info b)
{
info c;
c.x=a.x+b.x;
c.y=a.y+b.y;
c.step=a.step+b.step;
return c;
}
int r,n;
char goal;
int check(info v)
{
if(v.x>=0 && v.y>=0 && v.x<n && v.y<n && (pos[v.x][v.y]=='.'||pos[v.x][v.y]==goal) && (!vis[v.x][v.y]))
return 1;
return 0;
}
int zuobiao[2][30];
int main(void)
{
int tcase,i,j,cnt;
direct[0].x=1;direct[0].y=0;direct[0].step=1;
direct[1].x=-1;direct[1].y=0;direct[1].step=1;
direct[2].x=0;direct[2].y=1;direct[2].step=1;
direct[3].x=0;direct[3].y=-1;direct[3].step=1;
scanf("%d",&tcase);
for (int q=1; q<=tcase; q++)
{
MM(pos);
MM(vis);
scanf("%d",&n);
cnt=0;
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
cin>>pos[i][j];
if(pos[i][j]>='A'&&pos[i][j]<='Z')
{
zuobiao[0][pos[i][j]-'A']=i;
zuobiao[1][pos[i][j]-'A']=j;
cnt++;
}
}
}
goal='B';
r=0;
bool done=true;
for (int w=0; w<cnt; w++)
{
MM(vis);
if(!done)
break;
done=false;
info t=info(zuobiao[0][w],zuobiao[1][w]);
t.step=0;
queue<info>Q;
Q.push(t);
vis[t.x][t.y]=1;
pos[t.x][t.y]='.';
while (!Q.empty())
{
info no=Q.front();
Q.pop();
if(pos[no.x][no.y]==goal)
{
goal++;
pos[no.x][no.y]='.';
r+=no.step;
done=true;
break;
}
for (i=0; i<4; i++)
{
info v=no+direct[i];
if(check(v))
{
Q.push(v);
vis[v.x][v.y]=1;
}
}
}
}
(goal=='A'+cnt||cnt==0)?printf("Case %d: %d\n",q,r):printf("Case %d: Impossible\n",q);
}
return 0;
}
LightOJ——1066Gathering Food(BFS)的更多相关文章
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 【BZOJ5492】[HNOI2019]校园旅行(bfs)
[HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
- 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)
一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...
- 层层递进——宽度优先搜索(BFS)
问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
随机推荐
- 当ThreadLocal碰上线程池
ThreadLocal使用 ThreadLocal可以让线程拥有本地变量,在web环境中,为了方便代码解耦,我们通常用它来保存上下文信息,然后用一个util类提供访问入口,从controller层到s ...
- POJ 3140 Contestants Division (树形DP,简单)
题意: 有n个城市,构成一棵树,每个城市有v个人,要求断开树上的一条边,使得两个连通分量中的人数之差最小.问差的绝对值.(注意本题的M是没有用的,因为所给的必定是一棵树,边数M必定是n-1) 思路: ...
- COGS 930. [河南省队2012] 找第k小的数
题目描述 看到很短的题目会让人心情愉悦,所以给出一个长度为N的序列A1,A2,A3,...,AN, 现在有M个询问,每个询问都是Ai...Aj中第k小的数等于多少. 输入格式 第一行两个正整数N,M. ...
- UWP中获取Encoding.Default
Encoding.GetEncoding(0); 即可
- softmax_loss的归一化问题
cnn网络中,网络更新一次参数是根据loss反向传播来,这个loss是一个batch_size的图像前向传播得到的loss和除以batch_size大小得到的平均loss. softmax_loss前 ...
- Docker基础内容之网络基础
网络命名空间基本原理 单机版多容器实例网络交互原理 在宿主机上面打开两张网卡eth0与eth1,打通两张网卡的链路 在test1上面启动一个veth网卡,创建一个namespace:并桥接到eth0上 ...
- off-by-one&doublefree. 看雪10月ctf2017 TSRC 第四题赛后学习
off-by-one 0x00 发现漏洞 1.off-by-one 在massage函数中,如图所示,可以修改的字节数比原内存大小多了一个字节 2.悬挂指针 可以看到,在free堆块的时候,没有清空指 ...
- PAT (Basic Level) Practise (中文)-1038. 统计同成绩学生(20)
PAT (Basic Level) Practise (中文)-1038. 统计同成绩学生(20) http://www.patest.cn/contests/pat-b-practise/10 ...
- ios runloop学习
今天突然才之间才意识到NSTimer这样的运行方式,是在多线程中实现的循环还是在主线程中去实现的呢.当然不可能是在主线程中的while那么简单,那样什么都干不了,简单看了下NSTimer是以同步方式运 ...
- C# IsNullOrEmpty与IsNullOrWhiteSpace
IsNullOrEmpty:非空非NULL判断 IsNullOrWhiteSpace:非空非NULL非空格判断 后者优于前者 if (!string.IsNullOrWhiteSpace(valueE ...