HUST 1017 Exact cover (Dancing links)

本文探讨了1017-Exactcover问题,该问题是选择矩阵中某些行使得每列恰好有一个1的问题。文章提供了一种解决方法,并通过C++实现,包括详细的数据结构定义和算法流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1017 - Exact cover

时间限制:15秒 内存限制:128兆

自定评测 6110 次提交 3226 次通过
题目描述
There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in exactly one of the selected rows. Try to find out the selected rows.
输入
There are multiply test cases. First line: two integers N, M; The following N lines: Every line first comes an integer C(1 <= C <= 100), represents the number of 1s in this row, then comes C integers: the index of the columns whose value is 1 in this row.
输出
First output the number of rows in the selection, then output the index of the selected rows. If there are multiply selections, you should just output any of them. If there are no selection, just output "NO".
样例输入
6 7
3 1 4 7
2 1 4
3 4 5 7
3 3 5 6
4 2 3 6 7
2 2 7
样例输出
3 2 4 6
提示
来源
dupeng

 

 

 

 

 

 

本人智商奇低,看了三天才学会,模板验证题。

顺便一提这是正式转入C++后第一A。

  1 #include<iostream>
  2 #include<cstdio>
  3 using    namespace    std;
  4 
  5 const    int    HEAD = 0;
  6 const    int    N = 1005;
  7 int    MAP[N][N];
  8 int    U[N * N],D[N * N],L[N * N],R[N * N],H[N * N],C[N * N],ANS[N * N];
  9 
 10 void    ini(int col);
 11 bool    dancing(int k);
 12 void    output(void);
 13 void    remove(int c);
 14 void    resume(int c);
 15 int    main(void)
 16 {
 17     int    n,m,num,col;
 18     int    count,front,first;
 19 
 20     while(cin >> n >> m)
 21     {
 22         ini(m);
 23 
 24         count = m + 1;
 25         for(int i = 1;i <= n;i ++)
 26         {
 27             cin >> num;
 28             front = first = count;
 29             while(num --)
 30             {
 31                 cin >> col;
 32 
 33                 U[count] = U[col];
 34                 D[count] = col;
 35                 L[count] = front;
 36                 R[count] = first;
 37 
 38                 D[U[col]] = count;
 39                 U[col] = count;
 40                 R[front] = count;
 41 
 42                 H[count] = i;
 43                 C[count] = col;
 44                 front = count;
 45                 count ++;
 46             }
 47             L[first] = count - 1;
 48         }
 49         if(!dancing(1))
 50             cout << "NO" << endl;
 51     }
 52 
 53     return    0;
 54 }
 55 
 56 void    ini(int col)
 57 {
 58     U[HEAD] = D[HEAD] = H[HEAD] = C[HEAD] = HEAD;
 59     R[HEAD] = 1;
 60     L[HEAD] = col;
 61 
 62     int    front = HEAD;
 63     for(int i = 1;i <= col;i ++)
 64     {
 65         U[i] = D[i] = i;
 66         L[i] = front;
 67         R[i] = HEAD;
 68         R[front] = i;
 69         front = i;
 70 
 71         C[i] = i;
 72         H[i] = 0;
 73     }
 74 }
 75 
 76 bool    dancing(int k)
 77 {
 78     int    c = R[HEAD];
 79     if(c == HEAD)
 80     {
 81         output();
 82         return    true;
 83     }
 84 
 85     remove(C[c]);
 86     for(int i = D[c];i != c;i = D[i])
 87     {
 88         ANS[k] = H[i];
 89         for(int j = R[i];j != i;j = R[j])
 90             remove(C[j]);
 91         if(dancing(k + 1))
 92             return    true;
 93         for(int j = L[i];j != i;j = L[j])
 94             resume(C[j]);
 95     }
 96     resume(C[c]);
 97 
 98     return    false;
 99 }
100 
101 void    output(void)
102 {
103     int    i,j;
104     for(i = 1;ANS[i];i ++);
105     cout << i - 1 << " ";
106     for(j = 1;j < i - 1;j ++)
107         cout << ANS[j] << " ";
108     cout << ANS[j] << endl;
109 }
110 
111 void    remove(int c)
112 {
113     R[L[c]] = R[c];
114     L[R[c]] = L[c];
115 
116     for(int i = D[c];i != c;i = D[i])
117         for(int j = R[i];j != i;j = R[j])
118         {
119             D[U[j]] = D[j];
120             U[D[j]] = U[j];
121         }
122 }
123 
124 void    resume(int c)
125 {
126     R[L[c]] = c;
127     L[R[c]] = c;
128 
129     for(int i = U[c];i != c;i = U[i])
130         for(int j = R[i];j != i;j = R[j])
131         {
132             D[U[j]] = j;
133             U[D[j]] = j;
134         }
135 }

 

转载于:https://www.cnblogs.com/xz816111/p/4332729.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值