首页 > 其他分享 >HDU 5402 Travelling Salesman Problem

HDU 5402 Travelling Salesman Problem

时间:2022-11-09 19:32:32浏览次数:43  
标签:HDU int sum else cell printf Problem include Salesman


Problem Description

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


如果n和m里面有一个是奇数那么全部走遍就好了。


否则要找一个最小的点不要,这个点的坐标要满足x+y是奇数


如果不是的话,舍弃该点一定会导致另外一个点也走不到。


然后找到这个点,暴力的一行一行的跑就好了。


#pragma comment(linker, "/STACK:1024000000,1024000000") 
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long LL;
const LL base = 1e9 + 7;
const int maxn = 105;
LL T, n, m, a[maxn][maxn], sum, x, y;

inline void read(int &x)
{
char ch;
while ((ch = getchar())<'0' || ch>'9');
x = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') x = x * 10 + ch - '0';
}

void get()
{
x = 1; y = 2;
for (int i = 1; i <= n;i++)
for (int j = 1; j <= m; j++)
if (((i + j) & 1) && a[x][y] > a[i][j]) x = i, y = j;
}

int main()
{
while (scanf("%lld%lld", &n, &m) !=EOF)
{
sum = 0;
for (int i = 1; i <= n;i++)
for (int j = 1; j <= m; j++)
{
scanf("%lld", &a[i][j]);
sum += a[i][j];
}
if (n & 1 || m & 1)
{
printf("%lld\n", sum);
if (n & 1)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j < m; j++)
if (i & 1) printf("R"); else printf("L");
if (i < n) printf("D"); else printf("\n");
}
}
else
{
for (int i = 1; i <= m; i++)
{
for (int j = 1; j < n; j++)
if (i & 1) printf("D"); else printf("U");
if (i < m) printf("R"); else printf("\n");
}
}
}
else
{
get();
printf("%lld\n", sum - a[x][y]);
for (int i = 1; i <= n; i += 2)
{
if (x == i || x == i + 1)
{
for (int j = 1; j < y; j++)
{
if (j & 1) printf("D"); else printf("U");
printf("R");
}
if (y < m) printf("R");
for (int j = y + 1; j <= m; j++)
{
if (j & 1) printf("U"); else printf("D");
if (j < m) printf("R");
}
if (i < n - 1) printf("D");
}
else if (i < x)
{
for (int j = 1; j < m; j++) printf("R");
printf("D");
for (int j = 1; j < m; j++) printf("L");
printf("D");
}
else
{
for (int j = 1; j < m; j++) printf("L");
printf("D");
for (int j = 1; j < m; j++) printf("R");
if (i < n - 1) printf("D");
}
}
printf("\n");
}
}
return 0;
}



标签:HDU,int,sum,else,cell,printf,Problem,include,Salesman
From: https://blog.51cto.com/u_15870896/5838622

相关文章

  • HDU 1272 小希的迷宫
    ProblemDescription上次Gardon的迷宫城堡小希玩了很久(见ProblemB),现在她也想设计一个迷宫让Gardon来走。但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向......
  • HDU 2242 考研路茫茫——空调教室
    ProblemDescription众所周知,HDU的考研教室是没有空调的,于是就苦了不少不去图书馆的考研仔们。Lele也是其中一个。而某教室旁边又摆着两个未装上的空调,更是引起人们无......
  • HDU 4041 Eliminate Witches!
    ProblemDescriptionKanameMadokaisaMagicalGirl(MahouShoujo/PuellaMagi).ThedutyofaMagicalGirlistoeliminateWitches(Majo).Thoughsoundshorrif......
  • HDU 1237 简单计算器
    ProblemDescription读入一个只包含+,-,*,/的非负整数计算表达式,计算该表达式的值。Input测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字......
  • HDU 4739 Zhuge Liang's Mines
    ProblemDescriptionIntheancientthreekingdomperiod,ZhugeLiangwasthemostfamousandsmartestmilitaryleader.HisenemywasShimaYi,whoalways......
  • HDU 3608 最长回文
    ProblemDescription给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如aba,abba等 Inp......
  • HDU 5591 ZYB's Game
    ProblemDescription withhisclassmatesinhiking:ahostkeepsanumberin  loses,orthehostwilltellalltheplayersthatthenumbernowisbigger......
  • HDU 4433 locker
    ProblemDescriptionApasswordlockerwithNdigits,eachdigitcanberotatedto0-9circularly.Youcanrotate1-3consecutivedigitsupordownino......
  • HDU 5586 Sum
    ProblemDescriptionThereisanumbersequence ,youcanselectainterval[l,r]ornot,allthenumbers  willbecome ..Afterthat,thesumofnnumbers......
  • HDU 4436 str2int
    ProblemDescriptionInthisproblem,youaregivenseveralstringsthatcontainonlydigitsfrom'0'to'9',inclusive.Anexampleisshownbelow.1......