码迷,mamicode.com
首页 > 编程语言 > 详细

八皇后 c++

时间:2016-03-14 21:26:48      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

#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;
}

 

八皇后 c++

标签:

原文地址:http://www.cnblogs.com/zhangjialu2015/p/5277081.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!