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


    题目链接: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,nm2).

    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,y1), "R" means you walk to cell (x,y+1), "U" means you walk to cell (x1,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
    ",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("
    ");
    }
    void build_2()
    {
        printf("%d
    ",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("
    ");
            }
        }
        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("
    ");
            }
        }
    }
    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
    */
    


  • 相关阅读:
    四则运算
    Git工具学习整理
    java拦截器中使用的动态代理
    Java动态代理
    Git常用命令
    Maven学习
    ruby调用jenkins API使用
    c#认证考试第四章(国庆作业及其总结)
    C#认证考试第三章
    c#认证考试练习题目
  • 原文地址:https://www.cnblogs.com/mthoutai/p/7345834.html
Copyright © 2020-2023  润新知