hdu-4127 flood-it! (ida* algorithm)

Source: Internet
Author: User

Fuzhou today to do the title of regional competition to reproduce, a whole field in the key is still unable to AC, time card is very tight, but in fact, is also their own search science is too poor, purple book The least Brush is the seventh chapter of the problem.

I saw in the beginning that the problem requires ida* algorithm, but yesterday only to see the depth of the understanding, through the game after the problem, feel the whole idea has a new breakthrough.

ida* algorithm is the combination of iterative deepening search and a * algorithm, iterative deepening search is very simple, that is, from small to large enumeration depth upper limit, suitable for solving depth unknown or the same as the problem requires the minimum number of iterations of the topic.

The essence of A * algorithm is to write a valuation function, how to write it? is actually a pruning, by calculating a relatively loose lower bound, which is estimated from the current arrival target at least also recursion how many layers, if it plus the current depth d is greater than the maximum depth of maxd, should be pruned. In fact, the general format of the pruning function of the ida* algorithm is as follows: D + H () > Maxd pruning. Sometimes it is not necessary to write the H () function strictly in the code, but to figure out under what circumstances it is impossible to solve under the current depth limit.

Another very important question is how to optimize the recursive program. I actually wrote the valuation function on the pitch, but it still timed out because I had too many recursion. Everyone knows that if you extend a complete solution tree, the complexity of the time will be extremely scary, and if I write a brute force program that removes the valuation function, it is a complete solution tree.

In fact, I do a lot of useless recursion, you can imagine a tree, if the first to recursive a useless branch, then it will cause great waste.

I enumerate 6 colors at each level, and all of them look at the results, but the answer is only the color that is adjacent to the current connected block (the current connected block is the same color block that is connected to the upper-left corner element). So we might as well put a loop around each layer to find the colors that are next to them, just recursion them. It seems that we have added a cycle in each layer, wasting time, but compared with the sub-level solution tree can be ignored ~ because we cut off a lot of branches.

This gives us a hint: rather than running two loops in each level of recursion, try to reduce the useless recursion!

You can brush purple books on the Egyptian scores and editing manuscripts, are very classic. Of course, my seventh chapter is the least brush, it is time to fill up the ~

See the code for details:

#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,0,-1,1,-1,-1,1};int dy[] = {1,0,-1,0,1,-1,1,-1};void dfs2 (int r,int c,int col) {//update vis[i][j] array, to vis[    I][J] = = 2 becomes 1, with which it becomes 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) continue;        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 with col coloronly dyeing cnt++;            DFS2 (I,j,col);    }} return cnt;//returns the number of staining}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;//The block with the I color that does not meet the requirements is adjacent to the IF (DFS (d+1)) return true;    memcpy (vis,vi,sizeof (VIS)); } return false;} int main () {while (~SCANF ("%d", &n) &&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 NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu-4127 flood-it! (ida* algorithm)

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.