C# 解决八皇后问题
【摘要】
八皇后问题:
class Bahuanghou{static void Main(string[] args) { Console.ReadLine(); ChessSort(0, newint[8]); Console.ReadLine();} public static bool Compare(int lastChess, intdiffe...
八皇后问题:
-
class Bahuanghou{
-
static void Main(string[] args) {
-
Console.ReadLine();
-
ChessSort(0, newint[8]);
-
-
Console.ReadLine();
-
}
-
public static bool Compare(int lastChess, intdifference, int nowChess)
-
{
-
if(lastChess == nowChess || lastChess - nowChess == difference ||nowChess - lastChess == difference)
-
{
-
return false;
-
}
-
else
-
{
-
return true;
-
}
-
}
-
-
public static void Write(int[] Chesses)//输出结果
-
{
-
-
for (int i= 0; i < 8; i++)
-
{
-
Console.Write(Chesses[i]);//输出结果是皇后所在的位置
-
}
-
Console.Write("\n");
-
}
-
-
public static bool Conflict(int row, int Chess,int[] Chesses)
-
{
-
if (row ==0)
-
{
-
return false;
-
}
-
else
-
{
-
for (int n = 0; n < row;n++)
-
{
-
if (!Compare(Chesses[n], row - n, Chess))
-
{
-
returntrue;
-
}
-
}
-
return false;
-
}
-
}
-
-
-
public static void ChessSort(int num, int[]Chesses)
-
{
-
for (int j= 1; j < 9; j++)
-
{
-
if (num == 8)
-
{
-
Write(Chesses);
-
break;
-
-
}
-
-
if (!Conflict(num, j,Chesses))
-
{
-
Chesses[num] = j;
-
ChessSort(num + 1, Chesses);
-
}
-
}
-
-
}
文章来源: czhenya.blog.csdn.net,作者:陈言必行,版权归原作者所有,如需转载,请联系作者。
原文链接:czhenya.blog.csdn.net/article/details/76092019
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)