Hdu 5402 Travelling Salesman Problem 棋盘黑白染色
来源:互联网 发布:专业电气制图软件 编辑:程序博客网 时间:2024/05/15 23:48
Travelling Salesman Problem
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1560 Accepted Submission(s): 581
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
一个矩阵,从左上走到右下,每个格子最多经过一次,问路径上所有数字最大是多少。
显然长宽有一个为偶数时可以有遍历整个棋盘的方案。
当长宽都为偶数时,把棋盘黑白染色,左上和右下染成黑,和黑色相邻的染成白色,再把相邻的染成黑色,直到染完。发现黑格和白格数量永远相等。
这样,最优策略是绕过一个数字最小的白格子。因为路径上黑白交替,起点终点都是黑色,黑格子数量永远比白格子多一格,所以没办法绕过黑格子。
具体的路径怎么走,画画图可以推出来。
#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;const int maxn=105,inf=0x3f3f3f3f; const ll llinf=0x3f3f3f3f3f3f3f3f; const ld pi=acos(-1.0L); int a[maxn][maxn];int main() { int n,m; char s[4]={'D','R','U','R'}; int dir[4][2]={{1,0},{0,1},{-1,0},{0,1}}; while (scanf("%d%d",&n,&m)!=EOF) { int i,j,sum=0,p=inf,x,y; for (i=1;i<=n;i++) { for (j=1;j<=m;j++) { scanf("%d",&a[i][j]); sum+=a[i][j]; if ((i+j)%2==1&&a[i][j]<p) p=a[i][j],x=i,y=j; } } if (n%2==1) { printf("%d\n",sum); for (i=1;i<=n;i++) { for (j=1;j<m;j++) { if (i%2==1) printf("R"); else printf("L"); } if (i!=n) printf("D"); } } else if (m%2==1) { printf("%d\n",sum); for (i=1;i<=m;i++) { for (j=1;j<n;j++) { if (i%2==1) printf("D"); else printf("U"); } if (i!=m) printf("R"); } } else { printf("%d\n",sum-p); for (i=1;i<=(x-1)/2*2;i++) { for (j=1;j<m;j++) { if (i%2==1) printf("R"); else printf("L"); } printf("D"); } int cnt=0,k=0;j=1; while (cnt<2*(m-1)) { cnt++; if (i+dir[k][0]==x&&j+dir[k][1]==y) { k=(k-1+4)%4; printf("%c",s[k]); i+=dir[k][0];j+=dir[k][1]; k++;k%=4; } else { printf("%c",s[k]); i+=dir[k][0];j+=dir[k][1]; k++;k%=4; } } if (i!=n) printf("D"); for (i=(x-1)/2*2+3;i<=n;i++) { for (j=1;j<m;j++) { if (i%2==0) printf("R"); else printf("L"); } if (i!=n) printf("D"); } } printf("\n"); } return 0;}
阅读全文
0 0
- 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(模拟,棋盘染色问题)
- hdu5402 Travelling Salesman Problem(棋盘染色+模拟)
- 2015多校第九场 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
- 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 模拟构造
- ubantu linux下制作win10启动盘
- UVA Live 7957 (Gym 101201F) Illumination 2-SAT
- 单链表的操作练习
- HTTPS 是如何保证安全的?
- html中如何去掉浏览器右边的滚动条
- Hdu 5402 Travelling Salesman Problem 棋盘黑白染色
- 用SAP WEB IDE开发的同学必看!
- Hdu 5400 Arithmetic Sequence 数学推理
- SAPUI5教程——MaskInput的妙用
- Codeforces Gym 101473C Boss 拓扑排序
- Codeforces Gym 101473D Folding Machine DFS/XJBS
- Codeforces Gym 100096A Athletic competition 树型DP
- 方正集团发生“窝案”,北大当局负有什么责任?
- pascal语言入门