Travelling Salesman Problem

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 568    Accepted Submission(s): 200

Special Judge

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的棋盘,每个小方格内有一个数值,问从左上角走到右下角。经过的方格的数值和最大是多少。(每个方格仅仅能走一次)

解题思路:

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

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

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

如上面所说;

当n或m为奇数时。棋盘的全部点都能够遍历。

当n和m都是偶数时(如上图),依照黑白涂色的方式。能够发现不管怎样选择路径,走过的黑块数都比红块数多一个,最佳路径则是仅仅有一个红块未走,选择红块值最小的就可以。

如果n*m的棋盘是依照1到n和1到m,那么红块所在的位置[i][j]是(i+j)是奇数(即i和j一个是奇数一个是偶数)。如果未走的位置是(x。y)

对于路径的输出能够分为两类,

第一类:x是偶数(y一定是奇数)     例如以下图:

从第1行究竟x-2行。假设是奇数行,一直R。最后一个D;假设是偶数行,一直L。最后一个D。

第x-1和第x行。假设是第y列,R;假设是奇数列,DR;假设是偶数列,UR,不管是奇数列还是偶数列最后一列都没有R。假设x等于n。到这里就结束了;假设x不等于n,D。

从第x+1行到第n行。假设是奇数行,一直L。最后一个D;假设是偶数行,一直R。最后一个D。不管是奇数行还是偶数行最后一行没有D。

第二类:y是偶数(x一定是奇数)    例如以下图

从第1列究竟y-2行,假设是奇数列,一直D。最后一个R;假设是偶数列,一直U,最后一个R。

第y - 1和第y列。假设是第x行。R。假设是奇数行,RD;假设是偶数行,LD,不管是奇数行还是偶数行最后一行都没有D。假设y等于m。到这里就结束了;假设y不等于m。R。

从第y + 1行到第m列,假设是奇数列。一直U,最后一个R;假设是偶数列,一直D,最后一个R。不管是奇数列还是偶数列最后一列没有R。

代码例如以下:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-6)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 1e9+7
using namespace std;
typedef long long ll;
typedef unsigned long long ULL; int main()
{
int i,j,k,x,y,n,m,ans,minx,a[120][120];
//freopen("dd4.txt","w",stdout);
while(~scanf("%d%d",&n,&m))
{
ans=0;
minx=9999999;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
ans+=a[i][j];
if((i+j)&1)
{
if(a[i][j]<minx)
{
minx=a[i][j];
x=i;
y=j;
} }
}
}
if(n&1)
{
printf("%d\n",ans);
for(i=1;i<=n;i++)
{
if(i&1)
{
for(j=1;j<m;j++)
printf("R");
}
else
{
for(j=1;j<m;j++)
printf("L");
}
if(i!=n)
printf("D");
}
printf("\n");
}
else if(m&1)
{
printf("%d\n",ans);
for(i=1;i<=m;i++)
{
if(i&1)
{
for(j=1;j<n;j++)
printf("D");
}
else
{
for(j=1;j<n;j++)
printf("U");
}
if(i!=m)
printf("R");
}
printf("\n");
}
else
{
printf("%d\n",ans-minx);
//printf("x=%d y=%d\n",x,y);
if(n==2&&m==2)
{
if(minx==a[1][2])
printf("DR\n");
else
printf("RD\n");
}
else
{
if(x%2==0)
{
for(i=1;i<x-1;i++)
{
for(j=1;j<m;j++)
{
if(i&1)
printf("R");//->
else
printf("L");//<-
}
printf("D");//V
}
for(j=1;j<=m;j++)
{
if(j<y)
{
if(j&1)
printf("D");
else
printf("U");
}
else if(j>y)
{
if(j&1)
printf("U");
else
printf("D");
}
if(j!=m)
printf("R");
}
if(x!=n)
printf("D");
for(i=x+1;i<=n;i++)
{
for(j=1;j<m;j++)
{
if(i&1)
printf("L");
else
printf("R");
}
if(i!=n)
printf("D");//V
}
printf("\n");
}
else if(y%2==0)
{
for(i=1;i<y-1;i++)
{
for(j=1;j<n;j++)
{
if(i&1)
printf("D");
else
printf("U");
}
printf("R");
}
for(i=1;i<=n;i++)
{
if(i<x)
{
if(i&1)
printf("R");
else
printf("L");
}
else if(i>x)
{
if(i&1)
printf("L");
else
printf("R");
}
if(i!=n)
printf("D");
}
if(y!=m)
printf("R");
for(i=y+1;i<=m;i++)
{
for(j=1;j<n;j++)
{
if(i&1)
printf("U");
else
printf("D");
}
if(i!=m)
printf("R");
}
printf("\n");
}
}
}
}
return 0;
}

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. HDOJ 5402 Travelling Salesman Problem 模拟

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

  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(大模拟)

    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

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

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

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

随机推荐

  1. Android单选中listview中的一项

    public class LipsListAdapter extends BaseAdapter { private Context context; private List<Lips> ...

  2. thinkphp 日志记录

    日志记录\ThinkPHP\Lib\Think\Core\Log.class.php 1.可以在config.php中进行设置,默认为关闭状态. 'APP_DEBUG' => true 打开\T ...

  3. C/C++中输入多组数据方法

    --------开始-------- 对于刚开始学编程的人来说每次基本上就是一次数据输入,多次的话基本也是会给定一个数组的大小,但随着做刷算法题开始,题目有的会不直接告诉输入几组数据,基本输入都是多组 ...

  4. POJ 1470 Tarjan算法

    裸的LCA,读入小坑.Tarjan算法大坑,一开始不知道哪儿错了,后来才发现,是vis数组忘了清零了(⊙﹏⊙)b 傻傻的用了邻接矩阵...很慢啊,1100多ms. Closest Common Anc ...

  5. Run as ant build每次都执行两次-问题解决

    在Eclipse里面,运行ant,整个测试流程总是执行两遍,其几天试了下在DOS命令行直接调用ant, 结果发现只执行了一次,并且内存消耗好像也没那么大了,估计是eclipse自己的问题.问题解决了, ...

  6. # secure_link_module模块

    作用 制定并允许检查请求的链接的真实性以及保护资源免遭未经授权的访问 限制链接生效周期 首先检查nginx是否已安装模块 #nginx -V 输出nginx所有已安装模块,检查是否有ngx_http_ ...

  7. 联想笋尖S90(S90-t 、S90-u)解锁BootLoader

    工具下载链接: http://pan.baidu.com/s/1eSgZuka 备用下载链接: http://pan.baidu.com/s/1dFKqSId 本篇教程,仅限于联想笋尖S90(S90- ...

  8. MYSQL 代码删除和添加表格列方法

    一个表格建立后用代码删除或添加列: -- 删除列alter table teacher drop column create_time;-- 添加列alter table teacher add co ...

  9. C# 判断字符串是否左包含

    //测试字符串 左包含 //string str = "AAABBBCCC"; //char[] ss = str.ToArray(); //0-8 字符数组 //char[] s ...

  10. Unicode转换为UTF-8过程Demo

    碎碎念:这几天在学习Python对Unicode的支持 上学的时候,计算机基础课上总能听到老师讲什么字节,字符,Unicode,UTF-8吧啦吧啦一堆,反正我是只记住了名字,至于具体这些名字所表达的含 ...