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 (,) 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(≤n,m≤,n∗m≥).

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 .
 
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−), "R" means you walk to cell (x,y+), "U" means you walk to cell (x−,y), "D" means you walk to cell (x+,y).

 
Sample Input

  
 
Sample Output
RRDLLDRR
 
Author
xudyh
 
Source
 
真是个鸟题,一开始把情况都考虑了,交上去直接WA了,后来意识到是n、m同为偶数有错。  其实只要找找规律就可以得出找的最小值的横纵坐标的和要为奇数,不过这个的顺序也是很难写啊。。。。。。
 
 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m;
int mp[][];
int x,y;
int minn;
void get()
{
x = ; y = ;
for (int i = ; i <= n;i++)
for (int j = ; j <= m; j++)
if (((i + j) & ) && mp[x][y] > mp[i][j]) x = i, y = j;
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
int sum=;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
scanf("%d",&mp[i][j]);
sum+=mp[i][j];
}
}
if((n&))
{ printf("%d\n",sum);
if(n==)
{
for(int i=;i<m;i++)
printf("R");
printf("\n");
continue;
}
for(int i=;i<m;i++)
printf("R");
printf("D");
for(int i=;i<=n;i++)
{
if(i%==)
{
for(int j=;j<m;j++)
printf("L");
printf("D");
}
else
{
if(i!=n)
{
for(int i=;i<m;i++)
printf("R");
printf("D");
}
else
{
for(int i=;i<m;i++)
printf("R");
}
}
}
printf("\n"); }
else if((m%))
{
printf("%d\n",sum);
if(m==)
{
for(int i=;i<n;i++)
printf("D");
printf("\n");
continue;
}
for(int i=;i<n;i++)
printf("D");
printf("R");
for(int i=;i<=m;i++)
{
if(i%==)
{
for(int j=;j<n;j++)
printf("U");
printf("R");
}
else
{
if(i!=m)
{
for(int j=;j<n;j++)
printf("D");
printf("R");
}
else
{
for(int j=;j<n;j++)
printf("D");
}
}
}
printf("\n");
}
else if((m%)== && (n%)==)
{
get();
printf("%d\n", sum - mp[x][y]);
for (int i = ; i <= n; i += )
{
if (x == i || x == i + )
{
for (int j = ; j < y; j++)
{
if (j & ) printf("D"); else printf("U");
printf("R");
}
if (y < m) printf("R");
for (int j = y + ; j <= m; j++)
{
if (j & ) printf("U"); else printf("D");
if (j < m) printf("R");
}
if (i < n - ) printf("D");
}
else if (i < x)
{
for (int j = ; j < m; j++) printf("R");
printf("D");
for (int j = ; j < m; j++) printf("L");
printf("D");
}
else
{
for (int j = ; j < m; j++) printf("L");
printf("D");
for (int j = ; j < m; j++) printf("R");
if (i < n - ) printf("D");
}
}
printf("\n");
} }
return ;
}
 

hdu 5402 Travelling Salesman Problem(大模拟)的更多相关文章

  1. HDU 5402 Travelling Salesman Problem (模拟 有规律)(左上角到右下角路径权值最大,输出路径)

    Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (J ...

  2. 构造 - HDU 5402 Travelling Salesman Problem

    Travelling Salesman Problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5402 Mean: 现有一 ...

  3. HDU 5402 Travelling Salesman Problem (构造)(好题)

    大致题意:n*m的非负数矩阵,从(1,1) 仅仅能向四面走,一直走到(n,m)为终点.路径的权就是数的和.输出一条权值最大的路径方案 思路:因为这是非负数,要是有负数就是神题了,要是n,m中有一个是奇 ...

  4. HDU 5402 Travelling Salesman Problem(多校9 模拟)

    题目链接:pid=5402">http://acm.hdu.edu.cn/showproblem.php?pid=5402 题意:给出一个n×m的矩阵,位置(i.j)有一个非负权值. ...

  5. HDU 5402 Travelling Salesman Problem(棋盘染色 构造 多校啊)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5402 Problem Description Teacher Mai is in a maze wit ...

  6. HDU 5402 : Travelling Salesman Problem

    题目大意:n*m的格子,从左上角走到右下角,每个格子只能走一遍,每个格子上有一个非负数,要让途径的数字和最大,最后要输出路径 思路:显然茹果n,m有一个是奇数的话所有格子的数字都能被我吃到,如果都是偶 ...

  7. hdu 5402 Travelling Salesman Problem (技巧,未写完)

    题意:给一个n*m的矩阵,每个格子中有一个数字,每个格子仅可以走一次,问从(1,1)走到(n,m) 的路径点权之和. 思路: 想了挺久,就是有个问题不能短时间证明,所以不敢下手. 显然只要n和m其中一 ...

  8. HDOJ 5402 Travelling Salesman Problem 模拟

    行数或列数为奇数就能够所有走完. 行数和列数都是偶数,能够选择空出一个(x+y)为奇数的点. 假设要空出一个(x+y)为偶数的点,则必须空出其它(x+y)为奇数的点 Travelling Salesm ...

  9. HDU 5402(Travelling Salesman Problem-构造矩阵对角最长不相交路径)

    Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (J ...

随机推荐

  1. JavaScript学习笔记(高级部分—01)

    JavaScript的核心ECMAScript描述了该语言的语法和基本对象:DOM描述了处理网页内容的方法和接口:BOM描述了与浏览器进行交互的方法和接口. 简单说,ECMAScript描述了以下内容 ...

  2. javascript单元测试-jsamine[转]

    Jasmine的开发团队来自PivotalLabs,他们一开始开发的JavaScript测试框架是JsUnit,来源于著名的JAVA测试框架JUnit.JsUnit是xUnit的JavaScript实 ...

  3. FilterDispatcher已被标注为过时解决办法

    一些struts2的教程都是比较早的,当我们基于较新版本的struts2来实现代码的时候,往往会出现一些问题.比如这个警告:FilterDispatcher isdeprecated! 在web.xm ...

  4. Android两种 旋转Bitmap方法

    方法1. 利用Bitmap.createBitmap Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {      ...

  5. 自定义PopupWindow动画效果

    public class RollActivity extends Activity { private View view; private Button btn; private PopupWin ...

  6. Linux修改SSH连接数 重启SSH服务

    系统 linux,增加SSH终端连接数最大为1000个 解决方案: vi /etc/ssh/sshd_config 输入/MaxStartups 定位到如下并修改 1)        #MaxStar ...

  7. java下properties属性文件操作

    package cn.stat.p1.file; import java.io.File; import java.io.FileInputStream; import java.io.FileNot ...

  8. ubuntu 快捷键和安装知识知识

    本文节选自“The Official Ubuntu Book, 7th Edition.pdf” 快捷键部分直接引用原书中图片. Linux Folders Learning Unity Keyboa ...

  9. [BZOJ3561] DZY Loves Math VI

    (14.10.28改) 本来只想写BZOJ3739:DZY Loves Math VIII的,不过因为和VI有关系,而且也没别人写过VI的题解,那么写下. 不过我还不会插公式…… http://www ...

  10. 利用redis协助mysql数据库搬迁

    最近公司新项目上线,需要数据库搬迁,但新版本和老版本数据库差距比较大,关系也比较复杂.如果用传统办法,需要撰写很多mysql脚本,工程量虽然不大,但对于没有dba的公司来说,稍微有点难度.本人就勉为其 ...