/*
Name:八皇后(DFS回溯)
Actor:HT
Time:2015年6月21日
Error Reporte:
}
*/
#include"stdio.h"
#include"iostream"
#include"string.h"
using namespace std;
#define N 8
int sum;
int _amap[2 * N + 1] = { 0 };
int _bmap[2 * N + 1] = { 0 };
//int _xmap[N + 1] = { 0 };
int _ymap[N + 1] = { 0 };
int bigmap[N]; //写着玩
void set(int x, int y, int i)
{
_ymap[y] = i;
_amap[y - x + N] = i;
_bmap[y + x - 1] = i;
bigmap[x] = y; //写着玩
}
void print() //写着玩
{
int i, j;
for (i = 1; i <= N; i++)
{
for (j = 1; j <= N; j++)
{
if (bigmap[i] == j) cout << " * ";
else cout << " . ";
}
cout << endl;
}
}
void dfs(int x)
{
if (x == N+1)
{
sum++;
print(); //写着玩
cout << endl; //写着玩
}
int i;
for (i = 1; i <= N; i++)
{
if (_amap[i - x + N] == 1 || _bmap[i + x - 1] == 1 || _ymap[i] == 1) continue;
set(x, i, 1);
dfs(x + 1);
set(x, i, 0);
}
}
int main()
{
sum = 0;
dfs(1);
cout << sum << endl;
system("pause");
}
[数据结构]八皇后(暴力,解答树,DFS回溯)
原文作者:八皇后问题
原文地址: https://blog.csdn.net/z354681250/article/details/46580609
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/z354681250/article/details/46580609
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。