POJ 1084 Square Destroyer

来源:互联网 发布:java filereader 指针 编辑:程序博客网 时间:2024/06/05 04:45

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

22033 12 17 23

Sample Output

3

3

拿走最少的火柴使得没有任何一个正方形存在

转换为01矩阵,用dlx重复覆盖解决

#include<cstdio>#include<vector>#include<cmath>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;const ll maxn = 1005;int n, T, m, vis[maxn],tot1,tot2,x;struct point{int x,y,z;point(){};point(int x,int y,int z):x(x),y(y),z(z){};}a[maxn],b[maxn];inline void read(int &ret){char c;do {c = getchar();} while (c < '0' || c > '9');ret = c - '0';while ((c = getchar()) >= '0' && c <= '9')ret = ret * 10 + (c - '0');}struct DLX{#define maxn 10005#define F(i,A,s) for (int i=A[s];i!=s;i=A[i])int L[maxn], R[maxn], U[maxn], D[maxn];int row[maxn], col[maxn], ans[maxn], cnt[maxn];int n, m, num, sz;bool Flag;void add(int now, int l, int r, int u, int d, int x, int y){L[now] = l;R[now] = r;U[now] = u;D[now] = d;   row[now] = x;  col[now] = y;}void reset(int n, int m){Flag = false;this->n = n;this->m = m;for (int i = 0; i <= m; i++){add(i, i - 1, i + 1, i, i, 0, i);cnt[i] = 0;}L[0] = m; R[m] = 0; sz = m + 1;}void insert(int x, int y){int ft = sz - 1;if (row[ft] != x){add(sz, sz, sz, U[y], y, x, y);U[D[sz]] = sz; D[U[sz]] = sz;}else{add(sz, ft, R[ft], U[y], y, x, y);R[L[sz]] = sz; L[R[sz]] = sz;U[D[sz]] = sz; D[U[sz]] = sz;}++cnt[y];++sz;}//精确覆盖void remove(int now){R[L[now]] = R[now];L[R[now]] = L[now];F(i,D,now) F(j,R,i){D[U[j]] = D[j];U[D[j]] = U[j];--cnt[col[j]];}}void resume(int now){F(i,U,now)F(j,L,i){D[U[j]] = j;U[D[j]] = j;++cnt[col[j]];}R[L[now]] = now;L[R[now]] = now;}int dfs(int x){if (!R[0]) return 1;int now = R[0];F(i,R,0) if (cnt[now]>cnt[i]) now = i;remove(now);F(i,D,now){ans[x] = row[i];F(j,R,i) remove(col[j]);if (dfs(x + 1)) return 1;F(j,L,i) resume(col[j]);}resume(now);return 0;}//精确覆盖//重复覆盖void Remove(int now){F(i,D,now) {L[R[i]]=L[i];R[L[i]]=R[i];}}void Resume(int now){F(i,U,now) L[R[i]]=R[L[i]]=i;}int vis[maxn];int flag[maxn];int A(){int dis=0;F(i,R,0) vis[i]=0;F(i,R,0) if (!vis[i]){dis++;vis[i]=1;F(j,D,i) F(k,R,j) vis[col[k]]=1;}return dis;}void Dfs(int x){if (!R[0]) num=min(num,x);else if (x+A()<num){int now=R[0];F(i,R,0) if (cnt[now]>cnt[i]) now = i;F(i,D,now){Remove(i);F(j,R,i) Remove(j);Dfs(x+1);F(j,L,i) Resume(j);Resume(i);}}}void mul(){num=0x7FFFFFFF;}//重复覆盖}dlx;void make(int n){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]=point(i,j,k);}void Make(int n){for (int i=0;i<=n;i++){for (int j=0;j<n;j++) a[++tot1]=point(i,j,0);if (i==n) continue; for (int j=0;j<=n;j++) a[++tot1]=point(i,j,1);}}bool get(point &a,point&b){if (a.x==b.x&&a.z==0&&a.y<b.y+b.z&&a.y>=b.y) return true;if (a.y==b.y&&a.z==1&&a.x<b.x+b.z&&a.x>=b.x) return true;if (a.x==b.x+b.z&&a.z==0&&a.y<b.y+b.z&&a.y>=b.y) return true;if (a.y==b.y+b.z&&a.z==1&&a.x<b.x+b.z&&a.x>=b.x) return true;return false;}void reset(){memset(vis,0,sizeof(vis));dlx.mul();tot1=tot2=0;make(n);Make(n);dlx.reset(tot1,tot2);while (m--) {scanf("%d",&x);for (int i=1;i<=tot2;++i)if (!vis[i]&&get(a[x],b[i])) {dlx.R[dlx.L[i]]=dlx.R[i];dlx.L[dlx.R[i]]=dlx.L[i];vis[i]=1;}}for (int i=1;i<=tot1;++i)for (int j=1;j<=tot2;++j)if (!vis[j]&&get(a[i],b[j])) dlx.insert(i,j);}int main(){read(T);while (T--){scanf("%d%d",&n,&m);reset();dlx.Dfs(0);printf("%d\n",dlx.num);}return 0;}


0 0
原创粉丝点击