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


    编程五分钟,调试两小时...
  • 相关阅读:
    BZOJ 3028 食物 ——生成函数
    BZOJ 1933 [Shoi2007]Bookcase 书柜的尺寸 ——动态规划
    论咸鱼的自我修养之网络流
    SPOJ LCS2 Longest Common Substring II ——后缀自动机
    SPOJ NSUBSTR Substrings ——后缀自动机
    BZOJ 1879 [Sdoi2009]Bill的挑战 ——状压DP
    BZOJ 1875 [SDOI2009]HH去散步 ——动态规划 矩阵乘法
    BZOJ 1226 [SDOI2009]学校食堂Dining ——状压DP
    BZOJ 4566 [Haoi2016]找相同字符 ——广义后缀自动机
    BZOJ 3473 字符串 ——广义后缀自动机
  • 原文地址:https://www.cnblogs.com/kingjordan/p/12027112.html
Copyright © 2011-2022 走看看