八皇后 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;
}

 

posted on 2016-03-14 20:46  Tob's_the_top  阅读(131)  评论(0编辑  收藏  举报

导航