题目链接: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. 仿LOL项目开发第七天

    仿LOL项目开发第七天 by 草帽 不知不觉已经写到了第七篇这种类型的博客,但是回过头看看之前写的,发现都只能我自己能看懂. 我相信在看的童鞋云里雾里的,因为我基本上没怎么详细讲一个脚本怎么用?但是你 ...

  2. Android之Bean属性通知类

    调用: import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import android. ...

  3. Python3.6学习笔记(六)

    WSGI Python Web Server Gateway Interface 规范学习 由于Python的灵活性,提供了多种方式可以作为服务端语言,包括Python编写的服务器(Medusa).P ...

  4. Python3.6学习笔记(三)

    面向对象编程 面向对象编程 Object Oriented Programming 简称 OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程 ...

  5. Cocos2d-x3.3RC0通过JNI调用Android的Java层URI代码发送短信

    1.Jni不在赘述.翻看前面博客 2.直接上代码 1)Java层,直接加在AppActivity.java中 public class AppActivity extends Cocos2dxActi ...

  6. 如何重构"箭头型"代码

    本文主要起因是,一次在微博上和朋友关于嵌套好几层的if-else语句的代码重构的讨论(微博原文),在微博上大家有各式各样的问题和想法.按道理来说这些都是编程的基本功,似乎不太值得写一篇文章,不过我觉得 ...

  7. @Java VisualVM分析堆转储文件

    测试程序 public class HeapOOM { private static int i = 0; static class OOMObject { } public static void ...

  8. [12] 扇形体(Fan)图形的生成算法

    顶点数据的生成 bool YfBuildFunVertices ( Yreal radius, Yreal degree, Yreal height, Yuint slices, YeOriginPo ...

  9. 【PAT Advanced Level】1011. World Cup Betting (20)

    简单模拟题,遍历一遍即可.考察输入输出. #include <iostream> #include <string> #include <stdio.h> #inc ...

  10. HDU1226:超级密码(BFS)

    Problem Description Ignatius花了一个星期的时间终于找到了传说中的宝藏,宝藏被放在一个房间里,房间的门用密码锁起来了,在门旁边的墙上有一些关于密码的提示信息: 密码是一个C进 ...