Hdu-4127 Flood-it! (IDA * algorithm), hdu-4127flood-it

Source: Internet
Author: User

Hdu-4127 Flood-it! (IDA * algorithm), hdu-4127flood-it

Today, the questions of the Fuzhou Division semi-finals are reproduced. The answer is still not AC, and the time card is very tight, but it is actually too poor for your own search, the minimum number of clicks on the purple book is in chapter 7.

I saw from the very beginning that IDA * algorithm was required for this question, but I did not have a deep understanding of what I saw yesterday. After the game, I felt that there was a new breakthrough in the overall thinking.

IDA * algorithms are the combination of iterative search and A * algorithms. Iterative deep search is very simple, that is, the maximum depth of enumeration from small to large, it is suitable for solving questions with unknown depth or that require a minimum number of iterations just like this question.

A * the essence of an algorithm is to write an evaluation function. How can I write it? It is actually a pruning. By calculating and estimating a relatively loose lower bound, that is, estimate the minimum number of layers to be recursion from the current destination. If it is added with the current depth d, it is greater than the maximum depth maxd, pruning is required. In fact, the common format of the pruning functions of the IDA * algorithm is also as follows: d + h ()> maxd pruning. Sometimes, you don't have to strictly write the h () function in the code, but you just need to know under what circumstances it is impossible to get a solution under the current depth limit.

Another important issue is how to optimize recursive Programs. In fact, I have already written an evaluation function, but it still times out because I have too many recursion operations. As we all know, If we expand a complete answer tree, the time complexity will be extremely terrible, and if I write a brute force program to remove the valuation function, it will be a complete answer tree.

As a matter of fact, I have done a lot of useless recursion. You can imagine a tree. If a useless branch is recursion in the first place, it will be a great waste.

I listed 6 colors on each layer and checked the results, in fact, the answer may only be the color adjacent to the current connected block (the current connected block is the same color block connected to the element in the upper left corner ). So we may wish to add a loop to each layer to find the adjacent colors and only recursion them. It seems that we have added a loop to each layer, which wastes time, but it is negligible compared with the answer tree at the power level ~ Because we have cut off many branches.

This gives us an inspiration: we would rather run two more loops in each layer of recursion, and we also want to find a way to reduce useless recursion!

You can refresh the Egyptian scores in the purple book and edit the manuscript, which are classic. Of course, my seventh chapter is the least refreshing, and it's time to complete it ~

For details, see the code:

# Include <bits/stdc ++. h> using namespace std; const int maxn = 15; int n, maxd, a [maxn] [maxn]; int vis [maxn] [maxn]; int dx [] = {, 0,-,-1,-}; int dy [] = {,-, 1,-,-1 }; void dfs2 (int r, int c, int col) {// update the vis [I] [j] array and convert the value of vis [I] [j] = 2 to 1, 2 vis [r] [c] = 1; for (int I = 0; I <4; ++ I) {int x = r + dx [I]; int y = c + dy [I]; if (x <0 | x> = n | y <0 | y> = n) continue; if (vis [x] [y] = 1) contin Ue; vis [x] [y] = 2; if (a [x] [y] = col) dfs2 (x, y, col) ;}} int H () {int maze [8], cnt = 0; memset (maze, 0, sizeof (maze); for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) {if (vis [I] [j] = 1) continue; if (! Maze [a [I] [j]) {maze [a [I] [j] ++; cnt ++ ;}} return cnt ;} int filled (int col) {int cnt = 0; for (int I = 0; I <n; I ++) for (int j = 0; j <n; j ++) {if (a [I] [j]! = Col) continue; if (vis [I] [j] = 2) {// only when vis [I] [j] = 2 and the color is the same as col, cnt ++ is dyed; dfs2 (I, j, col) ;}} return cnt; // return the number of dyes} bool dfs (int d) {if (d = maxd) return H () = 0; if (H () + d)> maxd) return false; int vi [maxn] [maxn]; memcpy (vi, vis, sizeof (vi); for (int I = 0; I <6; I ++) {if (filled (I) = 0) continue; // a block with no conforming I color is adjacent to it if (dfs (d + 1 )) return true; memcpy (vis, vi, sizeof (vis);} return false;} int main () {While (~ Scanf ("% d", & n) {for (int I = 0; I <n; ++ I) for (int j = 0; j <n; ++ j) scanf ("% d", & a [I] [j]); memset (vis, 0, sizeof (vis )); dfs2 (0, 0, a [0] [0]); for (maxd = 0; ++ maxd) if (dfs (0) break; printf ("% d \ n", maxd) ;}return 0 ;}



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.