hdu 5402 Travelling Salesman Problem(大模拟)
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 (,) 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.
There are multiple test cases. For each test case, the first line contains two numbers n,m(≤n,m≤,n∗m≥). 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 .
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−), "R" means you walk to cell (x,y+), "U" means you walk to cell (x−,y), "D" means you walk to cell (x+,y).
RRDLLDRR
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m;
int mp[][];
int x,y;
int minn;
void get()
{
x = ; y = ;
for (int i = ; i <= n;i++)
for (int j = ; j <= m; j++)
if (((i + j) & ) && mp[x][y] > mp[i][j]) x = i, y = j;
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
int sum=;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
scanf("%d",&mp[i][j]);
sum+=mp[i][j];
}
}
if((n&))
{ printf("%d\n",sum);
if(n==)
{
for(int i=;i<m;i++)
printf("R");
printf("\n");
continue;
}
for(int i=;i<m;i++)
printf("R");
printf("D");
for(int i=;i<=n;i++)
{
if(i%==)
{
for(int j=;j<m;j++)
printf("L");
printf("D");
}
else
{
if(i!=n)
{
for(int i=;i<m;i++)
printf("R");
printf("D");
}
else
{
for(int i=;i<m;i++)
printf("R");
}
}
}
printf("\n"); }
else if((m%))
{
printf("%d\n",sum);
if(m==)
{
for(int i=;i<n;i++)
printf("D");
printf("\n");
continue;
}
for(int i=;i<n;i++)
printf("D");
printf("R");
for(int i=;i<=m;i++)
{
if(i%==)
{
for(int j=;j<n;j++)
printf("U");
printf("R");
}
else
{
if(i!=m)
{
for(int j=;j<n;j++)
printf("D");
printf("R");
}
else
{
for(int j=;j<n;j++)
printf("D");
}
}
}
printf("\n");
}
else if((m%)== && (n%)==)
{
get();
printf("%d\n", sum - mp[x][y]);
for (int i = ; i <= n; i += )
{
if (x == i || x == i + )
{
for (int j = ; j < y; j++)
{
if (j & ) printf("D"); else printf("U");
printf("R");
}
if (y < m) printf("R");
for (int j = y + ; j <= m; j++)
{
if (j & ) printf("U"); else printf("D");
if (j < m) printf("R");
}
if (i < n - ) printf("D");
}
else if (i < x)
{
for (int j = ; j < m; j++) printf("R");
printf("D");
for (int j = ; j < m; j++) printf("L");
printf("D");
}
else
{
for (int j = ; j < m; j++) printf("L");
printf("D");
for (int j = ; j < m; j++) printf("R");
if (i < n - ) printf("D");
}
}
printf("\n");
} }
return ;
}
hdu 5402 Travelling Salesman Problem(大模拟)的更多相关文章
- HDU 5402 Travelling Salesman Problem (模拟 有规律)(左上角到右下角路径权值最大,输出路径)
Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- 构造 - HDU 5402 Travelling Salesman Problem
Travelling Salesman Problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5402 Mean: 现有一 ...
- HDU 5402 Travelling Salesman Problem (构造)(好题)
大致题意:n*m的非负数矩阵,从(1,1) 仅仅能向四面走,一直走到(n,m)为终点.路径的权就是数的和.输出一条权值最大的路径方案 思路:因为这是非负数,要是有负数就是神题了,要是n,m中有一个是奇 ...
- HDU 5402 Travelling Salesman Problem(多校9 模拟)
题目链接:pid=5402">http://acm.hdu.edu.cn/showproblem.php?pid=5402 题意:给出一个n×m的矩阵,位置(i.j)有一个非负权值. ...
- HDU 5402 Travelling Salesman Problem(棋盘染色 构造 多校啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5402 Problem Description Teacher Mai is in a maze wit ...
- HDU 5402 : Travelling Salesman Problem
题目大意:n*m的格子,从左上角走到右下角,每个格子只能走一遍,每个格子上有一个非负数,要让途径的数字和最大,最后要输出路径 思路:显然茹果n,m有一个是奇数的话所有格子的数字都能被我吃到,如果都是偶 ...
- 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 ...
随机推荐
- js解析json,js转换json成map,获取map的key,value
json串格式 { "10.10.11.1": { "target_1": "34.2", "target_3": &q ...
- resin安装
1. 说明 Resin版本:resin-3.1.6 2. 安装jdk 安装jdk-1_5_0_09-windows-i586-p.exe程序到相应目录. (假设安装到D:\ jdk1.5.0_09) ...
- coco2d-html5制作弹弓射鸟第一部分---橡皮筋
一.写在前面的话 最近在学习cocos2d-html5方面的知识,一直想从事游戏方面的工作,自己也找了好多资料.网上关于cocos2d-html5的教程真的不多,也只有自己摸索,慢慢看示例代码.由于本 ...
- Python:常见错误集锦(持续更新ing)
初学Python,很容易与各种错误不断的遭遇.通过集锦,可以快速的找到错误的原因和解决方法. 1.IndentationError:expected an indented block 说明此处需要缩 ...
- Selenium2学习之-环境搭建
1.下载安装Eclipse 2.下载并配置jdk环境变量 2.1 介绍一下环境变量设置 2.1.1 点击环境变量 2.1.2 新增系统变量JAVA_HOME 变量名:JAVA_HOME 变量值:C:\ ...
- DataGrid( 数据表格) 组件[8]
本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于Panel(面板).Resizeable(调整大小).LinkButton(按钮).Pageination( ...
- MenuButton( 菜单按钮)
一. 加载方式//class 加载方式<a href="javascript:void(0)" id="edit" class="easyui- ...
- XAML 名称范围 (x:) 语言特性
本节介绍为 Windows 运行时实现的 XAML 语言特性的参考信息. 本部分内容 主题 描述 x:Class 属性 配置 XAML 编译,在标记和代码隐藏之间连接分部类.代码分部类在一个独立的代码 ...
- linux目录权限小记
r : 拥有读取目录结构列表的权限 x:拥有进入此目录的权限 w: 1: 建立新的档案和目彔: 2删除已经存在的档案和目录(无论该档案的权限为何!) 3能够重命名档案和目录: 4 能够移动目录里面的 ...
- defer和async
1.decument.wirte不能使用 2.<script src="text.js" type="text/javascript" defer=&qu ...