#include<cstdio> #include<cmath> #include<algorithm> #include<vector> #include<string> #include<functional> #include<ctype.h> #include<map> #define LL long long #define LD long double #define L(x) ((x).size()) #define FI first #define SE second #define MP make_pair #define PB push_back using namespace std; int n,b,r,a; char c; struct P{ int t[10]; void SET(int w){ LL s = 1<<(w%30); t[w/30]|= s; } bool is(int w){ LL s = 1<<(w%30); return (t[w/30]&s)!=0; } bool orr(P b){ for(int i=0;i<10;++i) t[i]|= b.t[i]; } }; void wyp(P x){ LL s; for(int i=0;i<20;++i){ s = 1<<(i%30); if(((x.t[i/30])&s)!=0)printf("1"); else printf("0"); }printf("\n"); } P ANDD(P a,P b){ P c; for(int i=0;i<10;++i) c.t[i] = (a.t[i])&(b.t[i]); return c; } bool comp(P a,P b){ for(int i=0;i<10;++i) if(a.t[i]!=b.t[i])return false; return true; } P act,need,nact,zero,all; P place[210]; int main(){ scanf("%d%d%d",&n,&b,&r); for(int i=0;i<n;++i){ for(int j=0;j<n;++j){ scanf(" %c",&c); if(c=='1') place[i].SET(j); } } for(int i=0;i<r;++i){ scanf("%d",&a); act.SET(a-1); } for(int i=0;i<b;++i)need.SET(i); for(int i=0;i<n;++i)all.SET(i); //printf("--\n"); //for(int i=0;i<n;++i)wyp(place[i]); //printf("--\n"); //wyp(act); for(int k=0;k<10000;++k){ nact = zero; if(comp(act,ANDD(need,act))){ printf("%d\n",k); return 0; } if(comp(act,all)){ printf("-1\n"); return 0; } for(int j=0;j<n;++j){ if(act.is(j)){ nact.orr(place[j]); } } //wyp(nact); act = nact; } printf("-1\n"); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | #include<cstdio> #include<cmath> #include<algorithm> #include<vector> #include<string> #include<functional> #include<ctype.h> #include<map> #define LL long long #define LD long double #define L(x) ((x).size()) #define FI first #define SE second #define MP make_pair #define PB push_back using namespace std; int n,b,r,a; char c; struct P{ int t[10]; void SET(int w){ LL s = 1<<(w%30); t[w/30]|= s; } bool is(int w){ LL s = 1<<(w%30); return (t[w/30]&s)!=0; } bool orr(P b){ for(int i=0;i<10;++i) t[i]|= b.t[i]; } }; void wyp(P x){ LL s; for(int i=0;i<20;++i){ s = 1<<(i%30); if(((x.t[i/30])&s)!=0)printf("1"); else printf("0"); }printf("\n"); } P ANDD(P a,P b){ P c; for(int i=0;i<10;++i) c.t[i] = (a.t[i])&(b.t[i]); return c; } bool comp(P a,P b){ for(int i=0;i<10;++i) if(a.t[i]!=b.t[i])return false; return true; } P act,need,nact,zero,all; P place[210]; int main(){ scanf("%d%d%d",&n,&b,&r); for(int i=0;i<n;++i){ for(int j=0;j<n;++j){ scanf(" %c",&c); if(c=='1') place[i].SET(j); } } for(int i=0;i<r;++i){ scanf("%d",&a); act.SET(a-1); } for(int i=0;i<b;++i)need.SET(i); for(int i=0;i<n;++i)all.SET(i); //printf("--\n"); //for(int i=0;i<n;++i)wyp(place[i]); //printf("--\n"); //wyp(act); for(int k=0;k<10000;++k){ nact = zero; if(comp(act,ANDD(need,act))){ printf("%d\n",k); return 0; } if(comp(act,all)){ printf("-1\n"); return 0; } for(int j=0;j<n;++j){ if(act.is(j)){ nact.orr(place[j]); } } //wyp(nact); act = nact; } printf("-1\n"); } |