N-Queue

/*************************************************************************
    > File Name: N-Queue.cpp
    > Author:
    > Mail:
    > Created Time: 2014年08月26日 星期二 15时00分40秒
    >Description:
    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
#include<iostream>
using namespace std;
static int tmp[8] = {0}; 
static int memthod = 0;
#include<stdio.h>
bool conflict(int cur,int i)
{
    int k = 0;
    for(;k<cur; k++)
    {
        if(tmp[cur] == tmp[k] || tmp[cur] - cur  == tmp[k] - k|| tmp[cur] + cur == tmp[k] + k)
        {
            return true;
        }
    }
    return false;
}
void solution(int cur, int n)
{
    if(cur == n) 
    {
        memthod++;
        return ;
    }
    int i,j;
    for(i = 0; i < n; i++)
    {
        bool select_position_ok = true;
        tmp[cur] = i;
        if (!conflict(cur,i))
        {
            solution(cur + 1, n);
        }
    }
}
int main()
{
    solution(0,8);
    printf("%d\n",memthod);
}


    该问题是经典的8皇后问题
 ************************************************************************/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值