Easy Finding POJ - 3740 (DLX)

显然这是一道dfs简单题

或许匹配也能做

然而用了dancing links

显然这也是一道模板题

好的吧

调了一上午 终于弄好了模板

Easy Finding
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 19052 Accepted: 5273

Description

Given a  M× N matrix  AA ij ∈ {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 are multiple cases ended by  EOF. Test case up to 500.The first line of input is  MN ( M ≤ 16,  N ≤ 300). The next  M lines every line contains  N integers separated by space.

Output

For each test case, if you could find it output "Yes, I found it", otherwise output "It is impossible" per line.

Sample Input

3 3
0 1 0
0 0 1
1 0 0
4 4
0 0 0 1
1 0 0 0
1 1 0 1
0 1 0 0

Sample Output

Yes, I found it
It is impossible

Source

 

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 11000, INF = 0x7fffffff;

int S[maxn], head[maxn], vis[maxn];
int U[maxn], D[maxn], L[maxn], R[maxn];
int C[maxn], X[maxn];
int n, m, ans, ret;

void init()
{
    for(int i = 0; i <= m; i++)
        D[i] = i, U[i] = i, R[i] = i + 1, L[i] = i - 1;
    L[0] = m, R[m] = 0;
    mem(S, 0), mem(head, -1);
    ans = m + 1;
}

void delc(int c)
{
    L[R[c]] = L[c], R[L[c]] = R[c];
    for(int i = D[c]; i != c; i = D[i])
        for(int j = R[i]; j != i; j = R[j])
            U[D[j]] = U[j], D[U[j]] = D[j], S[C[j]]--;

}

void resc(int c)
{
    for(int i = U[c]; i != c; i = U[i])
        for(int j = L[i]; j != i; j = L[j])
            U[D[j]] = j, D[U[j]] = j, S[C[j]]++;
    L[R[c]] = c, R[L[c]] = c;
}

void add(int r, int c)
{
    ans++, S[c]++, C[ans] = c, X[ans] = r;
    D[ans] = D[c];
    U[ans] = c;
    U[D[c]] = ans;
    D[c] = ans;
    if(head[r] < 0) head[r] = L[ans] = R[ans] = ans;
    else L[ans] = head[r], R[ans] = R[head[r]],L[R[head[r]]] = ans, R[head[r]] = ans;
}


bool dfs(int sh)
{
    if(!R[0])
    {
        ret = sh;
        return true;
    }
    int c = R[0];
    delc(c);
    for(int i = D[c]; i != c; i = D[i])
    {
        vis[sh] = i;
        for(int j = R[i]; j != i; j = R[j])
            delc(C[j]);
        if(dfs(sh + 1)) return true;
        for(int j = L[i]; j != i; j = L[j])
            resc(C[j]);
    }
    resc(c);
    return false;
}


int main()
{
    while(scanf("%d%d", &n, &m) != EOF)
    {
        init();
        int tmp;
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
            {
                rd(tmp);
                if(tmp) add(i, j);
            }
        if(dfs(0))
            printf("Yes, I found it\n");
        else
            printf("It is impossible\n");


    }

    return 0;
}

 

转载于:https://www.cnblogs.com/WTSRUVF/p/10613348.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值