/* FILE: randSeq.cpp last change: 23-July-2012 author: Romeo Rizzi * This program generates a sequence of n random integers. * Usage syntax: * > randSeq out_file n k MIN_MAV MAX_VAL seed */ #include #include #include #include #include using namespace std; const int MAX_N = 1000; const int MAX_L = 1000; int N, L; char source[MAX_N][MAX_L]; char target[MAX_N][MAX_L]; void trigger(char m[][MAX_L], int p); void printM(char m[][MAX_L]); bool testWithTarget(char m1[][MAX_L]); void sortM(char m[][MAX_L]); int main(int argc, char** argv) { ifstream fin("input.txt"); assert(fin); fin >> N >> L; for(int i = 0; i < N; i++) fin >> source[i]; for(int i = 0; i < N; i++) fin >> target[i]; fin.close(); //cout << "target disordinato:\n"; //printM(target); sortM(target); //cout << "target ordinato\n"; //printM(target); int max_cmb = pow(2, L); //cout << "origin:\n"; //printM(source); //cout << "--\n--\n"; int minMosse = L; int nowMosse = 0; bool almenoUnaSoluzione = false; for(int cmb = 0; cmb < max_cmb; cmb++) { nowMosse = 0; // (cmb >> j) & 0x1 => read j-esimo bit //cout << "cmb: " << cmb << "\n"; for(int j = 0; j < L; j++) { int p = (cmb >> j) & 0x01; if(p == 0) continue; trigger(source, j); nowMosse++; //printM(source); //cout << "--\n"; } if(testWithTarget(source)) { //cout << "i se somea\n"; almenoUnaSoluzione = true; if(nowMosse < minMosse) minMosse = nowMosse; } // torno indietro for(int j = 0; j < L; j++) { int p = (cmb >> j) & 0x01; if(p == 0) continue; trigger(source, j); } } ofstream fout("output.txt"); assert(fout); if(!almenoUnaSoluzione) { //cout << "NOT POSSIBLE\n"; fout << "NOT POSSIBLE"; } else { //cout << minMosse << "\n"; fout << minMosse; } fout.close(); } void trigger(char m[][MAX_L], int p) { for(int n = 0; n < N; n++) { if(m[n][p] == '1') m[n][p] = '0'; else m[n][p] = '1'; } } void printM(char m[][MAX_L]) { for(int i = 0; i < N; i++) { for(int j = 0; j < L; j++) cout << m[i][j]; cout << "\n"; } } bool testWithTarget(char m[][MAX_L]) { //cout << "disordinato\n"; //printM(m); sortM(m); //cout << "ordinato\n"; //printM(m); for(int i = 0; i < N; i++) for(int j = 0; j < L; j++) if(m[i][j] != target[i][j]) return false; return true; } int compareMyType (const void * a, const void * b) { char *r1 = (char*)a; char *r2 = (char*)b; int same = 0; for(int i = 0; i < L; i++) { if(r1[i] > r2[i]) { return 1; } if(r1[i] < r2[i]) { return -1; } if(r1[i] == r2[i]) same++; } if(same == L) return 0; return -1; } void sortM(char m[][MAX_L]) { qsort(m, N, MAX_L, compareMyType); }