(二) 源代码:
#include <iostream>
#define QUEENS 8
int Site[QUEENS];
int num =0;
int Isright(int n) //判断第 n 个皇后放上去之后,是否无冲突
{
int i;
for(i = 0 ; i < n ; i++)
{
if(Site[i] == Site[n])
return 0;
if(abs(Site[i] - Site[n]) == (n - i))
return 0;
}
return 1;
}
void Queen(int n) //递归放置第 n 个皇后
{
int i;
if(n == QUEENS)
{
int i,k,j;
num=num+1;
printf("第 %-3d 个结果: ",num);
for(i = 0 ; i < QUEENS ; i++)
{
if(i==0)
{
for(j=0;j<QUEENS;j++)
{
if(Site[j]!=1) continue;
for(k=0;k<8;k++)
{
if(k==j) printf( "%d " , Site[j]);
else printf( "# ");
}continue;
}
printf("");continue;
}
for(j=0;j<QUEENS;j++)
{
if(Site[j]!=i+1) continue;
for(k=0;k<8;k++)
{
if(k==j) printf( " %d " , Site[j]);
else printf( " # ");
}continue;
}printf("\n");
}
printf( "\n-----------------------------------\n\n ");
return;
}
for(i = 1 ; i <= QUEENS ; i++)
{
Site[n] = i;
if(Isright(n))
Queen(n + 1);
}
}
void main() //主函数
{
int k=0;
printf(" 八皇后的计算结果:\n\n");
Queen(k);
}
(三)运行结果:
八皇后的计算结果:
第 1 个结果:
1 # # # # # # #
# # # # # # 2 #
# # # # 3 # # #
# # # # # # # 4
# 5 # # # # # #
# # # 6 # # # #
# # # # # 7 # #
# # 8 # # # # #
-----------------------------------
第 2 个结果:
1 # # # # # # #
# # # # # # 2 #
# # # 3 # # # #
# # # # # 4 # #
# # # # # # # 5
# 6 # # # # # #
# # # # 7 # # #
# # 8 # # # # #
-----------------------------------
第 3 个结果:
1 # # # # # # #
# # # # # 2 # #
# # # # # # # 3
# # 4 # # # # #
# # # # # # 5 #
# # # 6 # # # #
# 7 # # # # # #
# # # # 8 # # #
-----------------------------------
第 4 个结果:
1 # # # # # # #
# # # # 2 # # #
# # # # # # # 3
# # # # # 4 # #
# # 5 # # # # #
# # # # # # 6 #
# 7 # # # # # #
# # # 8 # # # #
-----------------------------------
第 5 个结果:
# # # # # 1 # #
2 # # # # # # #
# # # # 3 # # #
# 4 # # # # # #
# # # # # # # 5
# # 6 # # # # #
# # # # # # 7 #
# # # 8 # # # #
-----------------------------------
第 6 个结果:
# # # 1 # # # #
2 # # # # # # #
# # # # 3 # # #
# # # # # # # 4
# 5 # # # # # #
# # # # # # 6 #
# # 7 # # # # #
# # # # # 8 # #
-----------------------------------
第 7 个结果:
# # # # 1 # # #
2 # # # # # # #
# # # # # # # 3
# # # 4 # # # #
# 5 # # # # # #
# # # # # # 6 #
# # 7 # # # # #
# # # # # 8 # #
-----------------------------------
第 8 个结果:
# # 1 # # # # #
2 # # # # # # #
# # # # # # 3 #
# # # # 4 # # #
# # # # # # # 5
# 6 # # # # # #
# # # 7 # # # #
# # # # # 8 # #
-----------------------------------
第 9 个结果:
# # # # 1 # # #
2 # # # # # # #
# # # 3 # # # #
# # # # # 4 # #
# # # # # # # 5
# 6 # # # # # #
# # # # # # 7 #
# # 8 # # # # #
-----------------------------------
第 10 个结果:
# # # # # # 1 #
2 # # # # # # #
# # 3 # # # # #
# # # # # # # 4
# # # # # 5 # #
# # # 6 # # # #
# 7 # # # # # #
# # # # 8 # # #
-----------------------------------
第 11 个结果:
# # # # 1 # # #
2 # # # # # # #
# # # # # # # 3
# # # # # 4 # #
# # 5 # # # # #
# # # # # # 6 #
# 7 # # # # # #
# # # 8 # # # #
-----------------------------------
第 12 个结果:
# # # 1 # # # #
2 # # # # # # #
# # # # 3 # # #
# # # # # # # 4
# # # # # 5 # #
# # 6 # # # # #
# # # # # # 7 #
# 8 # # # # # #
-----------------------------------
第 13 个结果:
# 1 # # # # # #
# # # # # 2 # #
3 # # # # # # #
# # # # # # 4 #
# # # 5 # # # #
# # # # # # # 6
# # 7 # # # # #
# # # # 8 # # #
-----------------------------------
第 14 个结果:
评论0