程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HUST 1017 Exact cover (Dancing links),hustexact

HUST 1017 Exact cover (Dancing links),hustexact

編輯:C++入門知識

HUST 1017 Exact cover (Dancing links),hustexact


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 }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved