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  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

意:

给你一个n*m(n<=16,m<=300)的矩阵。矩阵的每个元素只能是0或1.现在问你能不能从里面选一些列出来使的没一列有且仅有一个1.

思路:

开始逗比了。把题看成每行只有16个。一看16就乐了。这是典型的壮压嘛。把每行压成长度为16的二进制数。然后就是可行性判定了。写完一交re了。这还用说。m,n都看错了不re才怪。由于每行值个数太多,不能壮压了。无赖。只能百度。得知是Dancing Links.于是百度其资料。链接一枚

详细见代码:

#include<iostream>
#include<stdio.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=6010;
int U[maxn],D[maxn],L[maxn],R[maxn],C[maxn];//上下左右指针。c[i]结点i对应的列。
int S[310],H[310];//S[i]为i列1的个数。H[i]为i行的尾指针。
int n,m,cnt;
void init()
{
    int i;
    for(i=1;i<=n;i++)
        H[i]=-1;
    for(i=0;i<=m;i++)
    {
         S[i]=0;
         L[i+1]=i;
         R[i]=i+1;
         U[i]=D[i]=i;
    }
    R[m]=0;
    cnt=m+1;
}
void Insert(int r,int c)
{                        //头插法建链表
    U[cnt]=c,D[cnt]=D[c];//确定新增结点上下指针信息
    U[D[c]]=cnt,D[c]=cnt;//恢复链表信息
    if(H[r]==-1)         //确定左右指针信息
        H[r]=L[cnt]=R[cnt]=cnt;//加入头
    else
    {
        L[cnt]=H[r],R[cnt]=R[H[r]];//头插法
        L[R[H[r]]]=cnt,R[H[r]]=cnt;
    }
    S[c]++;//更新附加信息
    C[cnt++]=c;
}
void Remove(int c)//移除c列。
{
    int i,j;
    R[L[c]]=R[c],L[R[c]]=L[c];
    for(i=D[c];i!=c;i=D[i])
        for(j=R[i];j!=i;j=R[j])
            D[U[j]]=D[j],U[D[j]]=U[j],S[C[j]]--;
}
void Resume(int c)//还原c列。
{
    int i,j;
    R[L[c]]=L[R[c]]=c;
    for(i=D[c];i!=c;i=D[i])
        for(j=R[i];j!=i;j=R[j])
            D[U[j]]=U[D[j]]=j,S[C[j]]++;
}
bool dfs()
{
    if(R[0]==0)
        return true;
    int i,j,c,miv=INF;
    for(i=R[0];i;i=R[i])
        if(S[i]<miv)
            miv=S[i],c=i;
    Remove(c);//处理第c列
    for(i=D[c];i!=c;i=D[i])
    {
        for(j=R[i];j!=i;j=R[j])
            Remove(C[j]);
        if(dfs())
            return true;
        for(j=L[i];j!=i;j=L[j])
            Resume(C[j]);
    }
    Resume(c);
    return false;
}
int main()
{
    int op,i,j;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
            {
                scanf("%d",&op);
                if(op)
                    Insert(i,j);
            }
        if(dfs())
            printf("Yes, I found it\n");
        else
            printf("It is impossible\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值