Initial solution to the eight queens Problem

Source: Internet
Author: User

This afternoon, I spoke about the eight queens issue during ACM training. It took me about two hours to write a runable version. The method was stupid, but I could find all the solutions. Optimization later.

# Include <iostream. h>
// Chessboard
Int arr [8] [8] = {0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0 }};
Bool check (int r, int H)
{
If (h> 0)
{
For (int hh = h-1; HH> = 0; hh --) // check each column before
{
If (ARR [r] [HH] = 1)
Return false;
}
}
If (h> 0) & (r> 0 ))
{
For (INT rrr1 = R-1, int hhh1 = h-1; rrr1> = 0, hhh1> = 0; rrr1 --, hhh1 --) // upper left slash
{
If (ARR [rrr1] [hhh1] = 1)
Return false;
}
}
If (h> 0) & (r <7 ))
{
For (INT rrr2 = R + 1, int hhh2 = h-1; rrr2 <= 7, hhh2> = 0; rrr2 ++, hhh2 --) // left lower slash
{
If (ARR [rrr2] [hhh2] = 1)
Return false;
}
}
Return true;
}
Void display ()
{
// Display the chessboard
For (INT I = 0; I <8; I ++)
{
For (Int J = 0; j <8; j ++)
{
Cout <arr [I] [J] <"";
}
Cout <Endl;
}
}
Int main ()
{
For (INT R0 = 0; R0 <8; R0 ++)
{
Arr [R0] [0] = 1;
For (INT R1 = 0; R1 <8; R1 ++)
{
If (check (R1, 1 ))
{
Arr [R1] [1] = 1;
For (INT r2 = 0; r2 <8; r2 ++)
{
If (check (R2, 2 ))
{
Arr [R2] [2] = 1;
For (INT R3 = 0; R3 <8; R3 ++)
{
If (check (R3, 3 ))
{
Arr [R3] [3] = 1;
For (INT r4 = 0; R4 <8; R4 ++)
{
If (check (R4, 4 ))
{
Arr [R4] [4] = 1;
For (INT R5 = 0; R5 <8; R5 ++)
{
If (check (R5, 5 ))
{
Arr [R5] [5] = 1;
For (INT R6 = 0; R6 <8; R6 ++)
{
If (check (R6, 6 ))
{
Arr [R6] [6] = 1;
For (INT R7 = 0; R7 <8; R7 ++)
{
If (check (R7, 7 ))
{
Arr [R7] [7] = 1;
Display ();
Cout <Endl;
}
Arr [R7] [7] = 0;
}
Arr [R6] [6] = 0;
}
}
Arr [R5] [5] = 0;
}
}
Arr [R4] [4] = 0;
}
}
Arr [R3] [3] = 0;
}
}
Arr [R2] [2] = 0;
}
}
Arr [R1] [1] = 0;
}
}
Arr [R0] [0] = 0;
}
Return 0;
}

The loop is a little more useful and I can't recognize it myself.

Output:

0 1 0 0 0 0 0

0 1 0 0 0 0 0
0 0 0 0 0 1 0
0 0 0 0 1 0 0 0
0 0 0 0 0 0 1
1 0 0 0 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 1 0 0
0 0 1 0 0 0 0

0 0 0 0 0 1 0
0 1 0 0 0 0 0
0 0 0 0 1 0 0
0 0 1 0 0 0 0
1 0 0 0 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 0 0 1
0 0 0 0 1 0 0 0

0 0 0 0 0 0 1
0 1 0 0 0 0 0
0 0 0 0 1 0 0 0
0 0 1 0 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 0 1 0
0 0 0 1 0 0 0
0 0 0 0 1 0 0

0 0 0 1 0 0 0
0 1 0 0 0 0 0
0 0 0 0 0 0 1
0 0 0 0 1 0 0
1 0 0 0 0 0 0 0
0 0 1 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 1 0

0 0 1 0 0 0 0
0 0 0 0 1 0 0
0 1 0 0 0 0 0
0 0 0 0 0 1 0
1 0 0 0 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 0 0 1
0 0 0 0 1 0 0 0

0 0 1 0 0 0 0
0 0 0 0 1 0 0 0
0 1 0 0 0 0 0
0 0 0 0 0 0 1
1 0 0 0 0 0 0 0
0 0 0 0 0 1 0
0 0 0 1 0 0 0
0 0 0 0 1 0 0

0 0 0 0 1 0 0
0 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0 0 0 1 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 1 0 0

Too many results, some of which failed to be output. This is a very stupid method and needs to be optimized in the future.

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.