HDU 5402 Travelling Salesman Problem(棋盘染色 构造 多校啊)
2015-08-18 21:09
459 查看
题目链接: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
Sample Output
Author
xudyh
Source
2015 Multi-University Training Contest 9
题意:
有一个n*m的迷宫,每一个格子都有一个非负整数,一个人要从迷宫的左上角(1,1)走到迷宫的右下角(n,m),
而且要使他经过的路径的和最大,
求最大和为多少并且输出他所走的路径。
官方题解:
首先如果nn为奇数或者mm为奇数,那么显然可以遍历整个棋盘。
如果n,mn,m都为偶数,那么讲棋盘黑白染色,假设(1,1)(1,1)和(n,m)(n,m)都为黑色,那么这条路径中黑格个数比白格个数多11,而棋盘中黑白格子个数相同,所以必然有一个白格不会被经过,所以选择白格中权值最小的不经过。
构造方法是这样,首先RRRRDLLLLD这样的路径走到这个格子所在行或者上一行,然后DRUR这样走到这个格子的所在列或者前一列,然后绕过这个格子。然后走完这两行,接着按LLLLDRRRR这样的路径往下走。
再贴一个讲解很详细的链接:/article/2498926.html
代码如下:
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
2015 Multi-University Training Contest 9
题意:
有一个n*m的迷宫,每一个格子都有一个非负整数,一个人要从迷宫的左上角(1,1)走到迷宫的右下角(n,m),
而且要使他经过的路径的和最大,
求最大和为多少并且输出他所走的路径。
官方题解:
首先如果nn为奇数或者mm为奇数,那么显然可以遍历整个棋盘。
如果n,mn,m都为偶数,那么讲棋盘黑白染色,假设(1,1)(1,1)和(n,m)(n,m)都为黑色,那么这条路径中黑格个数比白格个数多11,而棋盘中黑白格子个数相同,所以必然有一个白格不会被经过,所以选择白格中权值最小的不经过。
构造方法是这样,首先RRRRDLLLLD这样的路径走到这个格子所在行或者上一行,然后DRUR这样走到这个格子的所在列或者前一列,然后绕过这个格子。然后走完这两行,接着按LLLLDRRRR这样的路径往下走。
再贴一个讲解很详细的链接:/article/2498926.html
代码如下:
#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
*/
相关文章推荐
- Poj 1061青蛙的约会 扩展欧几里德
- 【分享】纯爱调教神作 萝莉调教计划NaturalAnotherOneNatural Another One+Natural Another One 2nd
- MyBatis 基础总结
- 生产环境中跨节点隔离网络构架解析
- POSIX 螺纹具体解释(1-概要)
- 学习算法-基数排序(radix sort)卡片分类(card sort) C++数组实现
- HandlerThread 的用法 和Thread 的区别
- 第88讲:Scala中使用For表达式实现map、flatMap、filter学习笔记
- HDU2119Matrix(最小点集覆盖)
- HDOJ 1312 Red and Black (深搜 DFS)
- GDOI2016模拟8.18的士
- 数据结构基本概念
- android 多类型表单实现
- 拷贝构造函数的参数为什么是引用
- Oracle ADRCI工具的使用
- Genealogical tree POJ 2367【拓扑排序】
- 两个瓶盖换一瓶可乐问题
- 常对象只能调用常函数成员 c++
- amman D.Alternating Strings
- PHP