zoukankan      html  css  js  c++  java
  • codevs 1295 N皇后问题

     时间限制: 2 s
     空间限制: 128000 KB
     题目等级 : 黄金 Gold
     
     
     
    题目描述 Description

    在n×n格的棋盘上放置彼此不受攻击的n个皇后。按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子。n后问题等价于再n×n的棋盘上放置n个皇后,任何2个皇后不妨在同一行或同一列或同一斜线上。

    输入描述 Input Description

     给定棋盘的大小n (n ≤ 13)

    输出描述 Output Description

     输出整数表示有多少种放置方法。

    样例输入 Sample Input

    8

    样例输出 Sample Output

    92

    数据范围及提示 Data Size & Hint

    n<=13

    (时限提高了,不用打表了)

    #include <cstdio>
    #include <iostream>
    
    using namespace std;
    
    int ans,n;
    
    bool judge[4][2312333];
    void dfs(int now)
    {
        if(now==n+1)
        {
            ans++;
            return ;
        }
        for(int i=1;i<=n;i++)
        {
            if(!judge[1][i]&&!judge[2][i+now]&&!judge[3][n+1-now+i])
            {
                judge[1][i]=true,judge[2][i+now]=true,judge[3][n+1-now+i]=true;
                dfs(now+1);
                judge[1][i]=false,judge[2][i+now]=false,judge[3][n+1-now+i]=false;
            }
        }
        return ;
    }
    
    int main()
    {
        cin>>n;
        dfs(1);
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    sublime开启vim模式
    git命令行界面
    搬进Github
    【POJ 2886】Who Gets the Most Candies?
    【UVA 1451】Average
    【CodeForces 625A】Guest From the Past
    【ZOJ 3480】Duck Typing
    【POJ 3320】Jessica's Reading Problemc(尺取法)
    【HDU 1445】Ride to School
    【HDU 5578】Friendship of Frog
  • 原文地址:https://www.cnblogs.com/chen74123/p/6680871.html
Copyright © 2011-2022 走看看