Ural 1034 queens in peaceful positions

Source: Internet
Author: User

Ural_1034

It is very similar to the classic n queen problem. Although N is relatively large, it is possible to reschedule only three queens in the case of C (n, 3, each queen can be placed in at most three positions, so the maximum is C (50, 3) * 3 ^.

For more information about the n queen of bitwise computing, see the matrix67 blog: http://www.matrix67.com/blog/archives/266.

# Include <stdio. h> # Include < String . H> # Define Maxd 60 Int  N, CNT, visr [maxd], VISC [maxd], vis [maxd] [maxd];  Struct  Point { Int  X, Y;} p [maxd];  Void  Init (){  Int  I; memset (VIS,  0 , Sizeof  (VIS ));  For (I = 0 ; I <n; I ++ ) {Scanf (  "  % D  " , & P [I]. X ,&P [I]. Y); vis [p [I]. x] [p [I]. Y] = 1  ;}}  Int Precheck ( Int X, Int Y, Int  Z ){  Int  I, J, K; memset (visr,  0 , Sizeof  (Visr); memset (VISC,  0 ,Sizeof  (VISC ));  For (I = 0 ; I <n; I ++ )  If (I! = X & I! = Y & I! = Z ){  If  (Visr [p [I]. x])  Return   0  ; Visr [p [I]. x] = P [I]. Y;  If (VISC [p [I]. Y])  Return   0  ; VISC [p [I]. Y] = P [I]. X ;}}  Long   Long  Cstate (){  Int  I;  Long   Long Cs = 0  ;  For (I =1 ; I <= N; I ++ )  If  (VISC [I]) CS | = 1ll < I;  Return  CS ;}  Void DFS ( Int Cur, Long   Long CS, Long   Long Ls, Long   Long RS ){  Int  Y;  Long   Long  St;  If (Cur> N ){ ++ CNT;  Return  ;}  If (Y = Visr [cur]) {St = Ls | RS;  If (St & (1ll < Y ))  Return  ; DFS (cur + 1 , Cs, (LS | (1ll <y) < 1 , (RS | (1ll <y)> 1  );}  Else  {St = Cs | ls | RS;  For (Y = 1 ; Y <= N; y ++)  If (St & (1ll <y) = 0 &&! Vis [cur] [Y]) DFS (cur + 1 , CS | (1ll <Y), (LS | (1ll <y) < 1 , (RS | (1ll <y)> 1  );}}  Void  Solve (){  Int  I, J, K, P; CNT = 0 ;  For (I = 0 ; I <n; I ++ )  For (J = I + 1 ; J <n; j ++ )  For (K = J + 1 ; K <n; k ++ )  If  (Precheck (I, j, k) DFS (  1 , Cstate (),0 , 0  ); Printf (  "  % D \ n  "  , CNT );}  Int  Main (){  While (Scanf ( "  % D  " , & N) = 1  ) {Init ();  If (N <4  ) Printf (  "  0 \ n  "  );  Else  Solve ();}  Return   0  ;} 

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.