N皇后递归代码
#include <iostream>#include <cstring>
#define N 15
using namespace std;
int table = {0};
int count = 0;
//x行, y列
bool check(int x, int y)
{
//行不用检察
for(int i=0; i<x; i++)
{
if(table == y)
return false;
if(table+i == x+y)
return false;
if(table-i == y-x)
return false;
}
return true;
}
void dfs(int col, int n)
{
if(col == n)
{
count++;
return;
}
for(int i = 0; i<n; i++)
{
if(check(col, i))
{
table = i;
dfs(col+1, n);
//table = 0;
}
}
}
int main(){
int n;
cin >> n;
dfs(0, n);
cout << count << endl;
return 0;
}
页:
[1]