Travelling Salesman Problem (hdu 5402 模拟)
来源:互联网 发布:桃源恋歌动作数据 编辑:程序博客网 时间:2024/06/06 09:23
Travelling Salesman Problem
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 131 Accepted Submission(s): 52
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.
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 numbersn,m(1≤n,m≤100,n∗m≥2) .
In followingn 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 .
For each test case, the first line contains two numbers
In following
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,y−1) , "R" means you walk to cell (x,y+1) , "U" means you walk to cell (x−1,y) , "D" means you walk to cell (x+1,y) .
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
Sample Input
3 32 3 33 3 33 3 2
Sample Output
25RRDLLDRR
Author
xudyh
Source
2015 Multi-University Training Contest 9
Recommend
wange2014 | We have carefully selected several similar problems for you: 5405 5404 5403 5401 5400
题意:n*m的格子,每个格子上有权值,求从(1,1)走到(n,m)所经过的格子权值之和最大为多少,并输出路径。
思路:直接贴上题解,当时想的和它一样,就是bug得蛋疼。
首先如果n为奇数或者m为奇数,那么显然可以遍历整个棋盘。
如果n,m都为偶数,那么讲棋盘黑白染色,假设(1,1)和(n,m)都为黑色,那么这条路径中黑格个数比白格个数多1,而棋盘中黑白格子个数相同,所以必然有一个白格不会被经过,所以选择白格中权值最小的不经过。
构造方法是这样,首先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;}
1 0
- Travelling Salesman Problem (hdu 5402 模拟)
- HDU-5402 Travelling Salesman Problem(模拟)
- hdu 5402 Travelling Salesman Problem 模拟构造
- hdu 5402 Travelling Salesman Problem(构造+模拟)
- HDU 5402 Travelling Salesman Problem(多校9 模拟)
- hdu(5402)——Travelling Salesman Problem(模拟题)
- HDU 5402 Travelling Salesman Problem (MUT#9 暴力模拟)
- hdu 5402 Travelling Salesman Problem(模拟,棋盘染色问题)
- 规律,模拟,贪心(Travelling Salesman Problem,HDU 5402)
- 【HDU 5402】Travelling Salesman Problem(构造)
- hdu 5402 Travelling Salesman Problem(构造)
- HDU 5402 Travelling Salesman Problem
- HDU 5402 Travelling Salesman Problem
- hdu 5402 Travelling Salesman Problem
- hdu 5402 Travelling Salesman Problem
- hdu 5402 Travelling Salesman Problem
- HDOJ 5402 Travelling Salesman Problem 模拟
- HDU 5402 Travelling Salesman Problem(棋盘染色 构造 多校啊)
- 首期百度“金熊掌尖子班”毕业:21个团队获10亿融资
- Chapter5 : View controller & KVC - The Big Nerd Ranch Guide 读书笔记
- python中的有序字典OrderedDict
- category和protocol 第五天稍晚了一些。
- 关于double类型数字相加位数发生变化的问题
- Travelling Salesman Problem (hdu 5402 模拟)
- Android开源项目汇总
- hdu5396 Expression 区间dp +排列组合
- HDU 3255 题解 线段树+扫描线
- Linux基础篇
- iOS OC Label 详解
- gpgpu review
- 我第一个Python教程笔记
- 【多校第9场】【组合数学】【区间dp】【Expression】