HDU 5402 (构造题) Travelling Salesman Problem

题意

略。

思路

比赛的时候其实已经意识到是一个构造题了。

蓝儿m,n都是偶数的时候搞崩了。sad。。

m,n有一个是奇数不说了,可以走完所有。

两个都是偶数的时候,我们就去找一个最小的值且它的位置坐标和是奇数,然后就绕开这个走。其他都可以走完辣。

参考code:

/*
 #pragma warning (disable: 4786)
 #pragma comment (linker, "/STACK:0x800000")
 */
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <iterator>
#include <utility>
using namespace std;

template< class T > T _abs(T n)
{
    return (n < 0 ? -n : n);
}
template< class T > T _max(T a, T b)
{
    return (!(a < b) ? a : b);
}
template< class T > T _min(T a, T b)
{
    return (a < b ? a : b);
}
template< class T > T sq(T x)
{
    return x * x;
}
template< class T > T gcd(T a, T b)
{
    return (b != 0 ? gcd<T>(b, a%b) : a);
}
template< class T > T lcm(T a, T b)
{
    return (a / gcd<T>(a, b) * b);
}
template< class T > bool inside(T a, T b, T c)
{
    return a<=b && b<=c;
}

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define F(i, n) for(int (i)=0;(i)<(n);++(i))
#define rep(i, s, t) for(int (i)=(s);(i)<=(t);++(i))
#define urep(i, s, t) for(int (i)=(s);(i)>=(t);--(i))
#define repok(i, s, t, o) for(int (i)=(s);(i)<=(t) && (o);++(i))
#define MEM0(addr) memset((addr), 0, sizeof((addr)))
#define MP(x, y) make_pair(x, y)
#define REV(s, e) reverse(s, e)
#define SET(p) memset(pair, -1, sizeof(p))
#define CLR(p) memset(p, 0, sizeof(p))
#define MEM(p, v) memset(p, v, sizeof(p))
#define CPY(d, s) memcpy(d, s, sizeof(s))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define SZ(c) (int)c.size()
#define PB(x) push_back(x)
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pii pair< int, int >
#define psi pair< string, int >
#define ls u << 1
#define rs u << 1 | 1
#define lson l, mid, u << 1
#define rson mid, r, u << 1 | 1
#define debug(x) cout << #x << " = " << x << endl
const int N = 110, M = 510;
const double PI = acos(-1.0);

const int maxn = 110;
const int maxm = maxn * maxn;
const int inf = 1 << 29;
const ll mod = 3221225473;
const double eps = 1e-10;

ll t,m,n,a[maxn][maxn],sum,x,y;
void get(){
    x = 1;y = 2;
    rep(i,1,n){
        rep(j,1,m){
            if( ((i + j) & 1) && a[x][y] > a[i][j]) x = i,y = j;
        }
    }
}
int main(){
    READ("in.txt");
    while(scanf("%lld%lld",&n,&m)!=EOF){
        sum = 0;
        rep(i,1,n){
            rep(j,1,m){
                scanf("%lld",&a[i][j]);
                sum += a[i][j];
            }
        }
        if(n & 1 || m & 1){
            printf("%lld\n", sum);
            if(n & 1){
                rep(i,1,n){
                    rep(j,1,m-1){
                        if(i & 1) printf("R"); else printf("L");
                    }
                    if(i < n) printf("D"); else printf("\n");
                }
            }else{
                rep(i,1,m){
                    rep(j,1,n-1){
                        if(i & 1) printf("D"); else printf("U");
                    }
                    if(i < m) printf("R"); else printf("\n");
                }
            }
        }else{
            get();
            printf("%lld\n",sum - a[x][y]);
            for(int i = 1; i <= n ;i += 2){
                if(x == i || x == i + 1){
                    rep(j,1,y-1){
                        if(j & 1) printf("D"); else printf("U");
                        printf("R");
                    }
                    if(y < m) printf("R");
                    rep(j,y+1,m){
                        if(j & 1 )printf("U"); else printf("D");
                        if(j < m) printf("R");
                    }
                    if(i < n - 1) printf("D");
                }else if( i < x ){
                    rep(j,1,m-1) printf("R");
                    printf("D");
                    rep(j,1,m-1) printf("L");
                    printf("D");
                }else{
                    rep(j,1,m-1) printf("L");
                    printf("D");
                    rep(j,1,m-1) printf("R");
                    if(i < n - 1) printf("D");
                }
            }
            printf("\n");
        }
    }
    return 0;
}

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

时间: 2024-10-26 05:18:43

HDU 5402 (构造题) Travelling Salesman Problem的相关文章

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

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

构造 - 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 (模拟 有规律)

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

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

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

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

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

1150 Travelling Salesman Problem (25 分)

1150 Travelling Salesman Problem (25 分) 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 possible route that visits each city and retu