hdu-5402(多校2015)

Travelling Salesman Problem

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 258    Accepted Submission(s): 95
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
题意:给一个n*m的正整数棋盘,求从(1,1)走到(n,m)路径和最大,并输出一组走法。
思路:
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<stdio.h>
#include<math.h>
#include <string>
#include<string.h>
#include<map>
#include<queue>
#include<set>
#include<utility>
#include<vector>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define eps 1e-8
#define inf 0x3f3f3f3f
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define ll long long int
#define mod 1000000007
#define maxn 900006
#define maxm 500010
int f[101][101],n,m;
int main()
{
    while(~scanf("%d%d",&n,&m)){
            int kx=-1;
            int ky=-1;
            int sum=0;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
        {
            rd(f[i][j]);
            sum+=f[i][j];
            if((i+j-2)%2){
                if(kx==-1) kx=i,ky=j;
                else if(f[i][j]<f[kx][ky]) kx=i,ky=j;
            }
        }
        if(n%2||m%2){
            printf("%d\n",sum);
            if(n&1)
            for(int i=1;i<=n;i++)
            {
                if(i!=1) printf("D");
                for(int j=2;j<=m;j++)
                    if(i%2) printf("R");
                else printf("L");
            }
            else if(m&1)
                for(int i=1;i<=m;i++)
            {
                if(i!=1) printf("R");
                for(int j=2;j<=n;j++)
                    if(i%2) printf("D");
                else printf("U");
            }
            printf("\n");
            continue;
        }
        printf("%d\n",sum-f[kx][ky]);
        int k=1;
        while(1){
            if(kx%2&&k==kx) break;
        if(kx%2==0&&(k+1)==kx) break;
            for(int i=2;i<=m;i++)
                if(k%2) printf("R");
            else printf("L");
            printf("D");
            k++;
        }
        if(kx%2){
            int cx=k;
            int cy=1;
            while((cy+1)!=ky){
                printf("DRUR");
                cy+=2;
            }
            printf("DR");cx++;cy++;
            while(cy!=m){
                printf("RURD");cy+=2;
            }
                    //cx--;cy++;
                k+=2;
            }
        else{
            int cx=k;
            int cy=1;
            while(cy!=ky){
                printf("DRUR");
                cy+=2;
            }
            printf("RD");cx++;cy++;
            while(cy!=m) {
                printf("RURD");cy+=2;
            }
            k+=2;
        }
        for(int i=k;i<=n;i++)
        {
            printf("D");
            for(int j=2;j<=m;j++)
                if(i%2) printf("L");
            else printf("R");
        }
        printf("\n");
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值