C # version of Queen's question (non-recursive)

Source: Internet
Author: User
recursion | problem

/*
*author:junyi Sun @CCNU
* e-mail:fxsjy@yahoo.com.cn
*/

Using System;
Namespace Sunjoy
{
public class Queen
{
public static int Main ()
{
int board_size = 0,x=0,y=0;//Checkerboard size, current line, when front
UINT Solution_count = 0; Number of Queens placement scheme
int[] Rows, cols, slot1, Slot2, x2y;//row occupancy, column occupancy, "/" oblique slash occupancy, "\" diagonal position, queen coordinates
DateTime T_start, T_end;

Console.WriteLine ("Please enter the size of the chessboard");
Try
{
Board_size = Convert.ToInt32 (Console.ReadLine ());
if (board_size <= 0)
{
Console.WriteLine ("Illegal data");
return-1;
}
}
catch (Exception e) {Console.WriteLine ("Occurrence exception" + e.message);

rows = new Int[board_size];
cols = new Int[board_size];
slot1 = new int[board_size * 2-1];
Slot2 = new int[board_size * 2-1];
x2y = new Int[board_size];

for (int i = 0; i < board_size; i++)
X2y[i] =-1; Coordinates are initialized to 1.
T_start = DateTime.Now;
while (true)
{
for (y = x2y[x]+1 y < board_size; y++)
if (rows[x] = = 0 && cols[y] = = 0 && slot1[x + y] = = 0 && slot2[x-y + board_size-1] = = 0)
Break

if (Y < board_size)
{
The pieces in line x fall
ROWS[X] = 1; Cols[y] = 1; Slot1[x + y] = 1; Slot2[x-y + board_size-1] = 1; X2y[x] = y;
}
Else
{
Backtrack, pick up the pieces
if (x > 0)
{
X2Y[X] =-1;
x--;
Rows[x] = 0; COLS[X2Y[X]] = 0; Slot1[x + x2y[x]] = 0; SLOT2[X-X2Y[X] + board_size-1] = 0;
Continue
}
else break;
}
if (x = = board_size-1)//If a set of solutions has been obtained, that is, when the last line of pieces is settled
{
for (int i = 0; i < board_size; i++)
{
for (int j = 0; J < Board_size; J + +)
{
if (x2y[i] = = j) Console.Write ("Q");
else Console.Write ("");
}
Console.Write ("\ n");

}
Console.Write ("\ n");
solution_count++; Total programme number plus one
Rows[x] = 0; COLS[X2Y[X]] = 0; Slot1[x + x2y[x]] = 0; SLOT2[X-X2Y[X] + board_size-1] = 0;//Discard this column

}
Else
{
x + +; Continue with the next line
}
}
T_end = DateTime.Now;
Console.WriteLine ("Total {0} Group solution", Solution_count);
Console.WriteLine ("Calculate and print shared time {0} seconds", T_end-t_start);
return 0;
}
}
}

___________________________________________________________

Test results:
Please enter the size of the chessboard
4
Q
Q
Q
Q

Q
Q
Q
Q

Total 2 groups of solutions
Calculate and print shared time 00:00:00 seconds

_______________________________________________
Please enter the size of the chessboard
6
Q
Q
Q
Q
Q
Q

Q
Q
Q
Q
Q
Q

Q
Q
Q
Q
Q
Q

Q
Q
Q
Q
Q
Q

Total 4 groups of solutions
Calculate and print shared time 00:00:00.0156250 seconds




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.