链接:Miku

-------------------------

位运算的好题

(对于位运算本蒟蒻来说太毒瘤了)

-------------------------

对于这题的数据范围,把八皇后的代码改一改是不够的,必须要用位运算

---------------------------

先上代码

#include<cstdio>
#include<iostream>
#include<cstdio>
using namespace std;
int n,ans;
int map[25];
int end;
char c;
int sign; 
int lie;
int lowbit(int x){
    return x&(-x);
}
void dfs (int now,int l,int r,int deep)
 {
     if(now==end){
         ans++;
         return ;
     }
    int sign=end&(~(now|r|l|map[deep]));
    while(sign){
        int p=lowbit(sign);
        sign-=p;
        dfs(now+p,(l+p)<<1,(r+p)>>1,deep+1);
    }
}
int main()
{
    cin>>n;
    end=(1<<n)-1;
    for(int i=1;i<=n;++i){
        for(int j=1;j<=n;++j){
            cin>>c;
            if(c=='.')
                map[i]=map[i]|(1<<(n-j));
        }
    }
    dfs(0,0,0,1);
    cout<<ans;
    return 0;
} 
Ac

相关文章:

  • 2021-07-14
  • 2021-05-08
  • 2021-11-26
  • 2021-05-19
  • 2021-09-06
  • 2021-09-26
  • 2021-12-20
  • 2021-05-20
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-03-04
  • 2021-11-24
  • 2022-02-21
相关资源
相似解决方案