C # answers to the eight Queens question

Source: Internet
Author: User
Answer | Questions from V-Star [Windows Prince] responder, as follows:<br>
<br>
Using system;<br>
Class queen{<br>
const int SIZE = number of 8;//Queens <br>
public static void Main () <br>
{<br>
int[] Queen = new int [size];//per line Queen's position <br>
int y,x,i,j,d,t=0;<br>
y = 0;<br>
Queen[0] = -1;<br>
while (true) <br>
{<br>
for (x=queen[y]+1 x<size; x + +) <br>
{<br>
for (i=0;i<y;i++) <br>
{<br>
j = queen[i];<br>
D = y-i;<br>
Check to see if the new queen can attack each other with the previous Queen <br>
if ((j==x) | | (j==x-d) | | (j==x+d)) <br>
Break;<br>
}<br>
if (i>=y) <br>
break;//does not attack <br>
}<br>
if (x = SIZE)//no suitable location <br>
{<br>
if (0==y) <br>
{<br>
Back to the first line of <br>
Console.WriteLine ("Done");<br>
Break End <br>
}<br>
Back to the moon <br>
Queen[y]=-1;<br>
Y--;<br>
}<br>
Else<br>
{<br>
queen[y]=x;//determine the location of the Queen <br>
y++;//Next Queen <br>
if (y<size) <br>
Queen[y]=-1;<br>
Else<br>
{<br>
All queens are finished, output <br>
Console.WriteLine ("\ n" + ++t + ': ');<br>
for (i=0;i<size;i++) <br>
{<br>
for (j=0;j<size;j++) <br>
if (queen[i] = = j) <br>
Console.Write (' Q ');<br>
Else<br>
Console.Write ('. '); <br>
Console.WriteLine ();<br>
}<br>
y = size-1;//back <br>
}<br>
}<br>
}<br>
}<br>
}<br>
Eight Queens have 92 solutions. <br>
Another: I think the foreach and for loops are different in meaning, where you should use for.<br>
I hope you'll see the same. <br>
Heavy particle @y2k0928<br>
<br>
--------------------------------------------------------------------------------<br>
<br>
Delivered by: The Prince of Windows lists all unanswered star issues in September, 09:16: <br>
Topic: Thank you Brother heavy particle reply this post <br>
Read the relevant posts <br>
<br>
--------------------------------------------------------------------------------<br>
<br>
Answer: To the Prince of Windows: The Eight Queens Problem C # Edition by heavy particles in September, 22:32:<br>
<br>
Thank you for your comments and optimization, but compared the results, as if our results are consistent. :-) <br>
In addition, about that part of the printing, if this optimizes:<br>
Console.Write (queen[i]==j?) ' Q ': '. '); <br>
Seems to be a little more concise, what does the particle brother think? <br>
With regard to foreach and for, it is true that for both the literal and the document it seems to be more deterministic than foreach. However, foreach is also a traversal, the key is traversal of the repeatability and the law, that is, the results of each traversal is predictable and the same. In addition, we can customize the enumerator to determine the traversal behavior of foreach, so that "cannot traverse" or "unpredictable" into "ergodic" and "predictable", as in C + + overload [] operator makes the list "into" an array. <br>
<br>
For some basic data structure traversal, such as array, foreach or for the same, but the particle brother to remind you, do not take for granted anything, it will not lead you to go astray will not bring you any benefits. <br>
<br>
I am currently dealing with Ms C # Developer, in fact, there are some parts of the product code in MS C # (I do not know whether the formal or experimental). I would like to play with the particle brother and the big and small shrimp brothers and sisters together to play this new thing. <br>


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.