八皇后问题:
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);
}
}
}