zoukankan      html  css  js  c++  java
  • 八皇后 c++

    #include<iostream>
    #include<vector>
    #define jdz(a) a > 0?a:-a;
    using namespace std;
    bool check(int n,vector<int> &path)
    {
        int s = path.pop_back;
        int tmp;
        int dx = 1;
        while(!path.empty)
        {
            tmp = path.pop_back;
            if(tmp == s)
                return false;
            else if(s-tmp == dx || tmp-s == dx)
                return false;
        }
        return true;
    }
    void dfs(int n,bool &used,vector<int> &path,vector<vector<int>> &res)      //num代表第几个皇后,
    {
        if(n > 8)
        {
            res.push_back(path);
            return;
        }
        for(int i = 0;i <= used.size; i++)
        {
            if(used[i] == false && check(n,path)){
                path.push_back(i);
                used[i] = true;
    
                dfs(n+1,used,path,res);
                path.pop_back();
                used[i] = false;
            }
        }
    }
    int main(void)
    {
        vector<int> path;
        bool used[8];
        vector<vector<int>> res;
        for(int i = 0;i < 8;i ++)
        {
            used[i] = true;
            path.push_back(i);
            dfs(1,used,path,res);
    
        }
        return 0;
    }
  • 相关阅读:
    Spark安装
    JavaScript encodeURIComponent()
    Kafka分布式:ZooKeeper扩展
    Kafka特性
    Kafka消息topic分区
    Kafka消息文件存储
    哈希表
    sizeof
    pytest_demo_实战2_fixture应用
    pytest_demo_实战1
  • 原文地址:https://www.cnblogs.com/zhangjialu2015/p/5277081.html
Copyright © 2011-2022 走看看