FZU 1686 神龍的難題(DLX重複覆蓋),fzudlx

來源:互聯網
上載者:User

FZU 1686 神龍的難題(DLX重複覆蓋),fzudlx
FZU 1686 神龍的難題

題目連結

題意:中文題

思路:每一個1看成列,每個位置作為左上方的矩陣看成行,dlx重複覆蓋即可

代碼:

#include <cstdio>#include <cstring>using namespace std;const int MAXNODE = 66666;const int INF = 0x3f3f3f3f;const int MAXM = 230;const int MAXN = 230;int K;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) {this->n = n;this->m = m;ansd = INF;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) {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;}bool v[MAXNODE];int f() {int ret = 0;for(int c = R[0]; c != 0; c = R[c]) v[c] = true;for(int c = R[0]; c != 0; c = R[c]) {if(v[c]) {ret++;v[c] = false;for(int i = D[c]; i != c; i = D[i])for(int j = R[i]; j != i; j = R[j])v[col[j]] = false;}}return ret;}void Dance(int d) {if(d + f() >= ansd) return;if(R[0] == 0) {if (d < ansd) ansd = d;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]) {remove(i);for(int j = R[i]; j != i; j = R[j]) remove(j);ans[d] = row[i];Dance(d + 1);for(int j = L[i]; j != i; j = L[j]) resume(j);resume(i);}}} gao;const int N = 20;int n, m, g[N][N], n1, m1, to[N][N];int main() {while (~scanf("%d%d", &n, &m)) {int cnt = 0;for (int i = 0; i < n; i++)for (int j = 0; j < m; j++) {scanf("%d", &g[i][j]);if (g[i][j]) to[i][j] = ++cnt;}scanf("%d%d", &n1, &m1);gao.init(n * m, cnt);for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {for (int x = 0; x < n1 && i + x < n; x++) {for (int y = 0; y < m1 && j + y < m; y++) {if (g[i + x][j + y]) gao.Link(i * m + j + 1, to[i + x][j + y]);}}}}gao.Dance(0);printf("%d\n", gao.ansd);}return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.