Eight Digital Problems

Source: Internet
Author: User

Problem description:

The so-called digital 8 refers to a game with numbers 1, 2, 3 ,..., 8 square digital cards can be placed on a 3x3 digital disk. The cards must not overlap. Therefore, a space is displayed on a 3 × 3 Digital Drive. It is now required to gradually arrange any digital disks in a special arrangement based on the principle that only digital cards adjacent to spaces can be exchanged with spaces at a time. For example, it indicates a specific solution to the eight digital problem.

Problem Analysis:

First, the Eight Digital problems include an initial status (START)
And the target status (end). The so-called solution to the eight digital problem is to find a series of transitional states (START-> state1-> state2->... -> End ). Whether the status exists is the first problem to be solved:

Q1: How do I express each status and operation?

There are many representation methods, such as a 3*3 Eight Digital Disk can be compressed into an int value, but not applicable to 15 puzzle or greater than 8
. If space requirements are high, compression can be performed. This article uses an int representation.

The expression is as follows: Because the int value range is greater than 1e9, we take the 9-bit lower of an int. To facilitate searching for the space position, the Int bit is used to place spaces (1-9 ). The first eight digits record the numbers at the corresponding position in sequence from top to bottom of the row and from left to right of the column. For example:

It can be expressed as 2 3 1
5 8 4 6 7 5, 5 of a single digit indicates that the space is at 5th bits, and the first eight bits are recorded in order. The coordinate conversion formula is as follows:

Num (compressed INT)
X y (column Y in X row, 1 remembered) 1E (n) is 1 multiplied by 10 N times

Int
Temp = (x-1) * 3 + Y

If temp
> Num % 10 then return (Num/1e (9-temp + 1) % 10

Else return
(Num/1e (9-temp) % 10

For the convenience of this article, the target status is: 1 2 3 4 5 6 7 8 9 that is->

This article uses u r d
L indicates the four operations of spaces going up to the right to the left. For example, the diagram in the introduction includes two steps:
May not match the usual habit of playing such games, but this is to be consistent with the ACM example.

Correspondingly, the status changes caused by each operation are as follows:

R
: Num value ++
L
: Num value-

U
: Complicated

Int T0 =
9-num % 10 + 1

Int T1 = num
/1e (t0)

Int t2 =
T1. % 1000

T1 = T1-T2 +
(T2% 100) * 10 + T2/100

T1 * =
1e (t0)

Return (t1 +
(Num % T0)-3 ))

D
: The same as the U operation before return, return returns (t1 + (Num % T0) + 3 ))

Q2: Determine whether there is an intermediate state to enable start
Arrive at end?

You can use the combined mathematical method to quickly judge, for example, soj 2061 question
This is all about the problem in question 2360. However, the method of digital 8 is much simpler than that of Digital 8.

The method used in this article is to calculate the values in the reverse order. Taking 2 3 1 5 8 4 6 7 5 as an example, 5 indicates a space. If this parameter is not calculated, 23158467 is calculated.
The reverse value of is

0 + 0 + 2
(1 <2 1 <3) + 0 + 0 + 1 (
4 <5) + 1 (6 <8) + 1 (
7 <8) = 5

The reverse order of the Target status 1 2 3 4 5 6 7 8 9 is naturally 0.

Whether the two States are reachable or not. You can calculate the reverse order value of the two States. If the two statuses are the same, they are reachable. Otherwise, the two States are not reachable.

Simple proof:

L and R
The operation does not affect the reverse value of the status, because it only changes the single digit (space position ).

The U and D operations are performed to make numbers at a certain position
Right/left shift two places. Because every movement of the number sequence changes the value of the reverse order
After two moves, the parity remains unchanged.

Therefore, none of the four operations will affect the parity of the sequence.

Q3: how to find a series of intermediate states and problems?

The method to search for these intermediate states is search, but the search is prone to time and space problems. The basic principles of search are as follows:

From 1 3 7 2 4 6
8 5 2 status can be derived from three States, if you select 1 2 3 7 4 6 8 5
Then, three States are derived, and the selection is continued according to a policy until the new State derived is the end of the target State.

It is easy to see that such a search is similar to a tree like searching for the target leaf from the root of the tree to the stem. As its scale expands, its leaves become increasingly dense, and the final scale will become uncontrollable. In this way, it will greatly increase the search difficulty and consume a lot of time.

The following problems are encountered in normal search:

A
Loop occurs in search, that is, access to a certain status and then access this status.

B
If the search path is poor, you cannot obtain a better intermediate state set (that is, the number of elements in the intermediate state set is too large ).

C
Too many useless states are accessed during the search process. These statuses are not helpful for the final result.

Among the above three problems, A is a fatal problem, which may lead to an endless loop of the program; B and C are non-fatal, but if not handled properly, it may lead to a sharp decline in performance.

Q4: how to avoid repeated access to a status?

The most direct method is to record the access status of each status, and then derive the status without deriving the accessed status. The idea is to mark each status with a flag.
If it is set to true, it is not derived. If it is set to false, it is derived and modified to true.

In some algorithm descriptions, there are two linked lists, one being a live linked list (to be accessed) and the other being a dead linked list (to be accessed ). Each derivative state first determines whether it is already in two linked lists. If it exists, it is not derived. If it does not exist, it is placed in a live linked list. Put the state derived into the dead linked list.

In order to record whether each status has been accessed, we need enough space. Eight Digital problems: 9 !, This number is not very big, but another question that comes in the face is how we can quickly access these States. If we simply use a linked list, it will be quite large in size, when the number of states to be searched is very large, the State cannot be quickly found. The complexity is O (n). To solve this problem, we will use the hash function method, reduce the complexity to O (1 ).

Here, the hash function is applicable to many full sorting problems. N! It is the base, and the reverse order value of the nth digit of the State is the nth digit of the hash value. For spaces, multiply them (9-position) by 8 !. For example, 1
The hash value of 3 7 2 4 6 8 5 8 is equal:

0*0! + 0*1! + 0*2! + 2*3! + 1*4! + 1*5! + 0*6! + 3*7! + (9-8) * 8! =
55596 <9!

For specific reasons, you can check out some mathematical books, where the hash value of 1 2 3 4 5 6 7 8 9 is 0 and the minimum value is 8 7 6 5 4 3 2 1 0.
The hash value of IS (9! -1) the maximum value, while the other values are from 0 to (9! -1), and all are unique.

Q5: How can I obtain the best solution for search?

A common search is called DFS (deep-priority search ). In addition to DFS and BFS, the two are in different directions in terms of concept. DFS expands to a deeper level, while BFS expands to a wider scale. In the solution tree of the Eight Digital problems, the depth of the tree indicates the number of steps from the initial state to the target State. The DFS is blindly deep, so it is easy to find a solution with a large depth.

BFS can ensure that the depth of the solution is the least, because BFS will not access a deeper state before all the statuses in the same depth are completely accessed, so it is more suitable for eight digital problems, at least solves the problem of finding the best solution.

However, BFS and DFS cannot solve the problem C, because each State needs to expand, so extensive search can easily expand the number of states to be searched. Ultimately, efficiency is affected.

Q6: How can we reduce the status unrelated to the target status and solution of the extended search?

All of the above are searches from the start state to the end state. If you reverse the end state and the start state, there will actually be another search path (discussed in Q8 Policy 3 ), however, simply switching the end and start does not reduce the scale of State expansion. We can combine forward and reverse searches, which is a bidirectional wide search.

Two-way wide search refers to the simultaneous search from the start and end ends. When one end wants to access a status that has been accessed by the other end, it finds the solution and the search ends. Its advantage is that it can avoid the expansion of the status after extensive search.

Two-way wide search can save space and time by half!

Q7: determines a quick search policy?

Two-way wide search can greatly reduce time and space, but in some cases we do not need space savings. For example, in Q4, we have decided that the space we need to use is 9 !, So there is no need to save. In this way, we can focus on shortening the time.

Heuristic Search is a very practical search method in path search. by designing a good heuristic function, the state priority is calculated, and the state with a higher priority is given priority, you can search for the time to reach the target State in advance. A * is a heuristic search function f.
'() = G' () + H' () can be applied to eight digital problems.

G '()--
Estimated actual cost g * () from the starting status to the current status, g' ()> = g *()

H '()--
Estimation of the actual cost H * () from the current status to the target status, H '() <= H *()

Note two restrictions:

(1) H '()
<= H * () (2) the f' () value in any State must be greater than the f' () value in its parent state, that is, F
'() Increases monotonically.

Where, g '()
Is the search depth, H '()
It is an estimation function used to estimate the number of steps from the current state to the target State. There are generally two kinds of estimation functions for solving the eight digital problem. Difference (
Status A, status B), the return value is
The number is different from the number at each position in the B state. The other classic is the Manhattan distance.
Manhattan (status A, status B) returns the distance from the position of a to the position of B (see the example ).

For example, status 1 3 7 2 4 6 8 5 2 and status 1 2 3 4 5 6 7 8 9 difference
Is 5 (without spaces ). The Manhattan distance is:

1 (7D) + 1 (2u once) + 2 (4l twice) + 3 (6R twice u once) + 2 (5u once) =
9

Manhattan of a single number should be smaller than 5 because the distance between the diagonal corner is only 4. If it is greater than 4, the calculation is incorrect.

Whether difference or Manhattan, it is estimated that the smaller the value, the closer it is to end, the higher the priority.

When calculating difference and Manhattan, we recommend that you ignore spaces because spaces are dispensable in difference, which has little impact on the overall search.

Manhattan
Do not use spaces. In fact, every move step, excluding spaces, is equivalent to moving a number. If every movement is perfect, that is, a number is returned, the distance from the start state to the end state is Manhattan. Conversely, Manhattan is the minimum number of steps from start to end.

Return to F' () = G'
() + H' (), in fact, the wide search is H'
() = 0 is a special case of heuristic search, and deep search is f' () = 0
. H' () plays an important role in optimizing the search speed.

Q8: Can I further optimize the search policy?

The answer is yes.

A * the merits of a search policy are determined by the quality of H. Two H '() functions are listed above, but it is not enough to have them. After experimental analysis, in f' (), g
'() Determines the distance between the solution obtained from the start state and the end state to the optimal solution. H '() can affect the search speed.

So the first optimization strategy is to enlarge H' (), for example, to make H' () = 10 * Manhattan (), then f' () = G'
() + 10 * Manhattan (), which may increase the search speed. Unfortunately, the resulting solution will no longer be optimal.

Why can I zoom in on H' () to speed up the search? We can imagine that H' () describes the estimated distance from the current state to the end state, the shorter the estimated distance, the faster it is, the faster it is to reach the end state. While
G' () describes the current depth. Enlarge H'
() The goal is to ignore the depth factor as much as possible. It is a type of deep search with a policy. The natural speed is faster than that of wide search and deep search, because the depth factor is reduced, so it is getting farther and farther away from the optimal solution. About H'
() How much to zoom in is an interesting question. If you are interested, try some experiments.

The second is to update the status to be checked, because a * search requires a sequence to be checked. First, hash is used in Q4 to avoid repeated access to the same State. The status in the queue to be checked is not expanded. If the status is the same but its G
'() Is better than the original G' (), so we want to search for a new status rather than the original status. In this way, you only need to update its G '()
You can.

The third is to pay attention to the method of implementing the Program. At first, I used sort to sort f
'() And then find the State with the largest weight, and then find that it is faster to use make_heap. Think about it. Because there are many contacts to access, a large number of access queues will naturally affect the speed of repeated sorting, while heap operations are better than sorting. Another point is that the search method for updating the queue to be checked should also be better. I used priorityqueue in the Java demo, but the result is not very satisfactory.

Article 4 The optimization strategy is to use the IDA * algorithm, which is a type of a * algorithm and the ID is called iterative.
Deepening is the meaning of iteration deepening. The idea is as follows:

By the way, prepare a temp = max, H' that records the minimum values of a loop to take the Manhattan distance.

First estimate the H' () from the start state to the end state '()
Record as Min and put start in the queue to be accessed

Reads the next status of the queue and ends at the end of the queue.

If G' ()> min
Goto 6

If G' () + H' ()>
Whether Min is true, true goto 6, no goto 5

Expand the status and mark it as accessed. If the end state is found, the algorithm is ended. Goto ②

Temp = min (Manhattan,
Temp), Goto ③

If no scaling status exists, min = temp
(ID indicates here) loop from the beginning goto ②

Article 5 The Optimization Strategy itself has nothing to do with the search, and does not help you with the question. However, in theory, it is of reference value. Do you remember to start searching from end as described in Q6? If our task is for multiple start
When searching with end, we can record the path after each search. This path is very important, because in future searches, if the start and end paths have been recorded, you can call up the results directly.

Starting from end, you can easily determine whether the next start has a path to end. When the current search is complete, the accessed status can be directly used. If the start is not in the status list, find the next status from the linked list to be accessed by the search policy, it is equal to the previous search result.

The reason why I failed to help speed is that this optimization strategy needs to increase G'
() Proportion. Otherwise, the depth is quite large. Because the previous search policy has nothing to do with the next one, the previous path cannot be predicted, therefore, the depth is too large. The solution is to increase G'
().

Policy 5 adds a buffer to the program to avoid repeated computation. If you want to implement the eight-digit application, the buffer zone will help you.

Q10: how to record the path found?

When we find the solution, we need to have a similar rollback job to sort out a solution path. Because it is not a simple DFS, we cannot use the program stack used by function calls.

We can manually add a simulated stack. Hash is solved in Q4, and the corresponding State value can be quickly accessed using the hash table. Here, we change the original bool value to Char or other values that can represent an operation (at least five States are required, except U
R l d can also indicate that the status has been accessed.

When the solution is found, record the status value of the last access, and then start to read the operation corresponding to the status, just like the rollback operation of the stack operation, until the start point of the search is found. Record the operation value of stack exit, which is a path.

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.