Square Destroyer
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4233 | Accepted: 1891 |
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.
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.
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
题意:给出一个n*n的矩阵,用如图的方法摆出,拿去里面m根木棍,问最少要再拿几根木棍才能使得矩阵中不存在任何正方形
解题思路: 预处理出所有正方形,然后处理出拿掉每根木棍会破坏哪些正方形,最后跑一下舞蹈链的重复覆盖就好了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
const int maxn = 300005;
int n, m, x, y, tot1, tot2;
int vis[maxn];
struct node
{
int x, y, z;
}a[maxn], b[maxn];
struct DLX
{
int L[maxn], R[maxn], U[maxn], D[maxn];
int row[maxn], col[maxn], sum[maxn], ans[maxn];
int n, m, num, cnt;
int vis[maxn];
void add(int k, int l, int r, int u, int d, int x, int y)
{
L[k] = l; R[k] = r; U[k] = u;
D[k] = d; row[k] = x; col[k] = y;
}
void reset(int n, int m)
{
num = 0x7FFFFFFF;
this->n = n; this->m = m;
for (int i = 0; i <= m; i++)
{
add(i, i - 1, i + 1, i, i, 0, i);
sum[i] = 0;
}
L[0] = m, R[m] = 0, cnt = m + 1;
}
void insert(int x, int y)
{
int temp = cnt - 1;
if (row[temp] != x)
{
add(cnt, cnt, cnt, U[y], y, x, y);
U[D[cnt]] = cnt; D[U[cnt]] = cnt;
}
else
{
add(cnt, temp, R[temp], U[y], y, x, y);
R[L[cnt]] = cnt; L[R[cnt]] = cnt;
U[D[cnt]] = cnt; D[U[cnt]] = cnt;
}
sum[y]++, cnt++;
}
void Remove(int k)
{
for (int i = D[k]; i != k; i = D[i])
{
L[R[i]] = L[i];
R[L[i]] = R[i];
}
}
void Resume(int k)
{
for (int i = U[k]; i != k; i = U[i]) L[R[i]] = R[L[i]] = i;
}
int A()
{
int dis = 0;
for (int i = R[0]; i != 0; i = R[i]) vis[i] = 0;
for (int i = R[0]; i != 0; i = R[i])
if (!vis[i])
{
dis++, vis[i] = 1;
for (int j = D[i]; j != i; j = D[j])
for (int k = R[j]; k != j; k = R[k])
vis[col[k]] = 1;
}
return dis;
}
void Dfs(int k)
{
if (!R[0]) { num = min(num, k); return ; }
else if (k + A() < num)
{
int now = R[0];
for (int i = R[0]; i != 0; i = R[i])
if (sum[now] > sum[i]) now = i;
for (int i = D[now]; i != now; i = D[i])
{
ans[k] = row[i];
Remove(i);
for (int j = R[i]; j != i; j = R[j]) Remove(j);
Dfs(k + 1);
for (int j = L[i]; j != i; j = L[j]) Resume(j);
Resume(i);
}
}
}
}dlx;
int check(node a, node b)
{
if (!a.z&&a.x == b.x&&a.y >= b.y&&a.y < b.y + b.z) return 1;
if (!a.z&&a.x == b.x + b.z&&a.y >= b.y&&a.y < b.y + b.z) return 1;
if (a.z&&a.y == b.y&&a.x >= b.x&&a.x < b.x + b.z) return 1;
if (a.z&&a.y == b.y + b.z&&a.x >= b.x&&a.x < b.x + b.z) return 1;
return 0;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &m);
tot1 = tot2 = 0;
memset(vis, 0, sizeof vis);
for (int k = 1; k <= n; k++)
for (int i = 0; i + k <= n; i++)
for (int j = 0; j + k <= n; j++) b[++tot2] = { i, j, k };
for (int i = 0; i <= n; i++)
{
for (int j = 0; j < n; j++) a[++tot1] = { i, j, 0 };
if (i == n) continue;
for (int j = 0; j <= n; j++) a[++tot1] = { i, j, 1 };
}
dlx.reset(tot1, tot2);
for (int i = 1; i <= m; i++)
{
scanf("%d", &x);
for (int j = 1; j <= tot2; j++)
if (!vis[j] && check(a[x], b[j]))
{
vis[j] = 1;
dlx.R[dlx.L[j]] = dlx.R[j];
dlx.L[dlx.R[j]] = dlx.L[j];
}
}
for (int i = 1; i <= tot1; i++)
for (int j = 1; j <= tot2; j++)
if (!vis[j]&&check(a[i], b[j])) dlx.insert(i, j);
dlx.Dfs(0);
printf("%d\n", dlx.num);
}
return 0;
}