The eight queens issue is an old and famous issue.Algorithm. This problem was raised by the famous mathematician Gauss in the 19th century in 1850: Eight queens were placed on 8x8 gags of chess, so that they could not attack each other, that is to say, neither of the two queens can be in the same row, column, or diagonal line.
Using System;
Using System. Collections. Generic;
Using System. diagnostics;
Public Class Myclass
{
Static Int [] Site = New Int [ 8 ];
Static Int Queennum = 8 ;
Static Int Count = 0 ; // Solution count
Public Static Void Main ()
{
//WL ();
Queen (0);
RL ();
}
Public Static Void Queen ( Int N)
{
If (Queennum = N)
{
WL ( " Solution No. {0} " , ++ Count );
For ( Int I = 0 ; I < Queennum; I ++ )
{
WL ("{0 },", Site [I]);
}
WL ( " ------------------------------------ " );
Return ;
}
Else
{
For ( Int I = 0 ; I < Queennum; I ++ )
{
Site [N]=I;
If(Isok (n ))
Queen (n+1);
}
}
}
Public Static Bool Isok ( Int N)
{
For ( Int I = 0 ; I < N; I ++ )
{
If (Site [I] = Site [N])
Return False ;
If (Math. Abs (site [I] - Site [N]) = N - I)
Return False ;
}
Return True ;
}
Helper Methods # Region Helper Methods
Private Static Void WL ( Object Text, Params Object [] ARGs)
{
Console. writeline (text. tostring (), argS );
}
Private Static Void RL ()
{
Console. Readline ();
}
Private Static Void Break ()
{
System. Diagnostics. Debugger. Break ();
}
# Endregion
}