题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5402

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

题意:

有一个n*m的迷宫,每个格子都有一个非负整数,一个人要从迷宫的左上角(1,1)走到迷宫的右下角(n,m)。

并且要使他经过的路径的和最大,

求最大和为多少而且输出他所走的路径。

官方题解:

首先假设nn为奇数或者mm为奇数,那么显然能够遍历整个棋盘。

如果n,mn,m都为偶数。那么讲棋盘黑白染色。如果(1,1)(1,1)和(n,m)(n,m)都为黑色,那么这条路径中黑格个数比白格个数多11,而棋盘中黑白格子个数同样,所以必定有一个白格不会被经过。所以选择白格中权值最小的不经过。

构造方法是这样,首先RRRRDLLLLD这种路径走到这个格子所在行或者上一行。然后DRUR这样走到这个格子的所在列或者前一列。然后绕过这个格子。

然后走完这两行,接着按LLLLDRRRR这种路径往下走。

再贴一个解说非常具体的链接:http://blog.csdn.net/queuelovestack/article/details/47756605

代码例如以下:

#include <cstdio>
int n, m;
int a[117][117];
int sum;
int min_x, min_y; void build_1()//都是偶数
{
printf("%d\n",sum-a[min_x][min_y]); for(int i = 1; i <= n; i+=2)
{
if(min_x==i || min_x==i+1)
{
for(int j = 1; j < min_y; j++)
{
if(j&1)
printf("D");
else
printf("U");
printf("R");
}
if(min_y < m)
printf("R");
for(int j = min_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(min_x > i)//上面
{
for(int j = 1; j < m; j++)
printf("R");
printf("D");
for(int j = m; j > 1; j--)
printf("L");
printf("D");
}
else//以下
{
for(int j = m; j > 1; j--)
printf("L");
printf("D");
for(int j = 1; j < m; j++)
printf("R");
if(i < n-1)
printf("D");
}
}
printf("\n");
}
void build_2()
{
printf("%d\n",sum); if(n&1)//假设n是奇数,先走偶数方向
{
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");
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
sum=0;
min_x = 1;
min_y = 2;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
scanf("%d",&a[i][j]);
sum+=a[i][j];
if(((i+j)&1) && (a[min_x][min_y]>a[i][j]))//坐标和为奇数且最小
{
min_x = i;
min_y = j;
}
}
}
if(!(n&1) && !(m&1))//都是偶数
{
build_1();
}
else
{
build_2();
}
}
return 0;
}
/*
3 3
2 3 3
3 3 3
3 3 2
*/

HDU 5402 Travelling Salesman Problem(棋盘染色 构造 多校啊)的更多相关文章

  1. 构造 - HDU 5402 Travelling Salesman Problem

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

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

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

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

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

  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

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

  6. hdu 5402 Travelling Salesman Problem(大模拟)

    Problem Description Teacher Mai ,) to the bottom right corner (n,m). He can choose one direction and ...

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

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

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

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

  9. HDOJ 5402 Travelling Salesman Problem 模拟

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

随机推荐

  1. 张明楷:案件事实认定方法的七点注意 z

    作者|张明楷 来源|<法学杂志> 大体而言,定罪是一个三段论的推理过程.刑法规范是大前提,案件事实是小前提,如果二者相符合,便可以作出相应的判决.具体地说,法官必须把应当判决的.具体的个案 ...

  2. 单独配置secondarynamenode

    一.配置说明 这是在我之前yarn框架上通过加入节点,改动相关的配置文件,使得secondarynamenode独立出来的,所以这里前期的一系列琐碎配置请參考我之前的博客: http://blog.c ...

  3. OpenShift 项目的备份和恢复实验

    本测试记录从openshift 3.6环境中导出项目,然后在将项目环境恢复到Openshift 3.11中所需要的步骤 从而指导导入导出的升级过程. 1.安装Openshift 3.6版本 过程略 2 ...

  4. 网站功能操作分布引导插件:Intro.js介绍;React里如何使用Intro.js以及如何进行分页导航

    插件作用:使用向导,引导新用户正确使用Web网站.我的环境是React+Mobx. 基本使用介绍,参加代码地址里的README.md:https://github.com/usablica/intro ...

  5. 【BZOJ】【1415】【NOI2005】聪聪和可可

    数学期望+记忆化搜索 论文:<浅析竞赛中一类数学期望问题的解决方法>——汤可因  中的第一题…… Orz 黄学长 我实在是太弱,这么简单都yy不出来…… 宽搜预处理有点spfa的感觉= = ...

  6. opencv2.4中SVD分解的几种调用方法

    原帖地址: http://blog.sina.com.cn/s/blog_6109b5d00101ag7a.html       在摄影测量和计算机视觉中,考虑最优解问题时,经常要用到SVD分解.奇异 ...

  7. iOS开发-UITableView常用方法

    UITableView常用来展示数据,类似于Android中的ListView,相对于Android中的ListView而言,UITableView的实现是非常简单,继承UITableViewData ...

  8. IT运维流程 — ITIL

    导读 在IT运维中,最有名也是最实用的流程就是ITIL.说到这里,我想大家都有过实施ITIL痛苦的经历,一定会有人说:我们没有办法实施ITIL. 问:ITIL是什么? 答:ITIL即IT基础架构库(I ...

  9. IStat Menus 5.02 5.03 的注册码

    1574-5977-7956-8062-0000 6015-5448-3282-4975-0000 9665-5955-6856-2071-0000 2447-9517-7939-5221-0000

  10. NodeBB,一个基于nodejs的响应式论坛

    喜欢方便的同学请绕道去discuz,好吧我是nodejs的重视患者,首先你要有自己的vps或则云空间,比如9cloud,我今天用的是阿里云的VPS. 进入阿里云Ubuntu主机 .... 输入密码进入 ...