8
18
2015

### Travelling Salesman Problem

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 34    Accepted Submission(s): 2
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

#define _CRT_SECURE_NO_WARNINGS

#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>

using namespace std;

typedef long long LL;

void Main();
int main()
{
Main();
return 0;
}
int n, m;
int a;
int tot, minn;
int xx, yy;

void Main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
tot = 0; minn = 10010;
for (int i = 1;i <= n;++i)
for (int j = 1;j <= m;++j)
{
scanf("%d", &a[i][j]);
tot += a[i][j];
if ((i + j) & 1)
{
if (minn > a[i][j])
{
minn = a[i][j];
xx = i; yy = j;
}
}
}
if (n & 1)
{
printf("%d\n", tot);
for (int i = 1;i <= n / 2;++i)
{
for (int j = 1;j <= m - 1;++j)printf("R");
printf("D");
for (int j = 1;j <= m - 1;++j)printf("L");
printf("D");
}
for (int j = 1;j <= m - 1;++j)printf("R");
puts("");
continue;
}
if (m & 1)
{
printf("%d\n", tot);
for (int i = 1;i <= m / 2;++i)
{
for (int j = 1;j <= n - 1;++j)printf("D");
printf("R");
for (int j = 1;j <= n - 1;++j)printf("U");
printf("R");
}
for (int j = 1;j <= n - 1;++j)printf("D");
puts("");
continue;
}
if (yy & 1)
{
printf("%d\n", tot - minn);
for (int i = 1;i <= ((yy - 1) >> 1);++i)
{
for (int j = 1;j <= n - 1;++j)printf("D");
printf("R");
for (int j = 1;j <= n - 1;++j)printf("U");
printf("R");
}
printf("R");
for (int i = 1;i <= ((xx - 1) >> 1);++i)printf("DLDR");
printf("D");
for (int i = 1;i <= (n - xx) / 2;++i)printf("DLDR");
for (int i = 1;i <= (m - yy) / 2;++i)
{
printf("R");
for (int j = 1;j <= n - 1;++j)printf("U");
printf("R");
for (int j = 1;j <= n - 1;++j)printf("D");
}
puts("");
}
else
{
printf("%d\n", tot - minn);
for (int i = 1;i <= ((yy - 1) >> 1);++i)
{
for (int j = 1;j <= n - 1;++j)printf("D");
printf("R");
for (int j = 1;j <= n - 1;++j)printf("U");
printf("R");
}
for (int i = 1;i <= ((xx - 1) >> 1);++i)printf("RDLD");
printf("D");
for (int i = 1;i <= ((n - xx - 1) >> 1);++i)printf("RDLD");
printf("R");
for (int i = 1;i <= (m - yy) / 2;++i)
{
printf("R");
for (int j = 1;j <= n - 1;++j)printf("U");
printf("R");
for (int j = 1;j <= n - 1;++j)printf("D");
}
puts("");
}
}
return;
}

Category: 程序 | Tags: | Read Count: 586 (输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com