/*************************************************************************
> 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.
该问题是经典的8皇后问题
************************************************************************/
> 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皇后问题
************************************************************************/