N Queen's questionTime
limit:1000MS
Memory Limit:32768KB
64bit IO Format:%i64d &%i64 U
Description
N-Queens are placed in the N*n's checkered checkerboard, making them not attack each other (that is, any 2 queens are not allowed to be in the same row, in the same column, or in a diagonal line with a 45-angle checkerboard border.)
Your task is to find out how many legal placement methods are available for a given n.
Input
There are several lines, one positive integer n≤10 per line, indicating the number of boards and queens, or, if n=0, the end.
Output
A number of rows, one positive integer per line, representing the number of different placements of the queen corresponding to the input row.
Sample Input
1850
Sample Output
19210
simple translation of the topic:Forget it, Chinese .... Do I have to translate into English? Solution thinking: Depth First search (DFS) line down search, if two points (x1,y1), (X2,y2) on the same slash, then x1+y1=x2+y2 or x1-y1=x2-y2, so we just judge four conditions, we can determine whether two points can attack each other.
Code:
1#include <cstdio>2#include <cstring>3 using namespacestd;4 Const intinf=0x3f3f3f3f;5 intans[ A],n,vis[4][ +];6 intsum;7 BOOLCheckintXintN//Check (x,n) Whether this point can be attacked to8 {9 returnvis[0][x]==0&&vis[1][n]==0&&vis[2][x+n]==0&&vis[3][ -+x-n]==0;Ten } One voidSet_value (intXintNintvalue) A { -vis[0][x]=vis[1][n]=vis[2][x+n]=vis[3][ -+x-n]=value; - } the voidDfsintx) - { - if(x>=n)//If you have already filled in n points, then the result adds a - { +sum++; - return; + } A for(intI=0; i<n;i++) at if(check (x,i)) - { -Set_value (X,i,1); -DFS (x+1); -Set_value (X,i,0); - } in } - intSolveinta) to { +sum=0; -memset (Vis,0,sizeofvis); theDfs0); * returnsum; $ }Panax Notoginseng - intMain () the { +memset (ans,0x3f,sizeofans); A while(SCANF ("%d", &n)!=eof&&N) the { + if(Ans[n]==inf) ans[n]=solve (n); -printf"%d\n", Ans[n]); $ } $ return 0; -}
N Queen's question
HDU 2553 N Queen problem (Dfs)