"ACM International College Student Program Design Competition Ⅰ"--BASIC programming problem

Source: Internet
Author: User

This column starts to introduce some of the contest topics in the ACM International College Student Program Design Competition, where readers can submit code with ZJU's online evaluation system (Zoj seems to have collapsed today).

In fact, the title can be seen in the book's Thinking, is a book, simple violence through the stack of topics to improve the ability to solve programming problems.

So let's start exploring.

zoj1037:

Background
For years, computer scientists has been trying to find efficient solutions to different computing problems. For some of them efficient algorithms is already available, these is the ' easy ' problems like sorting, evaluating a poly Nomial or finding the shortest path in a graph. For the ' hard ' ones only exponential-time algorithms is 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 V Isit each of the towns once and is once and return to the starting point.

Problem
The president of Gridland have 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 are one town at each of the points of a rectangular grid. Roads run from every to the directions North, Northwest, West, Southwest, south, southeast, East, and northeast, Prov IDed that there is a neighbouring the town of that direction. The distance between neighbouring towns in directions North-south or east-west are 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.

The main topic: Give a NXM matrix, now from a point to start traversing all points and back to the starting point, ask the minimum traversal distance is how much? (There are 8 directions from a point, and the distance between the adjacent points on the line is 1.) )

Mathematical analysis: In fact, the description of the topic is very misleading, many times stressed the so-called "travel salesman Problem", that is, when it has not been well solved the famous TSP problem, it is easy to bring people to a very chaotic thinking. But the topic is based on a more specific matrix, and we should be able to do it in a simpler way.

We should first consider a greedy strategy, walking distance of 1 of the benefit maximization is access to a node (of course, not the starting node), then if there is a scheme that allows us to traverse the distance of +1, will lead to more than 1 nodes, then this will eventually lead to the smallest distance, that is, the number of nodes MN.

So the question we are concerned about is whether there is such a strategy? If the M-column is an even number, then connecting the adjacent nodes to form a small square, it will form an odd series of small squares, which allows us to have a "S" type of zigzag traversal strategy, we can complete the greedy strategy.

That is, we can get the conclusion that if one of M, N is an even number, the minimum distance is MN.

So what if M and n are odd? Still from M. in hand, we are based on (m-1) xn such a matrix, then we can take and the above similar ideas to carry out the greedy traversal, so we can actually see that, for this situation, there is no way for the convenience of MN, but for the remaining two columns, At first we made the traverse route a long 1.41 "hypotenuse", then the original longitudinal s-shape into the transverse s-type, that is, the distance is MN + 0.41, which is the best solution to remove the distance of MN.

The text descriptions given above may be too abstract for readers to draw their own pictures and feel them.

Together we can get this problem linear algorithm:

M, N has an even number, and the result is MN.

No self, the result is MN + 0.41.

"ACM International College Student Program Design Competition Ⅰ"--BASIC programming problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.