• 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;
    }
  • 相关阅读:
    Java虚拟机的内存模型
    JAVA 对文件的操作
    JAVA 读取 YAML 文件
    Nginx 502 问题解决 及 安装
    Python pdb 调试 命令
    pycharm设置鼠标控制字体大小
    ISO9126 软件质量模型
    人生苦短我学Java9面向对象三大特性之多态 广深
    Golang微服务入门到精通之路3类的封装/继承/多态/接口类型 广深
    人生苦短我学Java10final关键字/代码块/抽象类 广深
  • 原文地址:https://www.cnblogs.com/Blackops/p/5766306.html
Copyright © 2020-2023  润新知