// Artur Kraska, II UWr #include <algorithm> #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #include <cmath> #include <list> #include <set> #include <map> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(typeof(coll.begin()) iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(typeof(coll.rbegin()) iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #define FOR(i, a, b) for(int i=a; i<=b; i++) #define FORD(i, a, b) for(int i=a; i>=b; i--) #define MP make_pair #define PB push_back #define deb(X) X; #define M 1000000007 #define INF 1000000007 using namespace std; #define typ int[10]; int n, b, r, dl, ile, a; char slowo[207]; set <pair<pair<long long, long long>, pair<long long, long long> > > byl; pair<pair<long long, long long>, pair<long long, long long> > pp; int m[207][20]; int maska[20], poprz[20]; int tab[20][1100][20]; void ustaw_bit(int w[20], int nr) { w[nr/dl] |= (1<<(nr%dl)); } void abcd(int w[20]) { long long a; a = 0; FOR(i, 0, 5) a |= w[i] << (i*dl); pp.first.first = a; a = 0; FOR(i, 0, 5) a |= w[i+6] << ((i)*dl); pp.first.second = a; a = 0; FOR(i, 0, 5) a |= w[i+12] << (i*dl); pp.second.first = a; a = 0; FOR(i, 0, 1) a |= w[i+18] << (i*dl); pp.second.second = a; } void czytaj_maske(int nr) { scanf("%s", slowo); forr(i, n) if(slowo[i] == '1') ustaw_bit(m[nr], i); // forr(i, n) // forr(j, ile) // cout << "maska[" << i << "][" << j << "] = " << m[i][j] << endl; } void tablicuj() { forr(i, ile) { int d = 0; FOR(maska, 1, (1<<dl)-1) { if((maska & (1<<d)) > 0) d++; forr(j, ile) { tab[i][maska][j] = tab[i][maska - (1<<(d-1))][j] | m[i*dl+d-1][j]; } } } } bool jest_ok(int w[20]) { FOR(i, b, n-1) { if((w[i/dl] & (1<<(i%dl))) > 0) return 0; } return 1; } int dzialaj() { int res = 0; while(1) { // forr(i, n) // printf("%d", (((maska[i/dl])&(1<<(i%dl))) > 0) ? 1 : 0); // printf("\n"); if(jest_ok(maska)) return res; res++; abcd(maska); if(CONTAIN(pp, byl)) return -1; byl.insert(pp); forr(i, ile) { poprz[i] = maska[i]; maska[i] = 0; } forr(i, ile) forr(j, ile) maska[j] |= tab[i][poprz[i]][j]; } } int main() { scanf("%d %d %d", &n, &b, &r); dl = 10; ile = (n+dl-1)/dl; forr(i, n) { czytaj_maske(i); } forr(i, r) { scanf("%d", &a); a--; ustaw_bit(maska, a); } tablicuj(); printf("%d\n", dzialaj()); return 0; }
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | // Artur Kraska, II UWr #include <algorithm> #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #include <cmath> #include <list> #include <set> #include <map> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(typeof(coll.begin()) iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(typeof(coll.rbegin()) iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #define FOR(i, a, b) for(int i=a; i<=b; i++) #define FORD(i, a, b) for(int i=a; i>=b; i--) #define MP make_pair #define PB push_back #define deb(X) X; #define M 1000000007 #define INF 1000000007 using namespace std; #define typ int[10]; int n, b, r, dl, ile, a; char slowo[207]; set <pair<pair<long long, long long>, pair<long long, long long> > > byl; pair<pair<long long, long long>, pair<long long, long long> > pp; int m[207][20]; int maska[20], poprz[20]; int tab[20][1100][20]; void ustaw_bit(int w[20], int nr) { w[nr/dl] |= (1<<(nr%dl)); } void abcd(int w[20]) { long long a; a = 0; FOR(i, 0, 5) a |= w[i] << (i*dl); pp.first.first = a; a = 0; FOR(i, 0, 5) a |= w[i+6] << ((i)*dl); pp.first.second = a; a = 0; FOR(i, 0, 5) a |= w[i+12] << (i*dl); pp.second.first = a; a = 0; FOR(i, 0, 1) a |= w[i+18] << (i*dl); pp.second.second = a; } void czytaj_maske(int nr) { scanf("%s", slowo); forr(i, n) if(slowo[i] == '1') ustaw_bit(m[nr], i); // forr(i, n) // forr(j, ile) // cout << "maska[" << i << "][" << j << "] = " << m[i][j] << endl; } void tablicuj() { forr(i, ile) { int d = 0; FOR(maska, 1, (1<<dl)-1) { if((maska & (1<<d)) > 0) d++; forr(j, ile) { tab[i][maska][j] = tab[i][maska - (1<<(d-1))][j] | m[i*dl+d-1][j]; } } } } bool jest_ok(int w[20]) { FOR(i, b, n-1) { if((w[i/dl] & (1<<(i%dl))) > 0) return 0; } return 1; } int dzialaj() { int res = 0; while(1) { // forr(i, n) // printf("%d", (((maska[i/dl])&(1<<(i%dl))) > 0) ? 1 : 0); // printf("\n"); if(jest_ok(maska)) return res; res++; abcd(maska); if(CONTAIN(pp, byl)) return -1; byl.insert(pp); forr(i, ile) { poprz[i] = maska[i]; maska[i] = 0; } forr(i, ile) forr(j, ile) maska[j] |= tab[i][poprz[i]][j]; } } int main() { scanf("%d %d %d", &n, &b, &r); dl = 10; ile = (n+dl-1)/dl; forr(i, n) { czytaj_maske(i); } forr(i, r) { scanf("%d", &a); a--; ustaw_bit(maska, a); } tablicuj(); printf("%d\n", dzialaj()); return 0; } |