《ACM国际大学生程序设计竞赛题解Ⅰ》——基础编程题

Posted 黑大帅之家

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《ACM国际大学生程序设计竞赛题解Ⅰ》——基础编程题相关的知识,希望对你有一定的参考价值。

  这个专栏开始介绍一些《ACM国际大学生程序设计竞赛题解》上的竞赛题目,读者可以配合zju的在线测评系统提交代码(今天zoj貌似崩了)。

  其实看书名也能看出来这本书的思路,就是一本题解书,简单暴力的通过题目的堆叠来提升解决编程问题的能力。

  那么下面开始探索吧。

 

  zoj1037:

 

Background
For years, computer scientists have been trying to find efficient solutions to different computing problems. For some of them efficient algorithms are already available, these are the "easy" problems like sorting, evaluating a polynomial or finding the shortest path in a graph. For the "hard" ones only exponential-time algorithms are known. The traveling-salesman problem belongs to this latter group. Given a set of N towns and roads between these towns, the problem is to compute the shortest path allowing a salesman to visit each of the towns once and only once and return to the starting point.

Problem
The president of Gridland has hired you to design a program that calculates the length of the shortest traveling-salesman tour for the towns in the country. In Gridland, there is one town at each of the points of a rectangular grid. Roads run from every town in the directions North, Northwest, West, Southwest, South, Southeast, East, and Northeast, provided that there is a neighbouring town in that direction. The distance between neighbouring towns in directions North-South or East-West is 1 unit. The length of the roads is measured by the Euclidean distance. For example, Figure 7 shows 2 * 3-Gridland, i.e., a rectangular grid of dimensions 2 by 3. In 2 * 3-Gridland, the shortest tour has length 6.

  题目大意:给出一个nxm的矩阵,现在从某点开始遍历所有点并回到起始点,问最少的遍历路程是多少?(从某点出发有8个方向,行上相邻的点之间的距离是1。)

  数理分析:其实这道题目的描述非常有误导性,多次强调所谓“旅行销售员问题”,也就是当现在还没有得到很好解决的著名的TSP问题,这就很容易将人带向很混乱的思维。但是这道题目基于比较特殊的矩阵图,我们应该能够更加简单的方法。

  我们首先应该考虑到的一个贪心策略,行走距离为1的利益最大化就是访问到了一个节点(当然不算起始节点),那么如果有这样一种方案使得我们,遍历距离+1,都会导致多访问了1个节点,那么这最终一定会导致最小的路程,即节点个数mn。

  那么下面我们所关注的问题便是,是否存在这样一个策略呢?如果m列是偶数,那么连接相邻节点形成小正方格,就会形成奇数列个小方格,这使得我们能够有一个走“S”型的锯齿状的遍历策略,能够使我们完成贪心策略。

  即我们能够得到结论,如果m、n当中有一个是偶数,最小距离都是mn。

  那么如果m、n都是奇数呢?依然从m列入手,我们基于(m-1)xn这样一个矩阵,那么我们可以采取和上面类似的思路进行贪心化的遍历,由此我们其实能够看到,对于这种情况,是没有一种路程为mn的便利方案的,但对于剩下的两列,一开始我们令遍历路线走一个长为1.41的“斜边”,然后由原来的纵向S型变成横向S型,即路程为mn + 0.41,这是除去路程为mn的最优方案。

  上文给出的文字描述可能过于抽象,读者可以自行画图感受一下。

  综合起来我们能够得到这道问题线性算法:

  m、n存在一个偶数,结果是mn。

  否自,结果是mn + 0.41.

   

 

以上是关于《ACM国际大学生程序设计竞赛题解Ⅰ》——基础编程题的主要内容,如果未能解决你的问题,请参考以下文章

数学 题解《ACM国际大学生程序设计竞赛题目与解读》

红书《题目与解读》第一章 数学 题解《ACM国际大学生程序设计竞赛题目与解读》

《ACM国际大学生程序设计竞赛题解I》——6.10

《ACM国际大学生程序设计竞赛题解I》——6.8

《ACM国际大学生程序设计竞赛题解I》——6.11

2018 ACM 国际大学生程序设计竞赛上海大都会部分题解