zoukankan      html  css  js  c++  java
  • hdoj 2612 Find a way【bfs+队列】

    Find a way

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


    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
    两个起点,多个终点,两个人必须到达同一个终点,开两个数组,分别存放第一个和第二个人到达终点的步数,然后在对应终点出让两人的步数相加,取最小的
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<algorithm>
    #define MAX 210
    #define INF 0x3f3f3f
    using namespace std;
    int bu1[MAX][MAX];//记录第一个人步数 
    int bu2[MAX][MAX];//记录第二个人步数 
    int p;
    char map[MAX][MAX];
    int vis[MAX][MAX];
    int n,m;
    struct node
    {
    	int x,y;
    	int step;
    };
    int MIN(int x,int y)
    {
    	return x<y?x:y;
    }
    void bfs(int x1,int y1,int p)
    {
    	memset(vis,0,sizeof(vis));
    	int j,i,ok=0;
    	int move[4][2]={0,1,0,-1,1,0,-1,0};
    	queue<node>q;
    	node beg,end;
    	beg.x=x1;
    	beg.y=y1;
    	beg.step=0;
    	q.push(beg);
    	while(!q.empty())
    	{
    		end=q.front();
    		q.pop();
    		if(map[end.x][end.y]=='@')//遇见@则表示到达终点 
    		{
    			if(p==1)
    			bu1[end.x][end.y]=end.step;
    			else
    			bu2[end.x][end.y]=end.step;
    		}
    		for(i=0;i<4;i++)
    		{
    			beg.x=end.x+move[i][0];
    			beg.y=end.y+move[i][1];
    			if(!vis[beg.x][beg.y]&&0<=beg.x&&beg.x<n&&0<=beg.y&&beg.y<m&&map[beg.x][beg.y]!='#')
    			{
    				vis[beg.x][beg.y]=1;
    				beg.step=end.step+11;
    				q.push(beg);
    			}
    		}
    	}
    }
    int main()
    {
    	int sum,j,i,t,k,x1,x2,y1,y2,min;
    	int s[11000];
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    		
    		for(i=0;i<n;i++)
    		{
    			scanf("%s",map[i]);
    		}
    		for(i=0;i<n;i++)
    		{
    			for(j=0;j<m;j++)
    			{
    				if(map[i][j]=='Y')
    				{
    					x1=i;y1=j;
    				}
    				else if(map[i][j]=='M')
    				{
    					x2=i;y2=j;
    				}
    			}
    		}
    		memset(bu1,INF,sizeof(bu1));
    	    bfs(x1,y1,1);
    	    memset(bu2,INF,sizeof(bu2));
    		bfs(x2,y2,2);
    		min=INF;
    		for(i=0;i<n;i++)
    		{
    			for(j=0;j<m;j++)
    			{
    				if(bu1[i][j]!=INF&&bu2[i][j]!=INF)
    				{
    					min=MIN(bu1[i][j]+bu2[i][j],min);//取两者步数和的最小值 
    				}
    			}
    		}
    		printf("%d
    ",min);
    	}
    	return 0;      
    }
    

      

  • 相关阅读:
    制作ubuntu容器完整步骤
    linux系统python3安装pip
    ssh连接服务器提示拒绝了密码
    ubuntu中vi编辑中按键错误
    虚拟机ubuntu连不上网
    NOIP2020退役记
    【NOIp2020游记】
    loki简单安装配置使用
    .net下com调用支持x86/x64
    nginx 504 Gateway Time-out
  • 原文地址:https://www.cnblogs.com/tonghao/p/4590972.html
Copyright © 2011-2022 走看看