zoukankan      html  css  js  c++  java
  • Find a way

    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<cstdio>
    #include<cstring>
    #include<queue>
    #include<algorithm>
    #define  INF  0x3f3f3f3f
    using namespace std;
    char map[1001][1001];
    int vis[1001][1001];
    	
    int n,m;
    int dx[4]={0,1,-1,0};
    int dy[4]={1,0,0,-1};
    int dis[2][1001][1001];
    struct node
    {
    	int hang ,lie;
    	int step;
    };
    void bfs(int h,int x,int y)
    {
    		memset(vis,0,sizeof(vis));
    		struct   node start;
    		vis[x][y]=1;
    		start.hang =x;
    		start.lie =y;
    		start.step =0;
    		queue<node>q;
    		q.push(start);
    		while(!q.empty() )
    		{
    			struct node tmp=q.front() ;
    			q.pop() ;
    			if(map[tmp.hang ][tmp.lie ]=='@')
    			{
    				dis[h][tmp.hang ][tmp.lie ]=tmp.step ;
    			}
    			struct node tmp2;
    			  for(int i=0;i<4;i++)
    			  {
    			  	tmp2.hang =tmp.hang +dx[i];
    			  	tmp2.lie =tmp.lie +dy[i];
    			  	if(tmp2.hang >=0&&tmp2.lie >=0&&tmp2.lie <m&&tmp2.hang <n&&vis[tmp2.hang ][tmp2.lie ]!=1&&map[tmp2.hang ][tmp2.lie ]!='#')
    			  	{
    			  		vis[tmp2.hang ][tmp2.lie ]=1;
    			  		tmp2.step =tmp.step +1;
    			  		q.push(tmp2); 
    				  }
    			  }
    		 } 
    }
    
    int main()
    {
             int x1,x2,y1,y2;
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    		    memset(dis,INF,sizeof(dis));
    		   memset(map,0,sizeof(map));
    			for(int i=0;i<n;i++)
    	    {
    		   scanf("%s",map[i]);
    		   for(int j=0;j<m;j++)
    		   {
    		   	if(map[i][j]=='M')
    		   	{
    		   			x1=i,y1=j;
    			   }
    		   	if(map[i][j]=='Y')
    		   	{
    		   			x2=i,y2=j;
    			   }
    		   }
    	     }
    	     int sum=INF;
    	     bfs(0,x1,y1);
    	     bfs(1,x2,y2);
    	      for(int i=0;i<n;i++)
    	      {
    	      	for(int j=0;j<m;j++)
    	      	{
    			   if(map[i][j]=='@')
    			   {
    			   	sum=min(sum,dis[0][i][j]+dis[1][i][j]);
    			   }
    			  }
    		  }
    		  printf("%d
    ",sum*11);
    	}
    	return 0;
    }


    编程五分钟,调试两小时...
  • 相关阅读:
    day03
    day02
    day01
    springBoot相关(二)
    predis操作redis方法大全
    按钮变色变色变色
    mysql中获取一天、一周、一月时间数据的各种sql语句写
    wordpress速度慢
    html关于强制显示、隐藏浏览器的滚动条
    css全局样式表
  • 原文地址:https://www.cnblogs.com/kingjordan/p/12027112.html
Copyright © 2011-2022 走看看