zoukankan      html  css  js  c++  java
  • HDU 5402 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(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里面有一个是奇数那么所有走遍就好了。

    否则要找一个最小的点不要,这个点的坐标要满足x+y是奇数
    假设不是的话,舍弃该点一定会导致另外一个点也走不到。
    然后找到这个点。暴力的一行一行的跑就好了。

    #pragma comment(linker, "/STACK:1024000000,1024000000") 
    #include<set>
    #include<map>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<cstdio>
    #include<vector>
    #include<string>
    #include<cstring>
    #include<algorithm>
    #include<functional>
    using namespace std;
    typedef long long LL;
    const LL base = 1e9 + 7;
    const int maxn = 105;
    LL T, n, m, a[maxn][maxn], sum, x, y;
    
    inline void read(int &x)
    {
        char ch;
        while ((ch = getchar())<'0' || ch>'9');
        x = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') x = x * 10 + ch - '0';
    }
    
    void get()
    {
        x = 1;    y = 2;
        for (int i = 1; i <= n;i++)
            for (int j = 1; j <= m; j++) 
                if (((i + j) & 1) && a[x][y] > a[i][j]) x = i, y = j;
    }
    
    int main()
    {
        while (scanf("%lld%lld", &n, &m) !=EOF)
        {
            sum = 0;
            for (int i = 1; i <= n;i++)
                for (int j = 1; j <= m; j++)
                {
                    scanf("%lld", &a[i][j]);
                    sum += a[i][j];
                }
            if (n & 1 || m & 1)
            {
                printf("%lld
    ", sum);
                if (n & 1)
                {
                    for (int i = 1; i <= n; i++)
                    {
                        for (int j = 1; j < m; j++) 
                            if (i & 1) printf("R"); else printf("L");
                        if (i < n) printf("D"); else printf("
    ");
                    }
                }
                else
                {
                    for (int i = 1; i <= m; i++)
                    {
                        for (int j = 1; j < n; j++)
                            if (i & 1) printf("D"); else printf("U");
                        if (i < m) printf("R"); else printf("
    ");
                    }
                }
            }
            else
            {
                get();
                printf("%lld
    ", sum - a[x][y]);
                for (int i = 1; i <= n; i += 2)
                {
                    if (x == i || x == i + 1)
                    {
                        for (int j = 1; j < y; j++)
                        {
                            if (j & 1) printf("D"); else printf("U");
                            printf("R");
                        }
                        if (y < m) printf("R");
                        for (int j = y + 1; j <= m; j++)
                        {
                            if (j & 1) printf("U"); else printf("D");
                            if (j < m) printf("R");
                        }
                        if (i < n - 1) printf("D");
                    }
                    else if (i < x)
                    {
                        for (int j = 1; j < m; j++) printf("R");
                        printf("D");
                        for (int j = 1; j < m; j++) printf("L");
                        printf("D");
                    }
                    else
                    {
                        for (int j = 1; j < m; j++) printf("L");
                        printf("D");
                        for (int j = 1; j < m; j++) printf("R");
                        if (i < n - 1) printf("D");
                    }
                }
                printf("
    ");
            }
        }
        return 0;
    }


  • 相关阅读:
    实时获取阿里旺旺聊天记录,实时获取千牛聊天记录
    千牛hook 旺旺hook,旺旺发消息call,千牛发消息call,千牛机器人,破解旺旺发消息代码,破解千牛发消息代码,反汇编旺旺发消息,反汇编千牛发消息,旺旺发消息组件,千牛发消息组件
    hook千牛 千牛破解发消息 千牛机器人 千牛发消息组件 调用千牛发消息 实时获取千牛聊天记录 可以提供代码
    [转发]分布式事务,这一篇就够了
    C++之throw以及try{}...catch{}【转载】
    C++之Effective C++学习-条款2
    c++中为什么析构函数要被设置为虚函数(virtual)
    c++中在声明静态变量时,使用const可直接初始化,不在需要定义式
    js检测浏览器类型_js检测是否为火狐浏览器
    PHP8.0 JIT 配置
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/6853156.html
Copyright © 2011-2022 走看看