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 ;}