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\n",sum);
			work1(n,m-1);
		}
		else if(m&1)
		{
			printf("%d\n",sum);
			work2(n-1,m);
		}
		else
		{
			printf("%d\n",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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值