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

Author

xudyh

Source

2015 Multi-University Training Contest 9

Recommend

wange2014

题意:给你一个nxm的迷宫,每个格子有一个非负整数,每次可以从一个格子移动到相邻格子,每个格子只能访问一次,问你从(1,1)到(n,m)经过的数字能够得到的最大和是多少,并且求出移动路径。

思路:由于每个格子是非负整数,当我们经过所有格子时肯定得到的和是最大的,于是问题就变成了我们是否能遍历所有的格子。当n或m为奇数时,显然可以,我们只要一行一行的走或一列一列的走就可以了。当n和m都为偶数时,如果我们把横坐标+纵坐标的和为奇数的点染成黑色,把偶数的点染成白色,那么棋盘上黑白棋子是一样多的,并且起点和终点都为黑色,所以从起点到终点的任意路径必然是黑色棋子比白色棋子多一个,故我们不能遍历所有的格子,一定会剩下一个白格子,为了使和最大,我们剩下值最小的白格子就可以了。

接下来说一下构造路径的过程,如果要跳过的白格子在偶数行,那么我们先用RRRRD和LLLLLD移动到它的上上一行,然后按照DR和UR遍历它的这一行和上一行,遇到它的列时用R跳过即可,接下来的行就按照LLLLLD和RRRRRD模拟就可以了;如果要跳过的白格子在奇数行,我们先移动到它的这一行,然后用同样的方法遍历它的这一行和它的下一行即可。

有一个坑点就是要跳过的格子在最后一列时不用R,直接D就行,WA了一次后及时发现了wwwwwww

代码:

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;

#define PB push_back
#define MP make_pair

#define REP(i,x,n) for(int i=x;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define FORD(i,h,l) for(int i=(h);i>=(l);--i)
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define OI(X) printf("%d",X);
#define RS(X) scanf("%s", (X))
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define Swap(a, b) (a ^= b, b ^= a, a ^= b)
#define Dpoint  strcut node{int x,y}
#define cmpd int cmp(const int &a,const int &b){return a>b;}

 /*#ifdef HOME
    freopen("in.txt","r",stdin);
    #endif*/
const int MOD = 1e9+7;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
//#define HOME

int Scan()
{
	int res = 0, ch, flag = 0;

	if((ch = getchar()) == '-')				//判断正负
		flag = 1;

	else if(ch >= '0' && ch <= '9')			//得到完整的数
		res = ch - '0';
	while((ch = getchar()) >= '0' && ch <= '9' )
		res = res * 10 + ch - '0';

	return flag ? -res : res;
}
/*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/

int a[105][105];
int main()
{
int n,m;
while(RII(n,m)!=EOF)
{
    int sum=0;
    int M=100000;
    int px=1;
    int py=1;
    REP(i,0,n)
    REP(j,0,m)
    {RI(a[i][j]);
    sum+=a[i][j];
    if((i+j)%2==1&&a[i][j]<M)
        {
            M=a[i][j];
            px=i;
            py=j;
        }
    }
    if(n%2==1)
    {
        printf("%d\n",sum);
        for(int i=0;i<n;i++)
        {
            if(i%2==0)
            {  for(int j=0;j<m-1;j++)
            printf("R");
            if(i!=n-1)
                printf("D");

            }
            else
            {
                for(int j=0;j<m-1;j++)
                    printf("L");
                printf("D");
            }
        }
    }
    else
        if(m%2==1)
    {
        printf("%d\n",sum);
        for(int i=0;i<m;i++)
        {
            if(i%2==0)
            {

                    for(int j=0;j<n-1;j++)
                        printf("D");
                    if(i!=m-1)
                        printf("R");

            }
            else
            {
               for(int j=0;j<n-1;j++)
                printf("U");
               printf("R");
            }
        }
    }
    else
    {
      printf("%d\n",sum-M);
      if(px%2==0)
         {

             for(int i=0;i<px;i++)
      {
          if(i%2==0)
            {  for(int j=0;j<m-1;j++)
            printf("R");
            if(i!=n-1)
                printf("D");

            }
            else
            {
                for(int j=0;j<m-1;j++)
                    printf("L");
                printf("D");
            }

      }
             for(int i=0;i<py;i++)
             {
                 if(i%2==0)
                 printf("DR");
                 else
                printf("UR");
             }
             if(py<m-1)
             printf("R");
             else
             {
                 if(px+1!=n-1)
                    printf("D");
             }
             for(int i=py+1;i<m;i++)
             {
                 if(i%2==0)
                    printf("UR");
                else
                {
                    printf("D");
                    if(i==m-1)
                        {   if(px+1!=n-1)
                            printf("D");}
                    else
                        printf("R");
                }
             }
             for(int i=px+2;i<n;i++)
             {
                 if(i%2==0)
                 {
                     for(int j=0;j<m-1;j++)
                        printf("L");
                     printf("D");

                 }
                 else
                 {
                     for(int j=0;j<m-1;j++)
                        printf("R");
                     if(i!=n-1)
                        printf("D");
                 }
             }

         }
         else
         {
                for(int i=0;i<px-1;i++)
             {
                 if(i%2==0)
            {  for(int j=0;j<m-1;j++)
            printf("R");
            if(i!=n-1)
                printf("D");

            }
            else
            {
                for(int j=0;j<m-1;j++)
                    printf("L");
                printf("D");
            }

             }

             for(int i=0;i<py;i++)
             {
                 if(i%2==0)
                 {
                     printf("DR");
                 }
                 else
                    printf("UR");
             }
             printf("R");
             for(int i=py+1;i<m;i++)
             {
                 if(i%2==0)
                 {
                     printf("UR");
                 }
                 else
                 {
                     printf("D");
                     if(i==m-1)
                     {
                         if(px!=n-1)
                         {
                             printf("D");
                         }
                     }
                     else
                     {
                         printf("R");
                     }
                 }
             }
             for(int i=px+1;i<n;i++)
             {
                 if(i%2==0)
                 {
                     for(int j=0;j<m-1;j++)
                        printf("L");
                     printf("D");
                 }
                 else
                 {
                     for(int j=0;j<m-1;j++)
                        printf("R");
                     if(i!=n-1)
                        printf("D");
                 }
             }
         }
    }
    printf("\n");

}

        return 0;
}

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

时间: 2025-01-08 01:15:49

hdu5402 Travelling Salesman Problem(棋盘染色+模拟)的相关文章

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 th

[hdu5402 Travelling Salesman Problem]YY

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

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

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

构造 - 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: 首先,因为每个格子都是非负整数,而且规定每个格子只能走一次,所以为了使和尽可能大,必定是走的格子数越多越好.这样我们就需

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

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

【HDOJ 5402】Travelling Salesman Problem

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

多校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

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