题目:
Travelling Salesman Problem
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 906 Accepted Submission(s): 331
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(1≤n,m≤100,n?m≥2).
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,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).
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
Recommend
wange2014
题意:给你一个nxm的迷宫,每个格子有一个非负整数,每次可以从一个格子移动到相邻格子,每个格子只能访问一次,问你从(1,1)到(n,m)经过的数字能够得到的最大和是多少,并且求出移动路径。
思路:由于每个格子是非负整数,当我们经过所有格子时肯定得到的和是最大的,于是问题就变成了我们是否能遍历所有的格子。当n或m为奇数时,显然可以,我们只要一行一行的走或一列一列的走就可以了。当n和m都为偶数时,如果我们把横坐标+纵坐标的和为奇数的点染成黑色,把偶数的点染成白色,那么棋盘上黑白棋子是一样多的,并且起点和终点都为黑色,所以从起点到终点的任意路径必然是黑色棋子比白色棋子多一个,故我们不能遍历所有的格子,一定会剩下一个白格子,为了使和最大,我们剩下值最小的白格子就可以了。
接下来说一下构造路径的过程,如果要跳过的白格子在偶数行,那么我们先用RRRRD和LLLLLD移动到它的上上一行,然后按照DR和UR遍历它的这一行和上一行,遇到它的列时用R跳过即可,接下来的行就按照LLLLLD和RRRRRD模拟就可以了;如果要跳过的白格子在奇数行,我们先移动到它的这一行,然后用同样的方法遍历它的这一行和它的下一行即可。
有一个坑点就是要跳过的格子在最后一列时不用R,直接D就行,WA了一次后及时发现了wwwwwww
代码:
#include <cstdlib> #include <cctype> #include <cstring> #include <cstdio> #include <cmath> #include<climits> #include <algorithm> #include <vector> #include <string> #include <iostream> #include <sstream> #include <map> #include <set> #include <queue> #include <stack> #include <fstream> #include <numeric> #include <iomanip> #include <bitset> #include <list> #include <stdexcept> #include <functional> #include <utility> #include <ctime> using namespace std; #define PB push_back #define MP make_pair #define REP(i,x,n) for(int i=x;i<(n);++i) #define FOR(i,l,h) for(int i=(l);i<=(h);++i) #define FORD(i,h,l) for(int i=(h);i>=(l);--i) #define SZ(X) ((int)(X).size()) #define ALL(X) (X).begin(), (X).end() #define RI(X) scanf("%d", &(X)) #define RII(X, Y) scanf("%d%d", &(X), &(Y)) #define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z)) #define DRI(X) int (X); scanf("%d", &X) #define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y) #define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z) #define OI(X) printf("%d",X); #define RS(X) scanf("%s", (X)) #define MS0(X) memset((X), 0, sizeof((X))) #define MS1(X) memset((X), -1, sizeof((X))) #define LEN(X) strlen(X) #define F first #define S second #define Swap(a, b) (a ^= b, b ^= a, a ^= b) #define Dpoint strcut node{int x,y} #define cmpd int cmp(const int &a,const int &b){return a>b;} /*#ifdef HOME freopen("in.txt","r",stdin); #endif*/ const int MOD = 1e9+7; typedef vector<int> VI; typedef vector<string> VS; typedef vector<double> VD; typedef long long LL; typedef pair<int,int> PII; //#define HOME int Scan() { int res = 0, ch, flag = 0; if((ch = getchar()) == '-') //判断正负 flag = 1; else if(ch >= '0' && ch <= '9') //得到完整的数 res = ch - '0'; while((ch = getchar()) >= '0' && ch <= '9' ) res = res * 10 + ch - '0'; return flag ? -res : res; } /*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/ int a[105][105]; int main() { int n,m; while(RII(n,m)!=EOF) { int sum=0; int M=100000; int px=1; int py=1; REP(i,0,n) REP(j,0,m) {RI(a[i][j]); sum+=a[i][j]; if((i+j)%2==1&&a[i][j]<M) { M=a[i][j]; px=i; py=j; } } if(n%2==1) { printf("%d\n",sum); for(int i=0;i<n;i++) { if(i%2==0) { for(int j=0;j<m-1;j++) printf("R"); if(i!=n-1) printf("D"); } else { for(int j=0;j<m-1;j++) printf("L"); printf("D"); } } } else if(m%2==1) { printf("%d\n",sum); for(int i=0;i<m;i++) { if(i%2==0) { for(int j=0;j<n-1;j++) printf("D"); if(i!=m-1) printf("R"); } else { for(int j=0;j<n-1;j++) printf("U"); printf("R"); } } } else { printf("%d\n",sum-M); if(px%2==0) { for(int i=0;i<px;i++) { if(i%2==0) { for(int j=0;j<m-1;j++) printf("R"); if(i!=n-1) printf("D"); } else { for(int j=0;j<m-1;j++) printf("L"); printf("D"); } } for(int i=0;i<py;i++) { if(i%2==0) printf("DR"); else printf("UR"); } if(py<m-1) printf("R"); else { if(px+1!=n-1) printf("D"); } for(int i=py+1;i<m;i++) { if(i%2==0) printf("UR"); else { printf("D"); if(i==m-1) { if(px+1!=n-1) printf("D");} else printf("R"); } } for(int i=px+2;i<n;i++) { if(i%2==0) { for(int j=0;j<m-1;j++) printf("L"); printf("D"); } else { for(int j=0;j<m-1;j++) printf("R"); if(i!=n-1) printf("D"); } } } else { for(int i=0;i<px-1;i++) { if(i%2==0) { for(int j=0;j<m-1;j++) printf("R"); if(i!=n-1) printf("D"); } else { for(int j=0;j<m-1;j++) printf("L"); printf("D"); } } for(int i=0;i<py;i++) { if(i%2==0) { printf("DR"); } else printf("UR"); } printf("R"); for(int i=py+1;i<m;i++) { if(i%2==0) { printf("UR"); } else { printf("D"); if(i==m-1) { if(px!=n-1) { printf("D"); } } else { printf("R"); } } } for(int i=px+1;i<n;i++) { if(i%2==0) { for(int j=0;j<m-1;j++) printf("L"); printf("D"); } else { for(int j=0;j<m-1;j++) printf("R"); if(i!=n-1) printf("D"); } } } } printf("\n"); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。