## FZU 1686 神龙的难题 （重复覆盖）

Problem 1686 神龙的难题

## Sample Input

4 4 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 2 2 4 4 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 2 2

4 1

## Source

FOJ月赛-2009年2月- TimeLoop

1为列，可以操作的为行

  1 /* ***********************************************
2 Author        :kuangbin
3 Created Time  :2014/5/27 17:53:47
4 File Name     :E:\2014ACM\专题学习\DLX\FZU1686.cpp
5 ************************************************ */
6
7 #include <stdio.h>
8 #include <string.h>
9 #include <iostream>
10 #include <algorithm>
11 #include <vector>
12 #include <queue>
13 #include <set>
14 #include <map>
15 #include <string>
16 #include <math.h>
17 #include <stdlib.h>
18 #include <time.h>
19 using namespace std;
20 const int MaxM = 15*15+10;
21 const int MaxN = 15*15+10;
22 const int maxnode = MaxN * MaxM;
23 const int INF = 0x3f3f3f3f;
24 struct DLX
25 {
26     int n,m,size;
27     int U[maxnode],D[maxnode],R[maxnode],L[maxnode],Row[maxnode],Col[maxnode];
28     int H[MaxN],S[MaxM];
29     int ansd;
30     void init(int _n,int _m)
31     {
32         n = _n;
33         m = _m;
34         for(int i = 0;i <= m;i++)
35         {
36             S[i] = 0;
37             U[i] = D[i] = i;
38             L[i] = i-1;
39             R[i] = i+1;
40         }
41         R[m] = 0; L[0] = m;
42         size = m;
43         for(int i = 1;i <= n;i++)H[i] = -1;
44     }
45     void Link(int r,int c)
46     {
47         ++S[Col[++size]=c];
48         Row[size] = r;
49         D[size] = D[c];
50         U[D[c]] = size;
51         U[size] = c;
52         D[c] = size;
53         if(H[r] < 0)H[r] = L[size] = R[size] = size;
54         else
55         {
56             R[size] = R[H[r]];
57             L[R[H[r]]] = size;
58             L[size] = H[r];
59             R[H[r]] = size;
60         }
61     }
62     void remove(int c)
63     {
64         for(int i = D[c];i != c;i = D[i])
65             L[R[i]] = L[i], R[L[i]] = R[i];
66     }
67     void resume(int c)
68     {
69         for(int i = U[c];i != c;i = U[i])
70             L[R[i]] = R[L[i]] = i;
71     }
72     bool v[MaxM];
73     int f()
74     {
75         int ret = 0;
76         for(int c = R[0]; c != 0;c = R[c])v[c] = true;
77         for(int c = R[0]; c != 0;c = R[c])
78             if(v[c])
79             {
80                 ret++;
81                 v[c] = false;
82                 for(int i = D[c];i != c;i = D[i])
83                     for(int j = R[i];j != i;j = R[j])
84                         v[Col[j]] = false;
85             }
86         return ret;
87     }
88     void Dance(int d)
89     {
90         if(d + f() >= ansd)return;
91         if(R[0] == 0)
92         {
93             if(d < ansd)ansd = d;
94             return;
95         }
96         int c = R[0];
97         for(int i = R[0];i != 0;i = R[i])
98             if(S[i] < S[c])
99                 c = i;
100         for(int i = D[c];i != c;i = D[i])
101         {
102             remove(i);
103             for(int j = R[i];j != i;j = R[j])remove(j);
104             Dance(d+1);
105             for(int j = L[i];j != i;j = L[j])resume(j);
106             resume(i);
107         }
108     }
109 };
110 DLX g;
111
112 int a[20][20];
113 int id[20][20];
114
115 int main()
116 {
117     //freopen("in.txt","r",stdin);
118     //freopen("out.txt","w",stdout);
119     int n,m;
120     while(scanf("%d%d",&n,&m) == 2)
121     {
122         int sz = 0;
123         memset(id,0,sizeof(id));
124         for(int i = 0;i < n;i++)
125             for(int j = 0;j < m;j++)
126             {
127                 scanf("%d",&a[i][j]);
128                 if(a[i][j] == 1)id[i][j] = (++sz);
129             }
130         g.init(n*m,sz);
131         sz = 1;
132         int n1,m1;
133         scanf("%d%d",&n1,&m1);
134         for(int i = 0;i < n;i++)
135             for(int j = 0;j < m;j++)
136             {
137                 for(int x = 0;x < n1 && i + x < n;x++)
138                     for(int y = 0;y < m1 && j + y < m;y++)
139                         if(id[i+x][j+y])
141                 sz++;
142             }
143         g.ansd = INF;
144         g.Dance(0);
145         printf("%d\n",g.ansd);
146     }
147     return 0;
148 }

posted on 2014-05-27 18:20  kuangbin  阅读(2555)  评论(0编辑  收藏  举报

JAVASCRIPT: