2015 Multi-University Training Contest 9 G(hdu5402 Travelling Salesman Problem)

roblem 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

题意:现有一个n*m的迷宫(n行m列),每一个格子都有一个非负整数,Teacher Mai想要从迷宫的左上角(1,1)到迷宫的右下角(n,m),并且使得他走过的路径的整数之和最大,问最大和为多少以及他走的路径。

思路:首先知道如果行或列只要有一个是奇数,就可以走完全图,如果行和列都是偶数,那么进行黑白染色,让左上角是黑,依次染色,其实可以发现,无论如何都走不完全图,那么就先看官方题解吧

代码:

const int N = 105;
const int mod = 1000000007;
int s[N][N];
int main()
{
    int n,m,i,j,sum,x,y;
    while(~scanf("%d%d",&n,&m))
    {
        sum=0;x=1,y=2;
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
            {
                scanf("%d",&s[i][j]);
                sum+=s[i][j];
                if(((i+j)&1)&&s[x][y]>s[i][j])
                    x=i,y=j;
            }
        if(n&1||m&1)
        {
            printf("%d\n",sum);
            if(n&1)
            {
                for(i=1;i<=n;i++)
                {
                    for(j=1;j<m;j++)
                        if(i&1)
                            printf("R");
                        else
                            printf("L");
                    if(i<n)
                        printf("D");
                    else
                        puts("");
                }
            }
            else
            {
                for(i=1;i<=m;i++)
                {
                    for(j=1;j<n;j++)
                        if(i&1)
                            printf("D");
                        else
                            printf("U");
                    if(i<m)
                        printf("R");
                    else
                        puts("");
                }
            }
        }
        else
        {
            printf("%d\n",sum-s[x][y]);
            for(i=1;i<=n;i+=2)
            {
                if(x==i||x==i+1)
                {
                    for(j=1;j<y;j++)
                    {
                        if(j&1)
                            printf("D");
                        else
                            printf("U");
                        printf("R");
                    }
                    if(y<m)
                        printf("R");
                    for(j=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(x<i)
                {
                    for(j=1;j<m;j++)
                        printf("L");
                    printf("D");
                    for(j=1;j<m;j++)
                        printf("R");
                    if(i<n-1)
                        printf("D");
                }
                else
                {
                    for(j=1;j<m;j++)
                        printf("R");
                    printf("D");
                    for(j=1;j<m;j++)
                        printf("L");
                    printf("D");
                }
            }
            puts("");
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值