poj 3740 Easy Finding(Dancing Links)

來源:互聯網
上載者:User

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.

Input

There are multiple cases ended by EOF. Test case up to 500.The first line of input is M, N (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 30 1 00 0 11 0 04 40 0 0 11 0 0 01 1 0 10 1 0 0

Sample Output

Yes, I found itIt is impossible

Source

POJ Monthly Contest - 2009.08.23, MasterLuo

題意:

給你一個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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.