N皇后问题:回溯算法与剪枝优化
#include <bits/stdc++.h>
using namespace std;
int a, ans = 0;
#define N 15
int fig[N];
char dp[N][N];
int posx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int posy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool INIT(int n)
{
memset(dp, ' ', sizeof(dp));
for (int i = 1; i <= n; i++)
{
dp[i][fig[i]] = 'Q';
}
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < 8; j++)
{
int x = i, y = fig[i];
while (x >= 1 && y >= 1 && x <= a && y <= a)
{
x += posx[j];
y += posy[j];
if (dp[x][y] == 'Q')
{
return false;
}
dp[x][y] = '1';
}
}
}
return true;
}
void pos(int x, vector<vector
原文地址: https://www.cveoy.top/t/topic/jB2b 著作权归作者所有。请勿转载和采集!