Recursion-eight queens/n queens

Source: Internet
Author: User

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
 


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.