topic link : [Kuangbin take you to fly] topic three Dancing links D-the Dragon's puzzle
Test Instructions Description
It's a world of Swords and magic. Heroes and demons coexist, turbulent and stable. But all in all, the Kingdom of Kurt is a peaceful country, the people live and live in peace, and the magic is less. But there are always some magic things that come into the city, Disturb the lives of the people. Some people will come out to protect the residents from the evil. Magic makes Emily the one. She rode her mount, the Dragon Myskgräs together to destroy the demons that interfered with the survival of mankind and to maintain the stability of the kingdom. Emily wanted to be able to complete the task with minimal damage. She used time to stop the magic stop time, and then he could fire a fireball to burn the enemy. Myskgräs wants to know how he can kill the enemy as quickly as possible and relieve Emily's burden.
Input
There are several groups of data that you need to process to EOF. The first row of each group of data has two numbers, n,m, (1<=n,m<=15) represents the area of the task. Then there are n rows, each with m integers, such as 1 to indicate that the point has a monster, and 0 to indicate that the point is free of monsters. Then the next line has two integers, N1,m1 (n1<=n,m1<=m), respectively, represents the row that Myskgräs can attack, the number of columns (the rows cannot be interchanged), Suppose that a single fireball can be emitted by a unit of time, and all monsters can be killed with a single blow.
Output
The output line, an integer, represents the shortest time that the meter-drop destroys all the magic objects.
Ideas
Exact overlay is the Formosan Aboriginal of all columns in this row after each row is selected. And the subject is repeated coverage, so delete, only delete the relevant columns can, not the connection.
The subject needs pruning, as described in the Pruning function f () code.
Ps:a the first repeated coverage problem, because the data range was t so many times, to cry without tears, the code swept over and over again, just can't think of is the data range of reasons. Careful is the kingly ...
Code
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <cstdlib>#include <vector>using namespace Std;Const intN = -* -;Const intMAX = N*n;intG[n][n];//Store mapintP[max];//Storage point numberintU[max], D[max], L[max], R[max];//array analog linked list pointing (up or down)intC[max], M[max];//Node columns and rowsintS[n];//stores the number of elements per columnintH[n];//Wardrobe handsintAnsvoidInitintNintm) { for(intI=0; i<=m; i++) {l[i+1] = i; R[i] = i+1; U[i] = d[i] = i; S[i] =0; } for(intI=1; i<=n; i++) H[i] =-1; l[0] = m; R[M] =0;}voidLinkintRowintColint ID)//Join the node to the linked list{c[ID] = col; m[ID] = row;//Record ranksu[ID] = U[col]; D[u[col]] =ID;//up/down connectiond[ID] = col; U[col] =ID;if(H[row] = =-1)///Left/Right connection (use the header for easy head insertion)H[row] = l[ID] = r[ID] =ID;Else{l[ID] = L[h[row]]; R[l[h[row]] =ID; L[h[row]] =ID; r[ID] = H[row]; } s[col]++;}voidRemoveintCol//Because it is repeated overwrite, so a loop, only delete the column, do not delete the related{ for(intI=d[col]; I!=col; I=d[i]) {L[r[i]] = l[i]; R[l[i]] = r[i]; }}voidResumeintCol) { for(intI=u[col]; I!=col; I=u[i]) {L[r[i]] = i; R[l[i]] = i; }}BOOLV[max];intF ()//Pruning function{intnum =0; for(inti=r[0]; i!=0; I=r[i])//0 represents not eliminatedV[i] =0; for(inti=r[0]; i!=0; I=r[i]) {if(!v[i]) {V[i] =1; num++;//(assuming maximum range of attack) to destroy the entire range of all attacks that kill it, similar to drawing a circle at the center of it for(intJ=u[i]; J!=i; J=U[J]) for(intK=R[J]; K!=j; K=R[K]) V[c[k] =1; } }returnNum;}voidDanceintK) {if(K+f () >= ans)return;if(! r[0] {ans = min (ans, k);return; }intCol = r[0]; for(inti=r[0]; i!=0; I=r[i])if(S[i] < S[col]) col = i; for(intI=d[col]; I!=col; I=d[i]) {remove (i); for(intJ=r[i]; J!=i; J=R[J]) Remove (j); Dance (k +1); for(intJ=l[i]; J!=i; J=L[J]) Resume (j); Resume (i); }}intMain () {intN, M; while(~SCANF ("%d%d", &n, &m)) {intnum =0; for(intI=1; i<=n; i++) for(intj=1; j<=m; J + +) {scanf ("%d", &g[i][j]);if(G[i][j]) p[j+ (I-1) *m] = ++num; }intR, C; scanf"%d%d", &r, &c); Init ((n-r+1) * (m-c+1), num);int ID= num+1; for(intI=1; i<=n-r+1; i++) { for(intj=1; j<=m-c+1; J + +) for(intX=i; x<=i+r-1; X + +) for(intY=j; y<=j+c-1; y++) {if(G[x][y]) Link (j+ (i-1) * (m-c+1), p[y+ (x1) *m],ID++); }} ans =0x3f3f3f3f; Dance0); printf"%d\n", ans); }return 0;}
Fzu 1686 Dragon Puzzle (Repeat overlay problem & dance chain)