2015 Multi-University Training Contest 9-1007 Travelling Salesman Problem
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.
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.
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).
3 3
2 3 3
3 3 3
3 3 2
25
RRDLLDRR
这是一道有趣的问题,怎样才干从左上到右下使经过路径和最大,每一个数字都是正数,那么假设能走全,肯定是走全比較好,所以当n||m有奇数时。可直接构造之,假设均为偶数时,能够发现,我能够绕啊绕的绕过一个点,剩下的都遍历,横纵坐标和偶数的无法仅仅避开这一个点。所以要想绕开这个点,必需要附带至少一个其他点且是能够单独避开的奇点。所以我们仅仅要找到偶点中最小的那个点绕开就好了。
这样全部情况都构造出来了。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
void print(int x,int y,char dir)
{
for(int i=x;i<y;i++)
printf("%c",dir);
} int f[111][111];
long long ret=0;
int x,y;
int m,n,mi;
int main()
{
while(scanf("%d%d",&m,&n)==2)
{
ret=0;
mi=0x3f3f3f3f;
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&f[i][j]);
ret+=f[i][j];
if((i+j)%2==1 && f[i][j]<mi)
{
mi=f[i][j];
x=i;
y=j;
}
}
if(m%2 == 1|| n%2==1)
{
cout<<ret<<'\n';
if(m%2==1)
{
for(int i=1;i<=m;i++)
{
if(i%2==1)
print(1,n,'R');
else
print(1,n,'L');
if(i!=m)
{
print(i,i+1,'D');
}
}
}
else
{
for(int i=1;i<=n;i++)
{
if(i%2==1)
print(1,m,'D');
else
print(1,m,'U');
if(i!=n)
print(i,i+1,'R');
}
}
}
else
{
cout<<ret-f[x][y]<<'\n';
if(x%2==1)
{
for(int i=1;i<x;i++)
{
if(i%2==1)
print(1,n,'R');
else
print(1,n,'L');
print(i,i+1,'D');
}
for(int i=1;i<=n;i++)
{
if(i<y)
if(i%2==1)
print(1,2,'D');
else
print(1,2,'U');
else
if(i>y)
{
if(i%2==0)
print(1,2,'D');
else
print(1,2,'U');
}
if(i!=n)
print(1,2,'R');
}
for(int i=x+2;i<=m;i++)
{
print(1,2,'D');
if(i%2==1)
print(1,n,'L');
else
print(1,n,'R');
}
}
else
{
for(int i=1;i<y;i++)
{
if(i%2==1)
print(1,m,'D');
else
print(1,m,'U');
print(1,2,'R');
}
for(int i=1;i<=m;i++)
{
if(i<x)
{
if(i%2==1)
print(1,2,'R');
else
print(1,2,'L');
}
else
if(i>x)
{
if(i%2==1)
print(1,2,'L');
else
print(1,2,'R');
}
if(i!=m)
print(1,2,'D');
}
for(int i=y+2;i<=n;i++)
{
print(1,2,'R');
if(i%2==1)
print(1,m,'U');
else
print(1,m,'D');
}
}
}
printf("\n");
}
return 0;
}
2015 Multi-University Training Contest 9-1007 Travelling Salesman Problem的更多相关文章
- HDU 5402 Travelling Salesman Problem (构造)(好题)
大致题意:n*m的非负数矩阵,从(1,1) 仅仅能向四面走,一直走到(n,m)为终点.路径的权就是数的和.输出一条权值最大的路径方案 思路:因为这是非负数,要是有负数就是神题了,要是n,m中有一个是奇 ...
- HDOJ 5402 Travelling Salesman Problem 模拟
行数或列数为奇数就能够所有走完. 行数和列数都是偶数,能够选择空出一个(x+y)为奇数的点. 假设要空出一个(x+y)为偶数的点,则必须空出其它(x+y)为奇数的点 Travelling Salesm ...
- HDU 5402 Travelling Salesman Problem (模拟 有规律)(左上角到右下角路径权值最大,输出路径)
Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- PAT A1150 Travelling Salesman Problem (25 分)——图的遍历
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- PAT 甲级 1150 Travelling Salesman Problem
https://pintia.cn/problem-sets/994805342720868352/problems/1038430013544464384 The "travelling ...
- 构造 - HDU 5402 Travelling Salesman Problem
Travelling Salesman Problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5402 Mean: 现有一 ...
- 1150 Travelling Salesman Problem(25 分)
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- PAT_A1150#Travelling Salesman Problem
Source: PAT A1150 Travelling Salesman Problem (25 分) Description: The "travelling salesman prob ...
- 1150 Travelling Salesman Problem
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- PAT-1150(Travelling Salesman Problem)旅行商问题简化+模拟图+简单回路判断
Travelling Salesman Problem PAT-1150 #include<iostream> #include<cstring> #include<st ...
随机推荐
- CAD参数绘制多行文字(com接口)
在CAD设计时,需要绘制多行文字,用户可以设置设置绘制文字的高度等属性. 主要用到函数说明: _DMxDrawX::DrawMText 绘制一个多行文字.详细说明如下: 参数 说明 DOUBLE dP ...
- 04C#运算符
C#运算符 运算符分类 与C语言一样,如果按照运算符所作用的操作数个数来分,C#语言的运算符可以分为以下几种类型: l 一元运算符:一元运算符作用于一个操作数,例如:-X.++X.X--等. l ...
- java自动机器人自动生成修姓名工具类
public class GenerateName { public static String getName() { Random random = new Random(); String[] ...
- torch.nn.Embedding理解
Pytorch官网的解释是:一个保存了固定字典和大小的简单查找表.这个模块常用来保存词嵌入和用下标检索它们.模块的输入是一个下标的列表,输出是对应的词嵌入. torch.nn.Embedding(nu ...
- jsMap地图网点
<!DOCTYPE html><html><head> <meta charset="utf-8"> <meta name=& ...
- [Luogu] P1865 A % B Problem
题目描述 区间质数个数 输入输出格式 输入格式: 一行两个整数 询问次数n,范围m 接下来n行,每行两个整数 l,r 表示区间 输出格式: 对于每次询问输出个数 t,如l或r∉[1,m]输出 Cros ...
- 笔试算法题(32):归并算法求逆序对 & 将数组元素转换为数组中剩下的其他元素的乘积
出题:多人按照从低到高排成一个前后队列,如果前面的人比后面的高就认为是一个错误对: 例如:[176,178,180,170,171]中的错误对 为 <176,170>, <176,1 ...
- 01基础数据类型——list相关操作
#列表的创建#列表是由[]来表示的,将元素放在[]中,如lst=["aa","bb",["cc","dd"," ...
- linux ping-测试主机之间网络的连通性
博主推荐:更多网络测试相关命令关注 网络测试 收藏linux命令大全 ping命令用来测试主机之间网络的连通性.执行ping指令会使用ICMP传输协议,发出要求回应的信息,若远端主机的网络功能没有问 ...
- Node.js中的Buffer
Buffer介绍 为什么要用Buffer? 在Node/ES6 出现之前,前端工程师只需要进行一些简单的额字符串或者ODM操作就可以满足业务需求了,所有对二进制数据比较陌生. 在node出现之后,前端 ...