您的位置:首页 > 理论基础 > 数据结构算法

[数据结构]八皇后(暴力,解答树,DFS回溯)

2015-06-21 10:05 393 查看
/*
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
;					//写着玩

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");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: