题目链接: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. 通过扩展jQuery UI Widget Factory实现手动调整Accordion高度

    □ 实现Accordion高度一致 <head> <meta name="viewport" content="width=device-width&q ...

  2. Python中函数的参数传递与可变长参数

    转自旭东的博客原文 Python中函数的参数传递与可变长参数 Python中传递参数有以下几种类型: (1)像C++一样的默认缺省函数 (2)根据参数名传参数 (3)可变长度参数 示例如下: (1)默 ...

  3. NSLog 不打印中文 - 解决

    解决方案:将项目的Debugger模式设置为 GDB 即可.(LLDB下不打印中文) 第一步: 第二步:

  4. python2 和 python3兼容写法

    一:使用ImportError,Python3中将一些Python2的模块名称做了修改,需要我们做一些处理来保证代码在不同Python版本中能够正常运行 # -*- coding: utf- -*- ...

  5. SpringMVC in IDEA开发实践

    按照上篇装过Tomcat之后. 本机本来装了IDEA和Maven. 参考以下这篇 https://my.oschina.net/gaussik/blog/385697 <使用IntelliJ I ...

  6. go语言基础之go猜数字游戏

    1. 产生一个随机的4位数 示例1: package main import "fmt" import "math/rand" import "tim ...

  7. SoundPool 音频播放 API 详解【示例】

    demo地址:https://github.com/baiqiantao/PermissionTest.git 一个大坑:SoundPool最多只能播放时长10s左右.大小70kb左右(这些值是我多次 ...

  8. j2ee model1模型完成分页逻辑的实现 详解!

    在显示用户全部信息的页面,在显示全部数据的时候,长长的滚动条,像是没有边界的天空一样, 让用户查看数据很不方便. 于是, 我们要把这些数据分页显示, 就像office的word一样,每页显示一定数量的 ...

  9. React Native for Android 热部署图片自己定义方案

    情景 热部署时,我们期望升级包中包括js代码与图片资源. bundle的热部署网上已经有两种方案了,一种是用反射,一种是利用RN自带函数.将bundle初始化时直接放到指定文件夹下,之后通过替换bun ...

  10. Mongo读书笔记1 -- GridFS

      一个Mongo文档最大4M. GridFS不依赖于MongoDB, 其他符合规范的驱动都可以访问它. GridFS包含两部分:一部分存储文件名和其他metadata; 另一部分存储实际的文件,通常 ...