Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <algorithm>
- #include <iostream>
- using namespace std;
- const int N = 5;
- int mask2bits[1 << N];
- int mask2bitsAllSet = 0; // 1023 = 1111111111 binary
- // count the number of bits set in x
- int popcnt( int x )
- {
- return x ? popcnt(x&(x-1))+1 : 0;
- }
- //
- void initMask2bits()
- {
- int ct = 0;
- for ( int i = 0; i < (1<<N); i++ )
- {
- int pop = popcnt( i );
- mask2bits[i] = pop == 2 ? 1 << ct++ : 0;
- }
- mask2bitsAllSet = (1<<ct)-1;
- }
- bool check( int a[], int b[] )
- {
- int m = 0;
- int masky = 0;
- // scan top-to-bottom left-to-right
- for ( int y = 0; y < N*2; y++ )
- {
- masky ^= 1 << b[y]; // masky keeps track of which rectangles are currently on, considering only y-coordinate
- // 00001 (binary) means just rectangle 0
- // 00010 (binary) means just rectangle 1
- // 00011 (binary) means just rectangle 0 and 1
- int maskx = 0;
- for ( int x = 0; x < N*2; x++ )
- {
- maskx ^= 1 << a[x]; // masky keeps track of which rectangles are currently on, considering only y-coordinate
- m |= mask2bits[maskx&masky]; // maskx&masky determines which rectangles are active at x,y
- // if that value has exactly 2 bits set, we keep track of that in 'm'
- // 'm' is another bitmask with 10 positions that must be set for a winner
- }
- }
- return m == mask2bitsAllSet;
- }
- int main(int n,char**p)
- {
- initMask2bits();
- int a[] = {0,1,2,3,4,0,1,2,3,4}; // relative order of rectangle start or stop on x-axis
- int b[] = {0,1,2,3,4,0,1,2,3,4}; // relative order of rectangle start or stop on y-axis
- int x = 0;
- do {
- do {
- do { // for every permutation of first/last 5 elements of y and last 5 elements of x
- if ( check( a, b ) ) // check if we have a winner
- {
- for ( int i = 0; i < N*2; i++ ) cout << a[i] << " "; cout << endl;
- for ( int i = 0; i < N*2; i++ ) cout << b[i] << " "; cout << endl;
- return 0;
- }
- } while( next_permutation( b+N, b+N*2 ) );
- } while( next_permutation( b+0, b+N ) );
- } while( next_permutation( a+N, a+N*2 ) );
- cout << "found no match" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement