Jinan Study Day 5 T3 AM

Source: Internet
Author: User

"Title description"

Rly can't play chess now, as many people don't know. However, as a oier,rly of course did eight Queen questions. Here again a few more words, the Queen can attack the same row with the same diagonal line, in

N*n's chessboard, placing n queens so that they can not attack each other, to find the number of different solutions, this is the classic n queen problem. Now the problem of promoting the N queen question, this question for you

It's a piece of cake. But because the last time rly to break the chessboard, and do not have a new, so rly plan a little bit, the problem is in the broken board of the N queen problem. He wants to know ... (You know that.) )

Wives are the same.

"Input description"

A row, an integer n.

The next n rows, n integers per line, or 0, mean no bad, or 1, means bad.

"Output description"

A row that outputs a different number of connectors.

"Sample Input"

4

1 0 1 1

1 1 1 0

0 1 1 1

1 1 0 1

"Sample Output"

1

"Data Range"

For 40% of data, n<=13.

For 100% of data, n<=16.

For 30% of the data the chessboard is not broken, you can think Rly again to buy a new one.

1 /*bit operation optimization n Queens*/2#include <cstdio>3#include <iostream>4 #defineN 205 using namespacestd;6 intHang[n],n,ans;7 voidDfsintTintAintBintC//a,b,c are binary numbers, indicating which column or diagonal is missing the Queen, 1 for the8 {9     if(t==n+1)Ten     { Oneans++;return; A     } -     intS= ((1<<n)-1) & (hang[t]|a|b|c)); -     /* the S is a binary number that represents the current column in which to put the Queen - (HANG[T]|A|B|C) indicates which columns are left out of the column and diagonal angles - Plus ~ (inverse), which columns can be placed Queen -     */  +      while(S) -     { +         intx=s& (-s);//x indicates where the queen can be placed now. ADFS (t+1, A+x, (b+x) <<1, (c+x) >>1); ats-=x; -     } - } - intMain () - { -scanf"%d",&n); in      for(intI=1; i<=n;i++) -        for(intj=1; j<=n;j++) to       { +           intX;SCANF ("%d",&x); -hang[i]+=x<< (J-1); the       } *Dfs1,0,0,0); $printf"%d", ans);Panax Notoginseng     return 0; -}

Idea: The N queen problem after the optimization of bit arithmetic

Jinan Study Day 5 T3 AM

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.