CodeVs 1295 N皇后问题

题目大意:

http://codevs.cn/problem/1295/

 

代码:

#include <iostream>
#include <cmath>

using namespace std;

int n;
int arr[15] = {0};
int count = 0;

bool canPos(int x)
{
    for(int i = 1; i < x; i++)
    {
        if(arr[i] == arr[x] || abs(arr[i]-arr[x]) == abs(x-i))
            return false;
    }
    return true;
}

void dfs(int x)
{
    if(x == n+1)
    {
        count++;
    }
    for(int i = 1; i <= n; i++)
    {
        arr[x] = i;
        if(canPos(x))
            dfs(x+1);


    }
}


int main()
{
    cin >> n;

    dfs(1);

    cout << count << endl;
    return 0;
}

 

posted @ 2017-12-21 13:58  prog123  阅读(140)  评论(0编辑  收藏  举报