Title: Give an n * N chess board, there are some places on this board can not play chess pieces.
Now ask you to put a K on this board, so that any of these pieces of two not on the same row, ask how many ways
Problem-solving ideas: Similar to the N-queen, can be a line of processing, with the binary 1 means that the position can be put, 0 means no, all indicates the status of each row can be put ((1 << N)-1)
Assuming the state of the column is S, then S & (the line's limit) & All is the end state of the row.
Now just find all 1 of this final state, then how to find this fast 1
Assuming that the final state is SS, then SS & (-SS) & All is the first 1 position (self-validating), then set a while loop to find all 1, remember &all
#include <cstdio>#include <algorithm>using namespace STD;Const intN = -;CharStr[n];intN, K, ans, all, statu[n];voidInit () {all = (1<< N)-1; for(inti =0; I < n; i++) {gets (str); Statu[i] = all; for(intj =0; J < N; J + +)if(Str[j] = ='. ') Statu[i] ^= (1<< j); } ans =0;}voidSolveintCurintNumints) {if(num = = k) {ans++;return; }if(cur = = N | | n-cur + num < k)return;intSS = (S & statu[cur]) & all; while(ss) {intt = SS & (-SS) & all; Solve (cur +1, Num +1, s ^ t); SS = (ss ^ t) & all; } Solve (cur +1, num, s);}intMain () { while(Gets (str)) {sscanf(STR,"%d%d", &n, &k);if(n = =-1&& k = =-1) Break; Init (); Solve0,0, all);printf("%d\n", ans); }return 0;}
POJ-1321 checkerboard Problem N Queen problem (binary plate)