HDU 5402 Travelling Salesman Problem(棋盘染色 构造 多校啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5402
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的迷宫,每个格子都有一个非负整数,一个人要从迷宫的左上角(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(棋盘染色 构造 多校啊)的更多相关文章
- 构造 - 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 (模拟 有规律)(左上角到右下角路径权值最大,输出路径)
Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- 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
题目大意:n*m的格子,从左上角走到右下角,每个格子只能走一遍,每个格子上有一个非负数,要让途径的数字和最大,最后要输出路径 思路:显然茹果n,m有一个是奇数的话所有格子的数字都能被我吃到,如果都是偶 ...
- hdu 5402 Travelling Salesman Problem(大模拟)
Problem Description Teacher Mai ,) to the bottom right corner (n,m). He can choose one direction and ...
- hdu 5402 Travelling Salesman Problem (技巧,未写完)
题意:给一个n*m的矩阵,每个格子中有一个数字,每个格子仅可以走一次,问从(1,1)走到(n,m) 的路径点权之和. 思路: 想了挺久,就是有个问题不能短时间证明,所以不敢下手. 显然只要n和m其中一 ...
- HDU 5402(Travelling Salesman Problem-构造矩阵对角最长不相交路径)
Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- HDOJ 5402 Travelling Salesman Problem 模拟
行数或列数为奇数就能够所有走完. 行数和列数都是偶数,能够选择空出一个(x+y)为奇数的点. 假设要空出一个(x+y)为偶数的点,则必须空出其它(x+y)为奇数的点 Travelling Salesm ...
随机推荐
- GDB高级用法
http://blog.csdn.net/wwwsq/article/details/7086151
- [html5]使用localStorage兼容低版本Safari无法使用indexeddb的情况
摘要 简单场景描述:将html5开发的app内嵌入ios app中,有部分数据,需要在本地存储,就想到使用浏览器的localstorage或者indexeddb,另外localstorage存储的方式 ...
- C#程序集系列12,C#编译器和CLR如何找寻程序集
本篇体验C#编译器和CLR运行时如何查找程序集,并自定义CLR运行时查找程序集的方式. □ C#编译器和CLR运行时如何查找程序集 C#编译器在哪里?--在C:\Windows\Microsoft.N ...
- MySQL必会的28条经典查询
MySQL必会的28条经典查询 原创作品.转载请注明出处https://blog.csdn.net/kk123k 表结构及测试数据请看我上一篇文章:学生选修成绩表测试数据 Student(Sno, ...
- 【BZOJ】【3437】小P的牧场
DP/斜率优化 斜率优化基本题……等等,好像就没啥变化啊= = 嗯目测这题跟仓库建设差不多?写题的时候倒是没想这么多……直接推了公式. $$f[i]=min\{f[j]+cal(j,i)+a[i]\} ...
- Spark一个简单案例
Spark是一个类似Map-Reduce的集群计算框架,用于快速进行数据分析. 在这个应用中,我们以统计包含"the"字符的行数为案例,.为建立这个应用,我们使用 Spark 1. ...
- Informatica 常用组件Lookup缓存之一 概述
可以配置查找转换以高速缓存查找表.PowerCenter 将在处理高速缓存查找转换中的第一个数据行时在存储器中建立高速缓存.它将根据您在转换或会话特性中配置的数量来分配高速缓存区内存.PowerCen ...
- Zabbix named 监控
Named 监控 配置 named.conf ,在options 配置节增加如下配置(同时检查/var/named 文件夹权限),然后重启named服务. tatistics-file "/ ...
- Javascript执行效率总结
Javascript是一门非常灵活的语言,我们可以随心所欲的书写各种风格的代码,不同风格的代码也必然也会导致执行效率的差异,开发过程中零零散散地接触到许多提高代码性能的方法,整理一下平时比较常见并且容 ...
- 【用jQuery来判断浏览器的类型】及【javascript获取用户ip地址】
用jQuery来判断浏览器的类型,主要是使用$.browser这个工具类,使用方法: $.browser.['浏览器关键字'] //谷歌浏览器.360浏览器等其他一些浏览器,没有专门的判断 funct ...