zoukankan      html  css  js  c++  java
  • LightOJ——1066Gathering Food(BFS)

    1066 - Gathering Food
    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
    ",q,r):printf("Case %d: Impossible
    ",q);	
    	}
    	return 0;
    }
  • 相关阅读:
    列"xx"不在表Table中
    asp.net中自定义验证控件
    ASP.NET母版与内容页相对路径的问题
    html点小图看大图最快捷的方法
    ThinkCMF的跳转303 404等页面的方法
    关于ThinkCMF自带插件上传不了图片的解决方法
    js中百分比运算,大型数据会算错
    数据库价格汇总查询的方法
    信息资源5
    操作系统概论
  • 原文地址:https://www.cnblogs.com/Blackops/p/5766311.html
Copyright © 2011-2022 走看看