sgu224:Little Queens(搜索)

来源:互联网 发布:淘宝网羊绒毛衣 编辑:程序博客网 时间:2024/05/08 15:45

题目大意:
      n皇后。

分析:
      搜索。。。

AC code:

#include <cstdio>#include <iostream>#define ONLINE_JUDGEconst int MAXN = 19;int n, k;int d1[MAXN<<1];int d2[MAXN<<1];int c[MAXN];int dfs(int now, int tk){    if(tk == k) return 1;    if(now == n+1) return 0;    int ret = dfs(now+1, tk);    for(int i = 1; i <= n; ++i)        if(!d1[now+i] && !d2[now-i+n] && !c[i])        {            d1[now+i] = d2[now-i+n] = c[i] = 1;            ret += dfs(now+1, tk+1);            d1[now+i] = d2[now-i+n] = c[i] = 0;        }    return ret;}int main(){    #ifndef ONLINE_JUDGE    freopen("sgu224.in", "r", stdin);    freopen("sgu224.out", "w", stdout);    #endif    std::ios::sync_with_stdio(0);    std::cin >> n >> k;    std::cout << dfs(1, 0) << std::endl;    #ifndef ONLINE_JUDGE    fclose(stdin);    fclose(stdout);    #endif    return 0;}
0 0