Travelling Salesman Problem (hdu 5402 模拟)

4 篇文章 0 订阅

Travelling Salesman Problem

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 131    Accepted Submission(s): 52
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
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5405  5404  5403  5401  5400 



题意:n*m的格子,每个格子上有权值,求从(1,1)走到(n,m)所经过的格子权值之和最大为多少,并输出路径。

思路:直接贴上题解,当时想的和它一样,就是bug得蛋疼。

首先如果nn为奇数或者mm为奇数,那么显然可以遍历整个棋盘。

如果n,mn,m都为偶数,那么讲棋盘黑白染色,假设(1,1)(1,1)(n,m)(n,m)都为黑色,那么这条路径中黑格个数比白格个数多11,而棋盘中黑白格子个数相同,所以必然有一个白格不会被经过,所以选择白格中权值最小的不经过。

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

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
typedef long long LL;

const int maxn=111;

int n,m,sum;
int Min,sx,sy;
int mp[maxn][maxn];

void print_1(int r,int c)
{
    for (int i=0;i<r/2;i++)
    {
        for (int j=0;j<c-1;j++)
            printf("R");
        printf("D");
        for (int j=0;j<c-1;j++)
            printf("L");
        printf("D");
    }
    for (int i=0;i<c-1;i++)
        printf("R");
}

void print_2(int r,int c)
{
    for (int i=0;i<c/2;i++)
    {
        for (int j=0;j<r-1;j++)
            printf("D");
        printf("R");
        for (int j=0;j<r-1;j++)
            printf("U");
        printf("R");
    }
    for (int i=0;i<r-1;i++)
        printf("D");
}

void out1(int r,int c)
{
    for (int i=0;i<r;i+=2)
    {
        for (int j=0;j<c-1;j++)
            printf("R");
        printf("D");
        for (int j=0;j<c-1;j++)
            printf("L");
        printf("D");
    }
}

void out2_1(int c,int x,int y)
{

    for (int i=0;i<y-2;i+=2)
        printf("DRUR");
    printf("DR");
    if (y!=c) printf("R");
    for (int i=0;i<c-y;i+=2)
    {
        printf("URD");
        if (i!=c-y-2) printf("R");
    }
    if (x+1!=n) printf("D");
}

void out2_2(int c,int x,int y)
{
    for (int i=0;i<y/2;i++)
        printf("DRUR");
    printf("RD");
    for (int i=0;i<(c-y)/2;i++)
        printf("RURD");
    if (x!=n) printf("D");
}

void out3(int r,int c)
{
    for (int i=0;i<r;i+=2)
    {
        for (int j=0;j<c-1;j++)
            printf("L");
        printf("D");
        for (int j=0;j<c-1;j++)
            printf("R");
        if (i!=r-2) printf("D");
    }
}

int main()
{
    int i,j;
    while (~scanf("%d%d",&n,&m))
    {
        sum=0;Min=INF;
        for (i=1;i<=n;i++)
        {
            for (j=1;j<=m;j++)
            {
                scanf("%d",&mp[i][j]);
                sum+=mp[i][j];
                if ((i+j)%2)
                {
                    if (Min>mp[i][j])
                    {
                        Min=mp[i][j];
                        sx=i;sy=j;
                    }
                }
            }
        }
        if (n%2)
        {
            printf("%d\n",sum);
            print_1(n,m);
        }
        else if (m%2)
        {
            printf("%d\n",sum);
            print_2(n,m);
        }
        else
        {
            printf("%d\n",sum-Min);
            int r,rr;
            if (sx%2)
            {
                r=sx-1;
                rr=n-sx-1;
            }
            else
            {
                r=sx-2;
                rr=n-sx;
            }
            out1(r,m);
            if (sx%2) out2_1(m,sx,sy);
            else out2_2(m,sx,sy);
            out3(rr,m);
        }
        printf("\n");
    }
    return 0;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 、 1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READmE.文件(md如有),本项目仅用作交流学习参考,请切勿用于商业用途。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值