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;
    }


    编程五分钟,调试两小时...
  • 相关阅读:
    mfc crc校验工具
    MFC 配置附加目录
    多线程中如何使用gdb精确定位死锁问题
    符号冲突
    动态库之间单例模式出现多个实例(Linux)
    c++普通函数在头文件定义报重复定义的错误。而class定义不会
    static初始化顺序及延伸
    tcmalloc使用中出现崩溃问题记录
    shell脚本—判断***是否安装
    【1080TI驱动+CUDA10.1+cudnn】安装记录
  • 原文地址:https://www.cnblogs.com/kingjordan/p/12027112.html
Copyright © 2011-2022 走看看