Topic link = = "http://www.bnuoj.com/v3/problem_show.php?pid=4299
Test instructions: A x*y on the chessboard put N Queens, and then put a queen, there are a few spaces can be placed, if and already on the board of Queen different rows of different columns of different slashes
Idea: Each point in the four straight lines, can not be put queen, so a bit of the line mark, and then traverse all points, if the point where the line has been marked, it is not selected. Finished
Code:
1#include <stdio.h>2#include <string.h>3#include <algorithm>4#include <iostream>5 6 using namespacestd;7 8 Const intt=4e4+9;9 Ten intH[T],L[T],XIE_1[T],XIE_2[T];///Xie_1 represents a straight line with a slope greater than 0 One intsum,n,m; A - voidbegin1 () - { thesum=0; -memset (H,0,sizeof(int)*T); -memset (L,0,sizeof(int)*T); -memset (Xie_1,0,sizeof(int)*T); +memset (xie_2,0,sizeof(int)*T); - } + A voidJudgeintXinty) at { - intW; -h[x]=1; l[y]=1; -xie_1[x+y+1]=1; -w=m+1-Y;///mark the board with a negative slope, the column label is inverted, from m-->1, so the line is also L (x+w+1) . -xie_2[x+w+1]=1; in } - to voidJudge_two (intXinty) + { - intw=m+1-y; the if(H[x] | | l[y] | | xie_1[x+y+1] || xie_2[x+w+1] ) * return ; $sum++;Panax Notoginseng - } the + A the + intMain () - { $ intk,a,b; $ while(SCANF ("%d%d%d", &n,&m,&k), (n+m+k)) - { - begin1 (); the for(intI=1; i<=k;i++) - {Wuyiscanf"%d%d",&a,&b); the judge (A, b); - } Wu for(intI=1; i<=n;i++) - for(intj=1; j<=m;j++) About Judge_two (i, j); $printf"%d\n", sum); - } - return 0; -}
View Code
Bnuoj 4299 God Save the i-th Queen (idea title)