题目大意:
就是现在给出一个n*m的01矩阵
每次可以选择将一个n1*m1的矩形中的所有1变成0
问将所有的1变成0需要使用n1*m1的矩形至少多少次
大致思路:
对于每一个位置作为列, 一共15*15列, 对于每一个可能选择的矩形都建立一行, 然后用Dancing Link做重复覆盖即可
代码如下:
Result : Accepted Memory : 2348 KB Time : 62 ms
/* * Author: Gatevin * Created Time: 2015/10/4 18:10:46 * File Name: Sakura_Chiyo.cpp */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> using namespace std; const double eps(1e-8); typedef long long lint; #define maxnode 90000 #define maxn 300 #define maxm 300 struct DLX { int n, m, size; int U[maxnode], D[maxnode], R[maxnode], L[maxnode], Row[maxnode], Col[maxnode]; int H[maxn], S[maxm]; int ansd, ans[maxn]; void init(int _n, int _m) { n = _n; m = _m; for(int i = 0; i <= m; i++) { S[i] = 0; U[i] = D[i] = i; L[i] = i - 1; R[i] = i + 1; } R[m] = 0; L[0] = m; size = m; for(int i = 1; i <= n; i++) H[i] = -1; } void Link(int r, int c) { ++S[Col[++size] = c]; Row[size] = r; D[size] = D[c]; U[D[c]] = size; U[size] = c; D[c] = size; if(H[r] < 0) H[r] = L[size] = R[size] = size; else { R[size] = R[H[r]]; L[R[H[r]]] = size; L[size] = H[r]; R[H[r]] = size; } } void remove(int c)//因为是重复覆盖每次讲某一列的所有1纳入不考虑范围 { //L[R[c]] = L[c]; R[L[c]] = R[c]; for(int i = D[c]; i != c; i = D[i]) L[R[i]] = L[i], R[L[i]] = R[i]; } void resume(int c) { for(int i = U[c]; i != c; i = U[i]) L[R[i]] = R[L[i]] = i; //L[R[c]] = R[L[c]] = c; } bool v[60]; int f()//A*搜索估价函数 { int ret = 0; for(int i = R[0]; i != 0; i = R[i]) v[i] = 1; for(int i = R[0]; i != 0; i = R[i]) if(v[i]) { ret++; v[i] = 0; for(int j = D[i]; j != i; j = D[j]) for(int k = R[j]; k != j; k = R[k]) v[Col[k]] = 0; } return ret; } int res; void Dance(int dep) { if(dep + f() >= res) return; if(R[0] == 0) { res = dep; return; } int c = R[0]; for(int i = R[0]; i != 0; i = R[i]) if(S[i] < S[c]) c = i; for(int i = D[c]; i != c; i = D[i]) { for(int j = R[i]; j != i; j = R[j]) remove(j); remove(i); Dance(dep + 1); resume(i); for(int j = L[i]; j != i; j = L[j]) resume(j); } return; } int maz[20][20]; int col[20][20]; void solve() { int N, M; while(~scanf("%d %d", &N, &M)) { int cnt = 0; for(int i = 1; i <= N; i++) for(int j = 1; j <= M; j++) { scanf("%d", &maz[i][j]); if(maz[i][j] == 1) col[i][j] = ++cnt; } int n1, m1; scanf("%d %d", &n1, &m1); init((N - n1 + 1)*(M - m1 + 1), cnt); for(int i = 1; i + m1 - 1 <= M; i++) for(int j = 1; j + n1 - 1 <= N; j++) { int row = (j - 1)*(M - m1 + 1) + i; for(int x = 1; x <= m1; x++) for(int y = 1; y <= n1; y++) { int px = i + x - 1; int py = j + y - 1; if(maz[py][px] == 1) Link(row, col[py][px]); } } res = (N - n1 + 1)*(M - m1 + 1); Dance(0); printf("%d\n", res); } return; } }; DLX dlx; int main() { dlx.solve(); return 0; }