POJ-1321 checkerboard Problem N Queen problem (binary plate)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.