Square Destroyer
Time Limit: 1000 MS Memory Limit: 10000 K
Total Submissions: 2695 Accepted: 1101
Description
The left figure below shows a complete 3*3 grid made with 2 * (3*4) (= 24) matchsticks. the lengths of all matchsticks are one. you can find squares of different sizes in the grid. the size of a square is the length of its side. in the grid shown in the left figure, there are 9 squares of size one, 4 squares of size two, and 1 square of size three.
Each matchstick of the complete grid is identified with a unique number which is assigned from left to right and from top to bottom as shown in the left figure. if you take some matchsticks out from the complete grid, then some squares in the grid will be destroyed, which results in an incomplete 3*3 grid. the right figure has strates an incomplete 3*3 grid after removing three matchsticks numbered with 12, 17 and 23. this removal destroys 5 squares of size one, 3 squares of size two, and 1 square of size three. consequently, the incomplete grid does not have squares of size three, but still has 4 squares of size one and 1 square of size two.
As input, you are given a (complete or incomplete) n * n grid made with no more than 2n (n + 1) matchsticks for a natural number 5 <= n. your task is to compute the minimum number of matchsticks taken
Out to destroy all the squares existing in the input n * n grid.
Input
The input consists of T test cases. The number of test cases (T) is given in the first line of the input file.
Each test case consists of two lines: The first line contains a natural number n, not greater than 5, which implies you are given a (complete or incomplete) n * n grid as input, and the second line begins with a nonnegative integer k, the number of matchsticks that are missing from the complete n * n grid, followed
K numbers specifying the matchsticks. note that if k is equal to zero, then the input grid is a complete n * n grid; otherwise, the input grid is an incomplete n * n grid such that the specified k matchsticks are missing from the complete n * n grid.
Output
Print exactly one line for each test case. The line shoshould contain the minimum number of matchsticks that have to be taken out to destroy all the squares in the input grid.
Sample Input
2
2
0
3
3 12 17 23
Sample Output
3
3
Source
Taejon 2001
It is difficult to build a 0/1 matrix for repeated coverage of Dancing Links. The matchrod is used as the row, and the square in the figure is used as the column. If the I match is a part of the edge of the j square, then the content of the content in the content can be [I] [j] = 1; otherwise, (delete.
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
# Include <cmath>
Using namespace std;
Const int maxn = 65*6*6*6;
Const int oo = 1 <30;
Const int maxrow = 65;
Const int maxcol = 6*6*6;
Bool CTX [maxrow] [maxcol];
Int t, n, k, ans;
Int has [65];
Int totRow, totCol, head, idx;
Int L [maxn], R [maxn], U [maxn], D [maxn];
Int RH [maxn], CH [maxn], S [maxn];
Bool OK (int x1, int y1, int x2, int y2)
{
Bool flag = true;
Int id;
For (int I = y1; I <= y2; ++ I ){
Id = (x1-1) * (2 * n + 1) + I;
If (! Has [id]) {
Flag = false;
Break;
}
Id = (x2) * (2 * n + 1) + I;
If (! Has [id]) {
Flag = false;
Break;
}
}
If (! Flag) return false;
For (int I = x1; I <= x2; ++ I ){
Id = (I-1) * (2 * n + 1) + n + y1;
If (! Has [id]) {
Flag = false;
Break;
}
Id = (I-1) * (2 * n + 1) + n + y2 + 1;
If (! Has [id]) {
Flag = false;
Break;
}
}
If (! Flag) return false;
Return true;
}
Void add (int x1, int y1, int x2, int y2, int c)
{
Int id;
For (int I = y1; I <= y2; ++ I ){
Id = (x1-1) * (2 * n + 1) + I;
CTX [id-1] [c] = 1;
Id = (x2) * (2 * n + 1) + I;
CTX [id-1] [c] = 1;
}
For (int I = x1; I <= x2; ++ I ){
Id = (I-1) * (2 * n + 1) + n + y1;
CTX [id-1] [c] = 1;
Id = (I-1) * (2 * n + 1) + n + y2 + 1;
CTX [id-1] [c] = 1;
}
}
Void initctx ()
{
Int cnt = 0;
Memset (CTX, 0, sizeof (CTX ));
For (int I = 1; I <= n; ++ I ){
For (int j = 1; j <= n; ++ j ){
For (int k = 0; I + k <= n & j + k <= n; ++ k ){
If (OK (I, j, I + k, j + k )){
Add (I, j, I + k, j + k, cnt );
Cnt ++;
}
}
}
}
TotRow = 2 * n * (n + 1 );
TotCol = cnt;
}
Int newNode (int up, int down, int left, int right)
{
U [idx] = up; D [idx] = down;
L [idx] = left; R [idx] = right;
U [down] = D [up] = L [right] = R [left] = idx;
Return idx ++;
}
Void build ()
{
Idx = maxn-1;
Head = newNode (idx, idx );
Idx = 0;
For (int j = 0; j <totCol; ++ j ){
NewNode (idx, idx, L [head], head );
CH [j] = j; S [j] = 0;
}
For (int I = 0; I <totRow; ++ I ){
Int k =-1;
For (int j = 0; j <totCol; ++ j ){
If (! CTX [I] [j]) continue;
If (-1 = k ){
K = newNode (U [CH [j], CH [j], idx, idx );
RH [k] = I; CH [k] = j; S [j] ++;
} Else {
K = newNode (U [CH [j], CH [j], k, R [k]);
RH [k] = I; CH [k] = j; S [j] ++;
}
}
}
}
Void remove (int c)
{
For (int I = D [c]; I! = C; I = D [I]) {
L [R [I] = L [I]; R [L [I] = R [I];/* S [CH [I] --; */
}
}
Void resume (int c)
{
For (int I = U [c]; I! = C; I = U [I]) {
L [R [I] = R [L [I] = I;/* S [CH [I] ++ ;*/
}
}
/* Valuation function */
Int h ()
{
Bool vis [maxcol];
Memset (vis, false, sizeof (vis ));
Int ret = 0;
For (int I = R [head]; I! = Head; I = R [I]) {
If (! Vis [I]) {
Ret ++;
Vis [I] = true;
For (int j = D [I]; j! = I; j = D [j]) {
For (int k = R [j]; k! = J; k = R [k]) {
Vis [CH [k] = true;
}
}
}
}
Return ret;
}
Void dance (int cnt)
{
If (cnt + h ()> = ans ){
Return;
}
If (R [head] = head ){
Ans = cnt;
Return;
}
Int c, Min = oo;
For (int I = R [head]; I! = Head; I = R [I]) {
If (S [I] <Min ){
Min = S [I]; c = I;
}
}
For (int I = D [c]; I! = C; I = D [I]) {
Remove (I );
For (int j = R [I]; j! = I; j = R [j]) {
Remove (j );
}
Dance (cnt + 1 );
For (int j = L [I]; j! = I; j = L [j]) {
Resume (j );
}
Resume (I );
}
Return;
}
Int main ()
{
Scanf ("% d", & t );
While (t --){
Scanf ("% d", & n );
Scanf ("% d", & k );
Memset (has, true, sizeof (has ));
Int num;
For (int I = 0; I <k; ++ I ){
Scanf ("% d", & num );
Has [num] = false;
}
Initctx ();
Build ();
Ans = oo;
Dance (0 );
Printf ("% d \ n", ans );
}
Return 0;
}