zoukankan      html  css  js  c++  java
  • hdu 5402 Travelling Salesman Problem

    题意:从一个方格的左上角走到右下角,拿起经过的全部数字,且每一个方格最多仅仅能走一次,问,终于到达右下角时,sum最大是多少。


    做法:……非常显然构造了


    首先假设nn为奇数或者mm为奇数,那么显然能够遍历整个棋盘。


    如果n,mn,m都为偶数,那么将棋盘黑白染色,如果(1,1)(1,1)和(n,m)(n,m)都为黑色,那么这条路径中黑格个数比白格个数多11。而棋盘中黑白格子个数同样。所以必定有一个白格不会被经过,所以选择白格中权值最小的不经过。


    构造方法是这样。首先RRRRDLLLLD这种路径走到这个格子所在行或者上一行,然后DRUR这样走到这个格子的所在列或者前一列,然后绕过这个格子。然后走完这两行。接着按LLLLDRRRR这种路径往下走。



    通过YY可知绕的时候事实上就两种情况,于是就能够写了……


    #include<map>
    #include<string>
    #include<cstring>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<queue>
    #include<vector>
    #include<iostream>
    #include<algorithm>
    #include<bitset>
    #include<climits>
    #include<list>
    #include<iomanip>
    #include<stack>
    #include<set>
    using namespace std;
    void work1(int n,int m)
    {
    	for(int i=0;i<n;i++)
    	{
    		if(i)
    			putchar('D');
    		for(int j=0;j<m;j++)
    			if(i&1)
    				putchar('L');
    			else
    				putchar('R');
    	}
    }
    void work2(int n,int m)
    {
    	for(int i=0;i<m;i++)
    	{
    		if(i)
    			putchar('R');
    		for(int j=0;j<n;j++)
    			if(i&1)
    				putchar('U');
    			else
    				putchar('D');
    	}
    }
    void work3(int n,int m,int r,int c)
    {
    	int len=r/2;
    	for(int i=0;i<len;i++)
    	{
    		for(int j=0;j<m-1;j++)
    			putchar('R');
    		putchar('D');
    		for(int j=0;j<m-1;j++)
    			putchar('L');
    		putchar('D');
    	}
    	len=c/2;
    	for(int i=0;i<len;i++)
    		printf("DRUR");
    	if(r&1)
    		printf("RD");
    	else
    		printf("DR");
    	len=m/2;
    	for(int i=c/2+1;i<len;i++)
    		printf("RURD");
    	len=n/2;
    	for(int i=r/2+1;i<len;i++)
    	{
    		putchar('D');
    		for(int j=0;j<m-1;j++)
    			putchar('L');
    		putchar('D');
    		for(int j=0;j<m-1;j++)
    			putchar('R');
    	}
    }
    int main()
    {
    	int n,m;
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    		int mn=INT_MAX,r,c,sum=0;
    		for(int i=0;i<n;i++)
    			for(int j=0;j<m;j++)
    			{
    				int t;
    				scanf("%d",&t);
    				sum+=t;
    				if((i+j&1)&&mn>t)
    				{
    					mn=t;
    					r=i;
    					c=j;
    				}
    			}
    		if(n&1)
    		{
    			printf("%d
    ",sum);
    			work1(n,m-1);
    		}
    		else if(m&1)
    		{
    			printf("%d
    ",sum);
    			work2(n-1,m);
    		}
    		else
    		{
    			printf("%d
    ",sum-mn);
    			work3(n,m,r,c);
    		}
    		puts("");
    	}
    }

    Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 749    Accepted Submission(s): 273
    Special Judge


    Problem Description
    Teacher Mai is in a maze with n rows and m columns. There is a non-negative number in each cell. Teacher Mai wants to walk from the top left corner (1,1) to the bottom right corner (n,m). He can choose one direction and walk to this adjacent cell. However, he can't go out of the maze, and he can't visit a cell more than once.

    Teacher Mai wants to maximize the sum of numbers in his path. And you need to print this path.
     

    Input
    There are multiple test cases.

    For each test case, the first line contains two numbers n,m(1n,m100,nm2).

    In following n lines, each line contains m numbers. The j-th number in the i-th line means the number in the cell (i,j). Every number in the cell is not more than 104.
     

    Output
    For each test case, in the first line, you should print the maximum sum.

    In the next line you should print a string consisting of "L","R","U" and "D", which represents the path you find. If you are in the cell (x,y), "L" means you walk to cell (x,y1), "R" means you walk to cell (x,y+1), "U" means you walk to cell (x1,y), "D" means you walk to cell (x+1,y).
     

    Sample Input
    3 3 2 3 3 3 3 3 3 3 2
     

    Sample Output
    25 RRDLLDRR
     

    Author
    xudyh
     

    Source

  • 相关阅读:
    php怎么实现多态?
    php怎么识别真实ip
    php析构函数什么时候调用?
    php解析xml的几种方式
    thinkPHP5框架路由常用知识点汇总
    用Python打造了一个渗透测试暴力探测器
    修复wecenter移动版description首页描述一样问题
    寒假小软件开发记录02--布局
    寒假小软件开发记录01--确定方向和素材准备
    大二上学期个人总结
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/6914259.html
Copyright © 2011-2022 走看看