Hdoj 4285 circuits (plug DP summary)

Source: Internet
Author: User

Question: Give a card with obstacles. Let's calculate the number of solutions containing K rings. Each non-obstacle board must be included in a ring, and there cannot be a situation of changing the ring (this condition is not seen, it took a few hours to debug, it is a force !!!!!)

Behavior n, column M, number of rings K

Plug DP: Uses column J as the inner loop and row I as the outer loop. Each time a node is added, m + 1 small square edge is added to the split line, calculate the state transition of the next state based on the state of these edges. Because the Ring cannot be intersecting, the bracket principle can be used.

Note: If the number of rings is required, we add a ring number flag, and changing the ring is a constraint that each time the ring is formed, the numbers of both sides of the plug are even, because the two sides of the plug is an even number, as long as there is a connection between the two sides of the plug to form a ring, then the number of both sides of the plug is not an even number;

The memory is also very limited. I first opened the memory a lot and figured out the maximum number of States, only 420000, which solved the memory problem;

Because there are too many States to use pure DP, I use Hash optimization;

Note that bit operations are much faster than %, so although each plug has only three States, we use two bits to record the operation much faster;

The maximum number of rings is 12*12/4 = 36, which requires six bits, while the State is (12 + 1) * 2 = 26 BITs, which can be expressed in the int type, but note that when the bit operation moves to the low position, the negative number will be supplemented by 1 in the last bit, so it takes a long time to debug Len.

I have submitted this question for 32 times. It's really hard work! Come on !!

My code:

# Include <string. h>
# Include <math. h>
# Include <stdio. h>
# Include <algorithm>
Using namespace STD;

# Deprecision Max 1333317

Int que [2] [420000], last [2], flag;
Int num [2] [420000];

Int mMax;

Int index;

Int hash [Max], list [Max], where [Max];

Char map [20] [20];
Int n, m, K;

Int find (int x, int la)
{
Int xx = x % Max;
While (XX <0)
Xx + = max;
While (hash [XX] = index & list [XX]! = X)
Xx = (xx + 1) % Max;
If (hash [XX]! = Index)
{
Where [XX] = La;
List [XX] = X;
Hash [XX] = index;
Return 0;
}
Return where [XX];
}

Int getarray (int A, Int J)
{
If (j <= m)
Return (A> (J * 2) & 3;
Else
Return (A> (m + 1) * 2) & (1 <6)-1 );
}

Int setarray (int * a, Int J, int X)
{
If (j <= m)
{
* A = (* )&(~ (3 <j * 2 ));
* A + = x <j * 2;
}
Else
{
* A & = (1 <(m + 1) * 2)-1;
* A | = x <(2 * m + 2 );
}
Return 0;
}

Int getnext (int A, Int J)
{
Int F = 1;
For (INT I = J + 1; I <= m; I ++)
{
If (getarray (A, I) = 1)
F ++;
If (getarray (A, I) = 2)
F --;
If (F = 0)
Return I;
}
Return 0;
}

Int getfirst (int A, Int J)
{
Int F = 1;
For (INT I = J-1; I> = 0; I --)
{
If (getarray (A, I) = 2)
F ++;
If (getarray (A, I) = 1)
F --;
If (F = 0)
Return I;
}
Return 0;
}

Int add (INT Nu, int)
{
If (getarray (Nu, m + 1)> K)
Return 0;
Int P = find (Nu, last [1-Flag] + 1 );
If (P! = 0)
{
Num [1-Flag] [p] + = num [flag] [a];
Num [1-Flag] [p] % = 1000000007;
}
Else
{
Que [1-Flag] [++ last [1-Flag] = Nu;
Num [1-Flag] [last [1-Flag] = num [flag] [a];
// If (last [1-Flag]> mMax)
// MMax = last [1-Flag];
}
Return 0;
}

Int check (int A, Int J)
{
Int I;
Int x = 0;
For (I = J-1; I> = 0; I --)
{
If (getarray (A, I) = 1)
X ++;
If (getarray (A, I) = 2)
X --;
}
Return X % 2;
}

Int change (int * Nu)
{
Int x = * Nu;
Setarray (Nu, 0, 0 );
For (INT I = 1; I <= m; I ++)
Setarray (Nu, I, getarray (X, I-1 ));
Return 0;
}

_ Int64 getresult ()
{
Int I, j, Z;

Int A, B, Nu;

Flag = 0;
Last [flag] = 1;
Que [flag] [1] = 0;
Num [flag] [1] = 1;



For (I = 0; I <n; I ++)
For (j = 0; j <m; j ++)
{
If (j = 0)
{
Last [1-Flag] = 0;
For (Z = 1; Z <= last [flag]; Z ++)
{
Nu = que [flag] [Z];
If (getarray (Nu, m )! = 0)
Continue;
Change (& Nu );
Que [1-Flag] [++ last [1-Flag] = Nu;
Num [1-Flag] [last [1-Flag] = num [flag] [Z];
}
Flag = 1-flag;
}
Last [1-Flag] = 0;

If (Map [I] [J] = '.')
{
Index --;
For (Z = 1; Z <= last [flag]; Z ++)
{
Nu = que [flag] [Z];
A = getarray (Nu, J );
B = getarray (Nu, J + 1 );
If (A = B &! = 0)
{
If (A = 1)
{
Setarray (& Nu, getnext (Nu, J + 1), 1 );
// Array [getnext (array, J + 1)] = 1;
}
Else
{
Setarray (& Nu, getfirst (Nu, J), 2 );
// Array [getfirst (array, j)] = 2;
}
Setarray (& Nu, J, 0 );
Setarray (& Nu, J + 1, 0 );
Add (Nu, Z );
}
Else if (a = 2 & B = 1)
{
Setarray (& Nu, J, 0 );
Setarray (& Nu, J + 1, 0 );
Add (Nu, Z );

}
Else if (a = 1 & B = 2)
{
If (check (Nu, j ))
Continue;
Setarray (& Nu, J, 0 );
Setarray (& Nu, J + 1, 0 );
Setarray (& Nu, m + 1, getarray (Nu, m + 1) + 1 );
Add (Nu, Z );
}
Else if (a = 0 & B = 0)
{
Setarray (& Nu, J, 1 );
Setarray (& Nu, J + 1, 2 );
Add (Nu, Z );
}
Else
{
Add (Nu, Z );

Setarray (& Nu, J, getarray (que [flag] [Z], J + 1 ));
Setarray (& Nu, J + 1, getarray (que [flag] [Z], j ));
Add (Nu, Z );
}
}


}
Else
{
For (Z = 1; Z <= last [flag]; Z ++)
{
Nu = que [flag] [Z];
If (getarray (Nu, j) | getarray (Nu, J + 1 ))
Continue;
Que [1-Flag] [++ last [1-Flag] = que [flag] [Z];
Num [1-Flag] [last [1-Flag] = num [flag] [Z];
}
}
Flag = 1-flag;
}
For (I = 1; I <= last [flag]; I ++)
{
Nu = que [flag] [I];
For (j = 0; j <= m; j ++)
{
If (getarray (Nu, j )! = 0)
Break;
If (j = M & getarray (Nu, m + 1) = K)
Return num [flag] [I];
}
}
Return 0;
}

Int main ()
{
// Freopen ("in.txt", "r", stdin );
// Freopen ("out.txt", "W", stdout );
Int I, J;
Int PX;
Int ca, cases;
Scanf ("% d", & cases );
For (CA = 1; Ca <= cases; CA ++)
{
// MMax = 0;
Px = 0;
Scanf ("% d", & N, & M, & K );
Memset (hash, 0, sizeof (hash ));

For (I = 0; I <n; I ++)
{
Scanf ("% s", map [I]);
For (j = 0; j <m; j ++)
If (Map [I] [J] = '*')
Px ++;
}
If (k> (M * n-Px)/4)
{
Printf ("0 \ n ");
Continue;
}
Index = 0;
Printf ("% i64d \ n", getresult ());
// Printf ("% d \ n", mMax );
}
Return 0;
}

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.