sgu224 分类: sgu 2015-06-12 10:42 19人阅读 评论(0) 收藏


喜闻乐见的暴搜。


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>

int n, k, col, dl, dr;

long long dfs(int row, int cnt)
{
    if(cnt == k) return 1;

    if(row > n) return 0;

    long long ret = 0;

    for(int i = 1; i <= n; i++)
        if(!(col&(1<<i)) && !(dl&(1<<(row-i+n))) && !(dr&(1<<(row+i))))
        {
            col ^= 1<<i, dl ^= 1<<(row-i+n), dr ^= 1<<(row+i);

            ret += dfs(row + 1, cnt + 1);

            col ^= 1<<i, dl ^= 1<<(row-i+n), dr ^= 1<<(row+i);
        }   
    if(n - row >= k - cnt) ret += dfs(row + 1, cnt);

    return ret; 
}

int main()
{   
#ifndef ONLINE_JUDGE    
    freopen("sgu224.in","r",stdin);
    freopen("sgu224.out","w",stdout);
#endif

    std::cin >> n >> k;

    if(k > n) std::cout << 0 << std::endl;
    else std::cout << dfs(1, 0) << std::endl;

#ifndef ONLINE_JUDGE    
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-06-12 10:42  <Dash>  阅读(171)  评论(0编辑  收藏  举报