Travelling Salesman Problem (hdu 5402 模拟)

Travelling Salesman ProblemTime Limit: 3000/1500 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 131Accepted Submission(s): 52Special Judge

Problem Description

Teacher Mai is in a maze withrows andcolumns. There is a non-negative number in each cell. Teacher Mai wants to walk from the top left cornerto the bottom right corner. 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.In followinglines, each line containsnumbers. The-th number in the-th line means the number in the cell. Every number in the cell is not more than.

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, "L" means you walk to cell, "R" means you walk to cell, "U" means you walk to cell, "D" means you walk to cell.

Sample Input

3 32 3 33 3 33 3 2

Sample Output

25RRDLLDRR

Author

xudyh

Source

Recommend

wange2014|We have carefully selected several similar problems for you:54055404540354015400

题意:n*m的格子,每个格子上有权值,求从(1,1)走到(n,m)所经过的格子权值之和最大为多少,并输出路径。

思路:直接贴上题解,但是想的和它一样,就是bug得蛋疼。

首先如果为奇数或者为奇数,那么显然可以遍历整个棋盘。

如果都为偶数,那么讲棋盘黑白染色,假设和都为黑色,,那么这条路径中黑格个数比白格个数多,而棋盘中黑白格子个数相同,所以必然有一个白格不会被经过,所以选择白格中权值最小的不经过。

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

代码:

#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int INF=0x3f3f3f3f;typedef long long LL;const int maxn=111;int n,m,sum;int Min,sx,sy;int mp[maxn][maxn];void print_1(int r,int c){for (int i=0;i<r/2;i++){for (int j=0;j<c-1;j++)printf("R");printf("D");for (int j=0;j<c-1;j++)printf("L");printf("D");}for (int i=0;i<c-1;i++)printf("R");}void print_2(int r,int c){for (int i=0;i<c/2;i++){for (int j=0;j<r-1;j++)printf("D");printf("R");for (int j=0;j<r-1;j++)printf("U");printf("R");}for (int i=0;i<r-1;i++)printf("D");}void out1(int r,int c){for (int i=0;i<r;i+=2){for (int j=0;j<c-1;j++)printf("R");printf("D");for (int j=0;j<c-1;j++)printf("L");printf("D");}}void out2_1(int c,int x,int y){for (int i=0;i<y-2;i+=2)printf("DRUR");printf("DR");if (y!=c) printf("R");for (int i=0;i<c-y;i+=2){printf("URD");if (i!=c-y-2) printf("R");}if (x+1!=n) printf("D");}void out2_2(int c,int x,int y){for (int i=0;i<y/2;i++)printf("DRUR");printf("RD");for (int i=0;i<(c-y)/2;i++)printf("RURD");if (x!=n) printf("D");}void out3(int r,int c){for (int i=0;i<r;i+=2){for (int j=0;j<c-1;j++)printf("L");printf("D");for (int j=0;j<c-1;j++)printf("R");if (i!=r-2) printf("D");}}int main(){int i,j;while (~scanf("%d%d",&n,&m)){sum=0;Min=INF;for (i=1;i<=n;i++){for (j=1;j<=m;j++){scanf("%d",&mp[i][j]);sum+=mp[i][j];if ((i+j)%2){if (Min>mp[i][j]){Min=mp[i][j];sx=i;sy=j;}}}}if (n%2){printf("%d\n",sum);print_1(n,m);}else if (m%2){printf("%d\n",sum);print_2(n,m);}else{printf("%d\n",sum-Min);int r,rr;if (sx%2){r=sx-1;rr=n-sx-1;}else{r=sx-2;rr=n-sx;}out1(r,m);if (sx%2) out2_1(m,sx,sy);else out2_2(m,sx,sy);out3(rr,m);}printf("\n");}return 0;}

版权声明:本文为博主原创文章,未经博主允许不得转载。

人总是珍惜未得到的,而遗忘了所拥有的

Travelling Salesman Problem (hdu 5402 模拟)

相关文章:

你感兴趣的文章:

标签云: