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. Linux下那些奇葩的命令

    相信喜爱编程,痴迷技术的你,肯定接触过甚至深爱着linux,甚至可能已经很熟悉linux了,可是linux那逗比的一面,你又知道多少. 本文!纯粹娱乐!不喜勿喷! 1.程序猿的愤慨! yes 当我们再 ...

  2. HDU 2435 There is a war (网络流-最小割)

    There is a war Problem Description       There is a sea.       There are N islands in the sea.       ...

  3. Mina入门实例

    继续上一篇,这篇主要讲通过mina往B端发送消息.并接受消息,mina是一个网络通信框架,封装了javaNIO.简单易用.网上有非常多关于他的介绍,在此不赘述了. 如上篇所介绍,完毕功能,须要五个类: ...

  4. 正则表达式JSP实例

    <%@ page language="java" import="java.util.*,cn.com.Person,cn.com.Adddress" p ...

  5. PropertyGird( 属性表格) 组件

    本节课重点了解 EasyUI 中 PropertyGird(属性表格)组件的使用方法,这个组件依赖于 DataGrid(数据表格)组件. 一. 加载方式 //class 加载方式<table i ...

  6. Responsive Table 利用@media

    html <table> <thead> <tr> <th>First Name</th> <th>Last Name</ ...

  7. bestcoder r44 p3 hdu 5270 ZYB loves Xor II

    这是昨晚队友跟我说的题,不知道当时是什么玄幻的事件发生了,,我看成了两两相乘的XOR 纠结了好长时间间 不知道该怎么办 今天早上看了下这道题,发现是两两相加的XOR  然后就想了想昨晚的思路 发现可做 ...

  8. 移动端-jquery Ajax执行滑动到底部进行加载 指定加载次数

    过完年了,又要有新的项目了,趁暴风雨前的宁静,就把之前让我挺头疼的ajax加载给总结了一下.在此分享. 声明下:笔者对于ajax也是新手,如有更好的写法或可以优化的写法,还望指正! 项目需求: 移动端 ...

  9. Kendo Web UI Grid添加一个html控件如(checkbox,button)

    在Kendo Web UI Grid增加一个控件如效果图: <div id="grid1"></div><script> $("#gr ...

  10. Java实现生产者消费者问题与读者写者问题

    摘要: Java实现生产者消费者问题与读者写者问题 1.生产者消费者问题 生产者消费者问题是研究多线程程序时绕不开的经典问题之一,它描述是有一块缓冲区作为仓库,生产者可以将产品放入仓库,消费者则可以从 ...