题意:从一个方格的左上角走到右下角,拿起经过的全部数字,且每一个方格最多仅仅能走一次,问,终于到达右下角时,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 rows
and columns.
There is a non-negative number in each cell. Teacher Mai wants to walk from the top left corner to
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 following lines,
each line contains numbers.
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 3 2 3 3 3 3 3 3 3 2
Sample Output
25 RRDLLDRR
Author
xudyh
Source
2015 Multi-University Training Contest 9