Because there are only 10 sets of data, to prevent a data occurrence multiple times resulting in timeouts so hit the table
It's quite simple ....
1 /* 2 is to consider where the Queen is placed, and for each row we need to enumerate each place where we can place the Queen,3 It is also necessary to determine whether the current position (line i) satisfies the condition, that is, to determine whether this position conflicts with the position of the Queen placed in the former i-1 line,4 If there is a conflict, the location is inappropriate; otherwise, you can enumerate the position of the next row of Queens until the nth row. 5 */6#include <stdio.h>7#include <string.h>8 9 intn,tmp;Ten intmap[ One]; One A voidDFS (intk) - { - intI,j,flag; the if(k==n+1) - { -tmp++; - return; + } - Else + { A for(i=1; i<=n;++i) at { -map[k]=i; -flag=1; - for(j=1; j<k;++j) - { - if(map[j]==i| | i-k==map[j]-j| | I+K==MAP[J]+J)//Note: 1, i=map[k] 2, not the same slash two point meaning is the line marked to the diagonal distance is not equal in { -flag=0; to Break; + } - } the if(flag) *DFS (k +1); $ }Panax Notoginseng } - } the + intMain () A { the inti,m; + intans[ One]; - for(n=1; n<=Ten;++N) $ { $tmp=0; -DFS (1); -ans[n]=tmp; the } - while(SCANF ("%d",&m), m)Wuyi { theprintf"%d\n", Ans[m]); - } Wu return 0; -}
The key is the record and expression of the used rows and columns
N Queen's question