2015 Multi-University Training Contest 9 1007

Travelling Salesman Problem

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 477    Accepted Submission(s): 159
Special Judge


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
 
题意:一个矩阵NxM,起点为(1,1),终点为(N,M),每经过一个点,就加上那个点的值,求到终点时总和最大值是多少,并打印路径,矩阵中不会出现负数。
分析:矩阵中不会出现负数,搜索会超时,所以模拟。
那么最好的情况肯定是全部都得到,这种情况分析一下发现只有N或者M至少有为奇数是才能实现。
剩下的情况就是N和M全为偶数的情况,官方题解:

如果N,M都为偶数,那么讲棋盘黑白染色,假设(1,1)(n,m)都为黑色,那么这条路径中黑格个数比白格个数多1

而棋盘中黑白格子个数相同,所以必然有一个白格不会被经过,所以选择白格中权值最小的不经过。

构造方法是这样,首先RRRRDLLLLD这样的路径走到这个格子所在行或者上一行,然后DRUR这样走到这个格子的所在列或者前一列,然后绕过这个格子。

然后走完这两行,接着按LLLLDRRRR这样的路径往下走。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<string>
#include<iostream>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<stdlib.h>
#include<algorithm>
#define LL __int64
#define FIN freopen("in.txt","r",stdin)
using namespace std;
const int MAXN=100+5;
const int INF=0x3f3f3f3f;
int a[MAXN][MAXN];
int n,m,ans,minn;
int x,y;

int main()
{
    //FIN;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        memset(a,0,sizeof(a));
        ans=0,minn=INF;

        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&a[i][j]);
                ans+=a[i][j];
                if(((i+j)&1) && a[i][j]<minn)
                {
                    minn=a[i][j];
                    x=i,y=j;
                }
            }
        }
        if(n&1)
        {
            printf("%d\n",ans);
            int inv=1;
            for(int i=1;i<=n;i++)
            {
                if(inv)
                {
                    for(int j=1;j<m;j++) printf("R");
                    inv=!inv;
                }
                else
                {
                    for(int j=1;j<m;j++) printf("L");
                    inv=!inv;
                }
                if(i!=n) printf("D");
            }
            printf("\n");
        }
        else if(m&1)
        {
            printf("%d\n",ans);
            int inv=1;
            for(int i=1;i<=m;i++)
            {
                if(inv)
                {
                    for(int j=1;j<n;j++) printf("D");
                    inv=!inv;
                }
                else
                {
                    for(int j=1;j<n;j++) printf("U");
                    inv=!inv;
                }
                if(i!=m) printf("R");
            }
            printf("\n");
        }
        else
        {
            printf("%d\n",ans-minn);
            for(int i=1;i<=n;i+=2)
            {
                if(i==x || i+1==x)
                {
                    int inv=1;
                    for(int j=1;j<y;j++)
                    {
                        if(inv)
                        {
                            printf("D");
                            inv=!inv;
                        }
                        else
                        {
                            printf("U");
                            inv=!inv;
                        }
                        printf("R");
                    }
                    if(y<m) printf("R");
                    for(int j=y+1;j<=m;j++)
                    {
                        if(inv)
                        {
                            printf("D");
                            inv=!inv;
                        }
                        else
                        {
                            printf("U");
                            inv=!inv;
                        }
                        if(j<m) printf("R");
                    }
                    if(i<n-1) printf("D");
                }
                else if(x<i)
                {
                    int inv=1;
                    for(int j=1;j<m;j++) printf("L");
                    printf("D");
                    for(int j=1;j<m;j++) printf("R");
                    if(i<n-1) printf("D");
                }
                else
                {
                    for(int j=1;j<m;j++) printf("R");
                    printf("D");
                    for(int j=1;j<m;j++) printf("L");
                    printf("D");
                }
            }
            printf("\n");
        }
    }
    return 0;
}
View Code

 

 

转载于:https://www.cnblogs.com/clliff/p/4740949.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值