POJ 1084 Square Destroyer[Dancing Links重复覆盖]
Square Destroyer
Time Limit: 1000MS Memory Limit: 10000K
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 many 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 illustrates 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 by
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 should 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
Dancing Links重复覆盖,难在如何构建0/1矩阵。把火柴杆作为行,图中存在的正方形作为列,如果第i根火柴是第j个正方形的边的一部分,那么mtx[i][j] = 1;否则,mtx[i][j] = 0。
[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 mtx[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;
mtx[id-1][c] = 1;
id = (x2) * (2 * n + 1) + i;
mtx[id-1][c] = 1;
}
for (int i = x1; i <= x2; ++i) {
id = (i - 1)*(2*n+1) + n + y1;
mtx[id-1][c] = 1;
id = (i - 1)*(2*n+1) + n + y2 + 1;
mtx[id-1][c] = 1;
}
}
void initMtx()
{
int cnt = 0;
memset(mtx, 0, sizeof(mtx));
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;
补充:软件开发 , C++ ,