POJ 3740 Easy Finding

题目链接:http://poj.org/problem?id=3740

dancing links 入门题

  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdio>
  4 #include <string>
  5 #include <iomanip>
  6 using namespace std;
  7 int M, N;
  8 #define maxn 16*300+5
  9 int R[maxn], L[maxn], U[maxn], D[maxn];
 10 int H[maxn], C[maxn], ans[20], colsum[300+5];
 11 int mp[18][310];
 12 int cnt, head;
 13 void addnode(int row, int col, int sum, int pre, int rowhead){
 14     cnt++;
 15     H[cnt] = row;
 16     C[cnt] = col;
 17
 18     if(pre == -1){
 19         R[cnt] = cnt; L[cnt] = cnt;
 20     }
 21     else if(sum == 2){
 22         R[pre] = cnt; R[cnt] = pre;
 23         L[pre] = cnt; L[cnt] = pre;
 24     }
 25     else{
 26         R[cnt] = rowhead; R[pre] = cnt;
 27         L[cnt] = pre; L[rowhead] = cnt;
 28     }
 29
 30     D[U[col]] = cnt;
 31     U[cnt] = U[col];
 32     D[cnt] = col;
 33     U[col] = cnt;
 34
 35     colsum[col]++;
 36 }
 37 void init(){
 38         cnt = 0; head = 0;
 39         memset(colsum, 0, sizeof(colsum));
 40         L[head] = N; R[head] = 1; D[head] = head; U[head] = head;
 41         for(int i = 1; i <= N; i++){
 42             cnt++;
 43             H[cnt] = 0; C[cnt] = i;
 44             U[cnt] = cnt; D[cnt] = cnt;
 45             if(i == N){
 46                 R[cnt-1] = cnt;
 47                 L[cnt] = cnt-1;
 48                 R[cnt] = head;
 49             }
 50             else{
 51                 R[cnt-1] = cnt;
 52                 L[cnt] = cnt-1;
 53             }
 54         }
 55         int temp;
 56         for(int i = 1; i <= M; i++){
 57             int sum = 0, pre = -1, rowhead;
 58             for(int j = 1; j <= N; j++){
 59                  scanf("%d", &temp);
 60                  if(temp == 1){
 61                      sum++;
 62                      addnode(i, j, sum, pre, rowhead);
 63                      if(sum == 1) rowhead = cnt;
 64                      pre = cnt;
 65                  }
 66             }
 67         }
 68 }
 69 void remove(int c){
 70     R[L[c]] = R[c];
 71     L[R[c]] = L[c];
 72     for(int i = D[c]; i != c; i = D[i]){
 73         for(int j = R[i]; j != i; j = R[j]){
 74             U[D[j]] = U[j];
 75             D[U[j]] = D[j];
 76             colsum[C[j]]--;
 77         }
 78     }
 79 }
 80 void resume(int c){
 81     R[L[c]] = c;
 82     L[R[c]] = c;
 83     for(int i = U[c]; i != c; i = U[i]){
 84         for(int j = R[i]; j != i; j = R[j]){
 85             U[D[j]] = j;
 86             D[U[j]] = j;
 87             colsum[C[j]]++;
 88         }
 89     }
 90 }
 91 bool dance(int k){
 92     int c = R[head];
 93     if(c == head){
 94         return true;
 95     }
 96     int min = 999999;
 97     for(int i = R[head]; i != head; i = R[i]){
 98         if(colsum[i] <= min){
 99             min = colsum[i];
100             c = i;
101         }
102     }
103     remove(c);
104     for(int i = D[c]; i != c; i = D[i]){
105         ans[k] = H[i];
106         for(int j = R[i]; j != i; j = R[j]) remove(C[j]);
107         if(dance(k+1)) return true;
108         for(int j = R[i]; j != i; j = R[j]) resume(C[j]);
109     }
110     resume(c);
111     return false;
112 }
113
114 int main(){
115     while(~scanf("%d%d", &M, &N)){
116         init();
117         if(dance(0)) printf("Yes, I found it\n");
118         else printf("It is impossible\n");
119     }
120
121     return 0;
122 }
时间: 2024-10-20 17:01:45

POJ 3740 Easy Finding的相关文章

poj 3740 Easy Finding(Dancing Links)

Easy Finding Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15668   Accepted: 4163 Description Given a M×N matrix A. Aij ∈ {0, 1} (0 ≤ i < M, 0 ≤ j < N), could you find some rows that let every cloumn contains and only contains one 1.

POJ 3740 Easy Finding(dfs回溯)

B - Easy Finding Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3740 Description Given a M× N matrix A. Aij ∈ {0, 1} (0 ≤ i < M, 0 ≤ j < N), could you find some rows that let every cloumn

[ACM] POJ 3740 Easy Finding (DLX模板题)

Easy Finding Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16178   Accepted: 4343 Description Given a M×N matrix A. Aij ∈ {0, 1} (0 ≤ i < M, 0 ≤ j < N), could you find some rows that let every cloumn contains and only contains one 1.

[ACM] POJ 3740 Easy Finding (DFS)

Easy Finding Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16482   Accepted: 4476 Description Given a M×N matrix A. Aij ∈ {0, 1} (0 ≤ i < M, 0 ≤ j < N), could you find some rows that let every cloumn contains and only contains one 1.

poj 3740 -- Easy Finding (dfs)

题目大意:给出一个m行n列的数组,元素只有0和1, 问:能不能找出几行,使得每一列都有且仅有一个1. 分析:直接深搜即可 #include<iostream> #include<cstdio> using namespace std; int vis[311];//记录该列有1没 int n, m; int a[20][311]; bool flag; bool fuhe(int i){ for (int j = 1; j <= n; j++) if (a[i][j] &am

[ACM] POJ 3740 Easy Finding (DFS)

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16202   Accepted: 4349 Description Given a M×N matrix A. Aij ∈ {0, 1} (0 ≤ i < M, 0 ≤ j < N), could you find some rows that let every cloumn contains and only contains one 1. Input There a

POJ 3740 Easy Finding DLX

题意:给你一个0,1矩阵 ,求精确覆盖 解题思路: DLX 解题代码: 1 // File Name: poj3740.cpp 2 // Author: darkdream 3 // Created Time: 2014年10月04日 星期六 20时06分31秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #i

POJ_3740 Easy Finding ——精确覆盖问题,DLX模版

Easy Finding Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18790   Accepted: 5184 Description Given a M×N matrix A. Aij ∈ {0, 1} (0 ≤ i < M, 0 ≤ j < N), could you find some rows that let every cloumn contains and only contains one 1.

(水 dfs) poj 3740

Easy Finding Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16854   Accepted: 4567 Description Given a M×N matrix A. Aij ∈ {0, 1} (0 ≤ i < M, 0 ≤ j < N), could you find some rows that let every cloumn contains and only contains one 1.