A * Application of algorithms in OI, application of algorithms oi

Source: Internet
Author: User

A * Application of algorithms in OI, application of algorithms oi
1. A * Algorithm

Our common search algorithms often have an exponential complexity. The complexity in OI cannot meet our requirements. At this time, we usually perform some pruning optimization, but in some cases, we can have A more clever method-A * algorithm.

As A basic heuristic search algorithm, the * algorithm is different from DFS and BFS In traversing all situations. It can select A better one from all cases and then traverse it. Therefore, it converts search from "blind search" to "targeted search ". The key is how to determine the optimal situation.

A * the core of an algorithm is A valuation function. We can use it to obtain the advantages and disadvantages of each situation. The formula is:

F (n) = g (n) + h (n)

Among them, g (n) is the actual cost from the initial state to the current State. It can be calculated that h (n) is the valuation function, and the price from the current state to the end state is estimated, f (n) is the estimated total cost. Therefore, we can evaluate each State and select a better f (n) State for traversal. It is not hard to see that this valuation function can have different options, but also directly affects the efficiency of the algorithm. Therefore, the selection of this function is extremely important.

2. Select h (n)

The h (n) mentioned below is the valuation function, and d (n) is the actual value (the actual cost from the current state to the end state)

If h (n)

If h (n) = d (n), the estimated cost is equal to the actual value, and the estimated result is equal to the actual result. Therefore, the search results are most efficient according to the actual optimal conditions.

If h (n)> d (n), the estimation cost is higher than the actual value, and the estimation result is less optimal. Therefore, the search range is smaller and the efficiency is high, but the optimal solution is not necessarily obtained.

In OI, to ensure optimal answers, we usually choose h (n) $ <$ d (n ).

Let's look at two examples:

The first one is SCOI2005 server guard spirit (BZOJ 1085). This question does not seem to have any other skills and can only be searched. The data scope is indeed not big. However, normal search times out, so it is natural to think of the * algorithm. In this question, h (n) is not difficult to figure out, that is, the number of knights in the current state that need to be moved. Although h (n) is small, the actual value is too large, but here we use an optimization estimate, that is, h (n) $ <$ d (n ), therefore, the answer accuracy can be ensured. The valuation function code is as follows:

Int h () {int sum = 0; for (int I = 1; I <= 5; I ++) for (int j = 1; j <= 5; j ++) if (map [I] [j]! = Aim [I] [j]) {// map is the current state, and aim is the target State sum ++;} return sum ;}

The second is the well-known eight digital problem (you can Baidu if you don't know it), which is easy to search. There are two methods to select h (n) for this question. The first method is similar to the previous one. How many numbers does h (n) need to be moved. But there is also a more clever (and more accurate of course) Selection Method: that is, the sum of the distance from each number to the Manhattan location (that is, the number of grids to be taken to the target location. It is not hard to see that such an estimate is also biased. The valuation function code is as follows:

Const int aimx [9] = {, 3, 2}, aimy [9] = {, 3, 1 }; // aimx [I] indicates the Y coordinate of the target State number I, and aimy indicates the Y coordinate int h () {int sum = 0; int nx [9], ny [9]; // nx [I] indicates the Y coordinate of the number I, and ny indicates the Y coordinate for (int I = 1; I <= 3; I ++) for (int j = 1; j <= 3; j ++) {nx [map [I] [j] = I; // map is in the current State ny [map [I] [j] = j ;}for (int I = 1; I <9; I ++) sum + = abs (nx [I]-aimx [I]) + abs (ny [I]-aimy [I]); // return sum from Manhattan to target location ;}

Now we have a certain understanding of the selection of Valuation Functions, the key is to flexibly and accurately select h (n) when writing questions.

3. IDA * Algorithm

A * in the implementation process, the algorithm usually selects the child node with the minimum f (n) in the obtained child node for expansion (f (n) is usually selected by the heap or priority queue) to minimize the number of subnodes), you can maintain the closed list and open list to detect the extended nodes (weight, and sometimes use hash to improve efficiency ). For detailed implementation steps, refer to other blogs. Here, we prefer thinking and application. We can see that common A * consumes most of the time in sorting f (n) and determining the situation, and similar to BFS storing the state to the node, this usually requires a lot of space.

IDA * integrates the * algorithm and iterative deep search (a dfs), which features A low space consumption. At the same time, nodes do not need to be stored, and status sorting and weighting are not required. This is superior to the common A * Algorithm in terms of time and space. It is used for pruning when f (n)> the predefined maximum search depth. Such code is often much less difficult. In OI, IDA * algorithm is much more practical than A * algorithm.

For example, IDA *'s code is very concise due to the eight-digit problem in the previous question: (some core code)

Void dfs (int x, int y, int g) // g is the formula g (n) {if (g + h ()> ans | flag) return; // g + h () is f (n), ans is the predefined maximum search depth if (h () = 0) {flag = 1; return ;} // h (n) = 0 is exactly the same as the target State for (int I = 0; I <4; I ++) {int rx = x + dx [I]; int ry = y + dy [I]; // traverse the if (rx <1 | ry <1 | rx> 3 | ry> 3) continue in four directions; // determine whether swap (map [x] [y], map [rx] [ry]); // Switch location dfs (rx, ry, g + 1 ); // next, search for swap (map [x] [y], map [rx] [ry]);} return;} for (ans = 0; ans ++) {// iterate to deepen dfs (sx, sy, 0); // IDA * search if (flag) {printf ("% d \ n", ans ); // optimal solution break ;}}

 

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.