Implementation of iqcar II-Solutions

Source: Internet
Author: User

Iqcar games are briefly introduced above. Next we will describe how to use a computer to find its solution.

After learning the data structure, the first thought is to use "Deep Priority Search" or "breadth first ".Algorithm". Is to keep trying every possibility until the solution is reached. Then, output the attempt process.

After carefully observing the picture above, we found that each vehicle is unlikely to be in place (because the car can only move forward and backward, there are only four possibilities for a 3-car, there are five possibilities for a car with a length of 2 ). Then, there will be no more possibilities for these cars to be arranged (the reason is that if there are too many cars, there will be a lot of restrictions between them, because the two cars cannot be squeezed into a grid, if the number of cars is small, the number of cars is small, but the number of cars is small ). In this way, if all the cars are arranged into a set of general questions, there will not be many elements in this set (the actual situation is that the general questions, the elements of this set are about 1200 ).

I want to use Graph Theory to solve this problem.

One location of all vehicles is arranged as a point in the figure. The line between two points indicates that one location can be moved from one arrangement to another. The initial state of a question is one vertex in the graph, and the condition for solving the problem is another vertex (such a vertex may be more than one ), the problem is transformed into finding a path from one point to another in the graph.

This path has a very famous algorithm, Dijkstra algorithm (shortest path algorithm ).

This problem is transformed into two steps.

1. Generate a set based on the initial input state. One position of all vehicles is arranged as one element in the set. And establish the relationship between each element (a line means that one location can be moved from one arrangement to another, and vice versa ).

2. Use the Dijkstra algorithm to find a path, which is also the shortest path, that is, the optimal solution.

Note: After writingProgramIn this question, because the length of each line is 1 by default, the Dijkstra algorithm is actually the breadth-first algorithm.

This section describes the implementation of each module.

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.