Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // % g++ --version | head -1
- // g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4)
- // % grep "model name" /proc/cpuinfo | uniq
- // model name : Intel(R) Xeon(R) CPU E5-4650 0 @ 2.70GHz
- // % g++ -O3 -o queens queens.cpp
- // % time ./queens
- // ./queens 0.72s user 0.00s system 99% cpu 0.724 total
- // % perf stat ./queens
- //
- // Performance counter stats for './queens':
- //
- // 731.375877 task-clock # 0.998 CPUs utilized
- // 2 context-switches # 0.000 M/sec
- // 0 CPU-migrations # 0.000 M/sec
- // 229 page-faults # 0.000 M/sec
- // 1,958,462,628 cycles # 2.678 GHz [83.33%]
- // 365,587,321 stalled-cycles-frontend # 18.67% frontend cycles idle [83.33%]
- // 243,090,347 stalled-cycles-backend # 12.41% backend cycles idle [66.66%]
- // 4,704,710,704 instructions # 2.40 insns per cycle
- // # 0.08 stalled cycles per insn [83.33%]
- // 735,945,018 branches # 1006.247 M/sec [83.32%]
- // 19,752,991 branch-misses # 2.68% of all branches [83.36%]
- //
- // 0.733080675 seconds time elapsed
- #include <algorithm>
- // #include <cstdio>
- using namespace std;
- int main( int argc, char **argv ) {
- int sols = 0;
- for ( int reps = 0; reps < 10000; ++reps ) {
- int cols[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
- int flags[ 30 ] = { 0 };
- int *nw = flags, *ne = flags + 22;
- sols = 0;
- for ( int it = 1; ; ++it ) {
- for ( int row = 0; row < 8; ++row ) {
- if ( nw[ row + cols[ row ] ] == it ||
- ne[ row - cols[ row ] ] == it ) {
- reverse( cols + row + 1, cols + 8 );
- goto fail;
- }
- nw[ row + cols[ row ] ] = it;
- ne[ row - cols[ row ] ] = it;
- }
- // for ( int row = 0; row < 8; ++row )
- // printf( "%s*%s\n", "......." + 7 - cols[ row ],
- // "......." + cols[ row ] );
- // printf( "\n" );
- ++sols;
- fail:
- if ( !next_permutation( cols, cols + 8 ) )
- break;
- }
- }
- return sols;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement