zoukankan      html  css  js  c++  java
  • HDU——2612Find a way(多起点多终点BFS)

    Find a way

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 9698    Accepted Submission(s): 3165

    Problem Description
    Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
    Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest. 
    Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
     
    Input
    The input contains multiple test cases.
    Each test case include, first two integers n, m. (2<=n,m<=200). 
    Next n lines, each line included m character.
    ‘Y’ express yifenfei initial position.
    ‘M’    express Merceki initial position.
    ‘#’ forbid road;
    ‘.’ Road.
    ‘@’ KCF
     
    Output
    For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
     
    Sample Input
    4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#
     
    Sample Output
    66 88 66
     

    主要看起点和终点那种点少,以少的为起点开搜。1A水过

    #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=210;
    char pos[N][N];
    int vis[N][N];
    int n,m;
    struct info
    {
    	int x;
    	int y;
    	int time;
    	bool operator<(const info &a)
    	{
    		return time>a.time;
    	}
    };
    vector<info>kfc;
    int total[2][200][200];
    info direct[4]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}},S,T;
    inline info operator+(const info &a,const info &b)
    {
    	info c;
    	c.x=a.x+b.x;
    	c.y=a.y+b.y;
    	return c;
    }
    void init()
    {
    	MM(pos);
    	MM(vis);
    	MMINF(total);
    	kfc.clear();
    }
    inline bool check(const info &a)
    {
    	return (a.x>=0&&a.x<n&&a.y>=0&&a.y<m&&!vis[a.x][a.y]&&pos[a.x][a.y]!='#');
    }
    queue<info>Q;
    int main(void)
    {
    	int i,j,cnt;
    	while (~scanf("%d%d",&n,&m))
    	{
    		init();
    		cnt=0;
    		for (i=0; i<n; i++)
    		{
    			for (j=0; j<m; j++)
    			{
    				cin>>pos[i][j];
    				if(pos[i][j]=='Y')
    				{
    					S.x=i;
    					S.y=j;
    					S.time=0;
    				}
    				else if(pos[i][j]=='M')
    				{
    					T.x=i;
    					T.y=j;
    					T.time=0;
    				}
    				else if(pos[i][j]=='@')
    				{
    					info k;
    					k.x=i;
    					k.y=j;
    					kfc.push_back(k);
    					cnt++;
    				}
    			}
    		}
    		int c=0;
    		while (!Q.empty())
    			Q.pop();
    		Q.push(S);
    		vis[S.x][S.y]=1;
    		while (!Q.empty())
    		{
    			info now=Q.front();
    			Q.pop();
    			if(pos[now.x][now.y]=='@')
    			{
    				total[0][now.x][now.y]=now.time;
    				if(++c==cnt)
    					break;
    			}	
    			for (i=0; i<4; i++)
    			{
    				info v=now+direct[i];
    				if(check(v))
    				{
    					v.time=now.time+11;
    					vis[v.x][v.y]=1;
    					Q.push(v);
    				}				
    			}
    		}
    		while (!Q.empty())
    			Q.pop();
    		MM(vis);
    		c=0;
    		Q.push(T);
    		while (!Q.empty())
    		{
    			info now=Q.front();
    			Q.pop();
    			if(pos[now.x][now.y]=='@')
    			{
    				total[1][now.x][now.y]=now.time;
    				if(++c==cnt)
    					break;
    			}	
    			for (i=0; i<4; i++)
    			{
    				info v=now+direct[i];
    				if(check(v))
    				{
    					v.time=now.time+11;
    					vis[v.x][v.y]=1;
    					Q.push(v);
    				}				
    			}
    		}
    		int r=INF;
    		for (i=0; i<cnt; i++)
    			r=min(r,total[0][kfc[i].x][kfc[i].y]+total[1][kfc[i].x][kfc[i].y]);
    		printf("%d
    ",r);
    	}
    	return 0;
    }
  • 相关阅读:
    转载:c++内存泄露机制
    推荐一款不错的dialog小工具:artDialog
    写的一些推广方法 拿出来分享下
    struts2标签具体解释
    父亲节:再见,总有一天
    Hadoop是什么
    熊猫烟花集团完美见证异速联远程接入系统
    OpenStack Networking
    管道(Pipe)/createPipe
    百度2014校园招聘算法——给出一组数据A=[a_0, a_1, a-2, ... a_n](当中n可变),打印出该数值元素的全部组合。
  • 原文地址:https://www.cnblogs.com/Blackops/p/5766306.html
Copyright © 2011-2022 走看看