The n queen problem implemented by bit operations

Source: Internet
Author: User

C language is the first language I learned in my professional courses in my freshman year, almost five years ago. I recently bought a copy of The Plauger bull <The standard C library>. I simply flipped it over and thought I was just like a slot... In the past, the understanding of C was just superficial. I do not know that this extremely good cross-platform portable compilation language has such a wonderful implementation.
I sorted out the document a few days ago and found this section of the N queen algorithm implemented by C bit. It took me half a day to figure it out .. As we all know, the general solution of Queen n is backtracking. We need to use a two-dimensional array to represent the chessboard and perform a search by row (Column). We need to judge each current solution, if it is successful, it continues; if it fails, it goes back.
The Code implemented with bit is extremely concise. However, it only uses a one-dimensional array and searches by row to normalize the restrictions on the column and object line.
Interested friends can look at: html "> http://jsomers.com/nqueen_demo/nqueens.html author method is similar to the following method.
1 # include <stdio. h>
2 # include <stdlib. h>
3 # include <time. h>
4
5 long int sum = 0, upperlim = 1;
6
7 void test (long row, long ld, long rd)
8 {
9 // printf ("% ld", row, ld, rd );
10 // printf ("% ld", row );
11 // printf ("% ld", upperlim );
12 if (row! = Upperlim)
13 {
14 long pos = upperlim &~ (Row | ld | rd );
15 // printf ("% ld", pos );
16 while (pos! = 0)
17 {
18 // printf ("% ld", pos );
19 long p = pos &-pos;
20 pos = pos-p; // obtain the rightmost column of the Queen.
21 test (row + p, (ld + p) <1, (rd + p)> 1 );
22}
23 return;
24}
25 else
26 {
27 sum ++;
28 return;
29}
30}
31
32 int main (int argc, char * argv [])
33 {
34 time_t tm;
35 int n = 16;
36 if (argc! = 1) n = atoi (argv [1]);
37 tm = time (0 );
38 if (n <1) | (n> 32 ))
39 {
40 printf ("only Queen issues between 1-32 can be calculated ");
41 exit (-1 );
42}
43 printf ("% d ", n );
44 printf ("% ld", upperlim );
45 upperlim = (upperlim <n)-1; // all bits are 1
46 printf ("% ld", upperlim );
47 test (0, 0 );
48 printf ("% ld in total, calculation time % d seconds", sum, (int) (time (0)-tm ));
49 system ("pause ");
50 return 0;
51}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.