当前位置: 首页 > news >正文

HDU 5402 Travelling Salesman Problem(棋盘染色 构造 多校啊)

HDU 5402 Travelling Salesman Problem(棋盘染色 构造 多校啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5402


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
2015 Multi-University Training Contest 9

题意:

有一个n*m的迷宫,每个格子都有一个非负整数,一个人要从迷宫的左上角(1,1)走到迷宫的右下角(n,m)。

并且要使他经过的路径的和最大,

求最大和为多少而且输出他所走的路径。


官方题解:

首先假设nn为奇数或者mm为奇数,那么显然能够遍历整个棋盘。

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

构造方法是这样,首先RRRRDLLLLD这种路径走到这个格子所在行或者上一行。然后DRUR这样走到这个格子的所在列或者前一列。然后绕过这个格子。

然后走完这两行,接着按LLLLDRRRR这种路径往下走。

再贴一个解说非常具体的链接:http://blog.csdn.net/queuelovestack/article/details/47756605

代码例如以下:

#include <cstdio>
int n, m;
int a[117][117];
int sum;
int min_x, min_y;

void build_1()//都是偶数
{
    printf("%d\n",sum-a[min_x][min_y]);
    
    for(int i = 1; i <= n; i+=2)
    {
        if(min_x==i || min_x==i+1)
        {
            for(int j = 1; j < min_y; j++)
            {
                if(j&1)
                    printf("D");
                else
                    printf("U");
                printf("R");
            }
            if(min_y < m)
                printf("R");
            for(int j = min_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(min_x > i)//上面
        {
            for(int j = 1; j < m; j++)
                printf("R");
            printf("D");
            for(int j = m; j > 1; j--)
                printf("L");
            printf("D");
        }
        else//以下
        {
            for(int j = m; j > 1; j--)
                printf("L");
            printf("D");
            for(int j = 1; j < m; j++)
                printf("R");
            if(i < n-1)
                printf("D");
        }
    }
    printf("\n");
}
void build_2()
{
    printf("%d\n",sum);
    
    if(n&1)//假设n是奇数,先走偶数方向
    {
        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("\n");
        }
    }
    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("\n");
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        sum=0;
        min_x = 1;
        min_y = 2;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                scanf("%d",&a[i][j]);
                sum+=a[i][j];
                if(((i+j)&1) && (a[min_x][min_y]>a[i][j]))//坐标和为奇数且最小
                {
                    min_x = i;
                    min_y = j;
                }
            }
        }
        if(!(n&1) && !(m&1))//都是偶数
        {
            build_1();
        }
        else
        {
            build_2();
        }
    }
    return 0;
}
/*
3 3
2 3 3
3 3 3
3 3 2
*/


posted on 2017-08-11 14:36 mthoutai 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/mthoutai/p/7345834.html

相关文章:

  • oracle归档日志增长过快处理方法
  • 对WebSocket技术的学习与探索(二)
  • noip 2016 Day T1 组合数
  • Python装饰器主要用法
  • JMeter学习-004-WEB脚本入门实战
  • JDBC的链接及封装
  • 对于盒模型的宽高获取填坑
  • 普通程序员如何入门AI
  • 技术分享之AQS——内容提要
  • ansible基本使用教程
  • Kafka【第一篇】Kafka集群搭建
  • vue2.0 新手教程(一)
  • POJ 3104 Drying 二分
  • hihocoder-1546-集合计数
  • JTemplates + $.Ajax
  • (ckeditor+ckfinder用法)Jquery,js获取ckeditor值
  • [js高手之路]搞清楚面向对象,必须要理解对象在创建过程中的内存表示
  • GitUp, 你不可错过的秀外慧中的git工具
  • Joomla 2.x, 3.x useful code cheatsheet
  • Swoft 源码剖析 - 代码自动更新机制
  • 飞驰在Mesos的涡轮引擎上
  • 记一次删除Git记录中的大文件的过程
  • 如何在GitHub上创建个人博客
  • 入门到放弃node系列之Hello Word篇
  • 它承受着该等级不该有的简单, leetcode 564 寻找最近的回文数
  • 详解NodeJs流之一
  • 在Docker Swarm上部署Apache Storm:第1部分
  • Hibernate主键生成策略及选择
  • ​​快速排序(四)——挖坑法,前后指针法与非递归
  • # 数据结构
  • (52)只出现一次的数字III
  • (动手学习深度学习)第13章 计算机视觉---微调
  • (三分钟了解debug)SLAM研究方向-Debug总结
  • (转)LINQ之路
  • .net 8 发布了,试下微软最近强推的MAUI
  • .net core 6 使用注解自动注入实例,无需构造注入 autowrite4net
  • .net core开源商城系统源码,支持可视化布局小程序
  • .NET Framework 4.6.2改进了WPF和安全性
  • .NET gRPC 和RESTful简单对比
  • .net 获取url的方法
  • .Net 中的反射(动态创建类型实例) - Part.4(转自http://www.tracefact.net/CLR-and-Framework/Reflection-Part4.aspx)...
  • .Net6 Api Swagger配置
  • .so文件(linux系统)
  • @AliasFor注解
  • @serverendpoint注解_SpringBoot 使用WebSocket打造在线聊天室(基于注解)
  • [.net]官方水晶报表的使用以演示下载
  • [acwing周赛复盘] 第 69 场周赛20220917
  • [Angular] 笔记 6:ngStyle
  • [c#基础]值类型和引用类型的Equals,==的区别
  • [C++]命名空间等——喵喵要吃C嘎嘎
  • [Codeforces] combinatorics (R1600) Part.2
  • [Fri 26 Jun 2015 ~ Thu 2 Jul 2015] Deep Learning in arxiv
  • [HCIE] IPSec-VPN (手工模式)
  • [HTML]HTML5实现可编辑表格
  • [HTML]Web前端开发技术12(HTML5、CSS3、JavaScript )——喵喵画网页