码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5402 Travelling Salesman Problem(棋盘染色 构造 多校啊)

时间:2015-08-18 22:55:28      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:hdu   多校2015   构造   棋盘染色   

题目链接: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(1n,m100,n?m2).

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的迷宫,每一个格子都有一个非负整数,一个人要从迷宫的左上角(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   多校2015   构造   棋盘染色   

原文地址:http://blog.csdn.net/u012860063/article/details/47759773

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!