Storing valid dimensions as for every ( x , y ) both ( x , y ) and ( y , x ) are valid
for ( int i = 0 ; i < K ; i ++ ) { ok [ dimensions [ i ] . first ] [ dimensions [ i ] . second ] = 1 ; ok [ dimensions [ i ] . second ] [ dimensions [ i ] . first ] = 1 ; }