hdu5402 Travelling Salesman Problem

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(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

第一想法是搜多,但看到100*100的大小,感觉不是搜索题,后来发现是模拟题。如果n或m有一个是奇数,那么一定可以把所有的点都走一遍,这样结果一定是最大的,所以只要考虑都是偶数的情况。我的思路是先把横纵坐标只和为奇数的染成黑色,偶数的染成白色,那么画图可以知道

如果选择一个黑色方格不走,那么其他点都能够走一遍,但如果选择一个白色方格不走,必须要不走另外两个黑色方格才能走到终点,所以 一定是选择最小的黑色方格数不走。

然后选出最小的黑色方格,然后模拟一下走法就行了。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define inf 99999999
int gra[106][106],n,m,sum;
void jishulu()
{
    int i,j;
    printf("%d\n",sum);
    if(n%2==1){
        for(i=1;i<=n;i++){
            for(j=1;j<=m-1;j++){
                printf("%c",i%2==1?'R':'L');
            }
            if(i<n)printf("D");
            else printf("\n");
        }
    }
    else{
        for(j=1;j<=m;j++){
            for(i=1;i<=n-1;i++){
                printf("%c",j%2==1?'D':'U');
            }
            if(j<m)printf("R");
            else printf("\n");
        }
    }
}

void oushulu()
{
    int i,j,minx=inf,dx=0,dy=0,x,y;
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            if((i+j)%2==1 && minx>gra[i][j]){
                dx=i;dy=j;minx=gra[i][j];
            }
        }
    }
    printf("%d\n",sum-minx);
    if(dx==n){
        for(i=1;i<=n-2;i++){
            for(j=1;j<=m-1;j++){
                printf("%c",i%2==1?'R':'L');
            }
            printf("D");
        }
        if(dy==1){
            x=n-1;y=1;
            while(1)
            {
                printf("RD");x++;y++;
                if(x==n && y==m){
                    printf("\n");break;
                }
                printf("RU");x--;y++;
            }
        }
        else{
            printf("D");
            x=n;y=1;
            while(1)
            {
                printf("RU");
                x--;y++;
                if(x+1==dx && y+1==dy){
                    printf("RRD");x++;y+=2;
                }
                else{
                    printf("RD");x++;y++;
                }
                if(x==n && y==m){
                    printf("\n");break;
                }
            }
        }
    }
    else if(dx!=n){
        for(i=1;i<=dx-1;i++){
            for(j=1;j<=m-1;j++){
                printf("%c",i%2==1?'R':'L');
            }
            printf("D");
        }
        if(dx%2==1){
           printf("D");
           x=dx+1;y=1;
           if(dy==m){
              while(1)
              {
                  if((y+1)!=m){
                    printf("RURD");y+=2;
                  }
                  else{
                     printf("R");y++;
                     if(x!=n)printf("D");break;
                  }
              }
           }
           else{
              while(1)
              {
                if(x-1==dx && y+1==dy){
                   printf("RRURD");y+=3;
                }
                else{
                   printf("RURD");y+=2;
                }
                if(y==m){
                    if(x!=n)printf("D");
                    break;
                }
              }
           }
           for(i=dx+2;i<=n;i++){
              for(j=1;j<=m-1;j++){
                 printf("%c",i%2==1?'L':'R');
              }
              if(i!=n)printf("D");
           }
           printf("\n");

        }
        else if(dx%2==0){
            printf("D");
            x=dx+1;y=m;
            if(dy==1){
              while(1)
              {
                  if((y-1)!=1){
                    printf("LULD");y-=2;
                  }
                  else{
                     printf("L");y--;
                     if(x!=n)printf("D");
                     break;
                  }
              }
           }
           else{
              while(1)
              {
                if(x-1==dx && y-1==dy){
                   printf("LLULD");y-=3;
                }
                else{
                   printf("LULD");y-=2;
                }
                if(y==1){
                    if(x!=n)printf("D");
                    break;
                }
              }
           }
           if(x!=n){
              for(i=dx+2;i<=n;i++){
                 for(j=1;j<=m-1;j++){
                   printf("%c",i%2==1?'L':'R');
                 }
                 if(i!=n)
                 printf("D");
              }
           }
           printf("\n");

        }
    }
}

int main()
{
    int i,j;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        sum=0;
        for(i=1;i<=n;i++){
            for(j=1;j<=m;j++){
                scanf("%d",&gra[i][j]);
                sum+=gra[i][j];
            }
        }
        if(n%2==1 || m%2==1){
            jishulu();
        }
        else oushulu();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-02 01:44:53

hdu5402 Travelling Salesman Problem的相关文章

hdu5402 Travelling Salesman Problem(棋盘染色+模拟)

题目: Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 906    Accepted Submission(s): 331 Special Judge Problem Description Teacher Mai is in a maze with n rows and m c

[hdu5402 Travelling Salesman Problem]YY

题意:给一个n*m的矩形,每个格子有一个非负数,求一条从(1,1)到(n,m)的路径(不能经过重复的格子),使得经过的数的和最大,输出具体的方案 思路:对于row为奇数的情况,一行行扫下来即可全部走完得到最大和,对于col为奇数的情况一列列扫即可.对于行和列全部为偶数的情况,将所有格子进行黑白染色,起点和终点的颜色一样,而路径上的颜色是交替的,说明总有一个点不能走到,枚举得到不可到点上的最小值,总和减去就是答案.具体的方案构造方法如下:由于只有一个格子被挖掉不能走,考虑整行或整列的走,走完这个格

构造 - HDU 5402 Travelling Salesman Problem

Travelling Salesman Problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5402 Mean: 现有一个n*m的迷宫,每一个格子都有一个非负整数,从迷宫的左上角(1,1)到迷宫的右下角(n,m),并且使得他走过的路径的整数之和最大,问最大和为多少以及他走的路径. analyse: 首先,因为每个格子都是非负整数,而且规定每个格子只能走一次,所以为了使和尽可能大,必定是走的格子数越多越好.这样我们就需

HDOJ 5402 Travelling Salesman Problem 模拟

行数或列数为奇数就能够所有走完. 行数和列数都是偶数,能够选择空出一个(x+y)为奇数的点. 假设要空出一个(x+y)为偶数的点,则必须空出其它(x+y)为奇数的点 Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 747    Accepted Submission(s): 272

多校9 1007 Travelling Salesman Problem

Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 829    Accepted Submission(s): 182Special Judge Problem Description Teacher Mai is in a maze with n rows and m columns

【HDOJ 5402】Travelling Salesman Problem

[HDOJ 5402]Travelling Salesman Problem 一开始以为是搜索 仔细画了画发现就一模拟 奇数行或奇数列的时候怎么走都能全走完 偶数行偶数列的时候就要挑了 . * . * . * * . * . * . . * . * . * * . * . * . 以4*6为例(如上图 星号可以保证不取其中一个可遍历完全图 点好的话就会连带一些星号 所以绕过星号中的最小值 是最佳遍历方式 输入的时候找到最小值并记录下行列 遍历到改行前以 右走到头 下 左走到头 下 右走到头这种方

HDU 5402 Travelling Salesman Problem (模拟 有规律)

Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 568    Accepted Submission(s): 200 Special Judge Problem Description Teacher Mai is in a maze with n rows and m colum

HDU 5402 Travelling Salesman Problem (构造)(好题)

大致题意:n*m的非负数矩阵,从(1,1) 只能向四面走,一直走到(n,m)为终点,路径的权就是数的和,输出一条权值最大的路径方案 思路:由于这是非负数,要是有负数就是神题了,要是n,m中有一个是奇数,显然可以遍历,要是有一个偶数,可以画图发现,把图染成二分图后,(1,1)为黑色,总能有一种构造方式可以只绕过任何一个白色的点,然后再遍历其他点,而绕过黑色的点必然还要绕过两个白色点才能遍历全部点,这是画图发现的,所以找一个权值最小的白色点绕过就可以了, 题解给出了证明: 如果n,mn,m都为偶数,

PAT 甲级 1150 Travelling Salesman Problem

https://pintia.cn/problem-sets/994805342720868352/problems/1038430013544464384 The "travelling salesman problem" asks the following question: "Given a list of cities and the distances between each pair of cities, what is the shortest possib