/* FILE: randEqualize.cpp last change: 31-Jul-2013 author: Romeo Rizzi * Usage syntax: * > randPermutGame m n opt seed */ #include #include #include #include using namespace std; const int MAX_N = 10000000; int n; const int MAX_M = 20; int m; int opt; int perm[MAX_M+1][MAX_N+1]; // le m righe int val_perm[MAX_N+1]; // rinumerazione valori da 1 a n int col_perm[MAX_N+1]; // rinumerazione colonne da 1 a n int row_missing[MAX_N+1]; // riga cui manca un valore inline void swap(int &a, int &b) { int tmp = a; a = b; b = tmp; } void randomPerm(int *perm, int n) { for(int i=n; i > 1; i--) swap( perm[i], perm[ (rand() % i) + 1 ] ); } int RandNumber(int min, int max) { /* returns an integer in [min, max] * see Stroustrup "The c++ Programming Language" 3th edition pg. 685 * for comments on the following manipulation choice. * In particular, considerations on the bad quality of low bits come into account. */ return min + (int) ( (max-min +1) * (double( rand()-0.000000000001 ) / RAND_MAX ) ); } void display() { cout << m << " " << n << endl; for(int r = 1; r <= m; r++) { for(int i = 1; i <= n; i++) cout << perm[r][i] << " "; cout << endl; } } int main(int argc, char** argv) { m = atoi(argv[1]); n = atoi(argv[2]); opt = atoi(argv[3]); assert( m >= 2 ); srand(time(NULL)); if(argc > 4) srand( atoi(argv[4]) ); for(int r = 1; r <= m; r++) for(int i = 1; i <= n; i++) perm[r][i] = i; for(int val = 1; val <= n; val++) if( val > n - opt ) row_missing[val] = RandNumber(2, m); else row_missing[val] = -1; for(int val = n - opt +1; val <= n; val++) { int new_val = RandNumber(1, n); while( (new_val == val) || ( row_missing[new_val] == row_missing[val] ) ) new_val = RandNumber(1, n); perm[ row_missing[val] ][val] = new_val; } //display(); for(int i = 1; i <= n; i++) col_perm[i] = val_perm[i] = i; randomPerm( val_perm , n ); randomPerm( col_perm , n ); cout << m << " " << n << endl; for(int r = 1; r <= m; r++) { for(int i = 1; i <= n; i++) cout << val_perm[ perm[r][ val_perm[ i] ] ] << " "; cout << endl; } return 0; }