IT company interview questions collected-C-related-eight queens explanation

Source: Internet
Author: User

Some people may not be able to read the previous post. I made a special arrangement and tried to make it easy to understand. The comments are very detailed and are basically annotated.

First, paste a picture. It is easier to see the picture.

Paste detailed code

# Include <stdio. h> # define row 8 // represents the column, the coordinate is X # define Col 8 // represents the row, and the coordinate is y # define num 7 // problem, array subscript 0-7 # define true 0 // true # define false 1 // false # define Yes 1 // queen # define no 0 // no queen # define no_back 0/ /indicates that backtracking is not required # define back 1 // indicates that backtracking is required for int A [row] [col]; // The default value is 0, indicating that the coordinate has no queen. If it is changed to 1, it indicates that the coordinate has a queen int K = 0; // indicates the condition in K. // n indicates the subscript of the column to be traversed, that is, the value of X. Rowint queens (int n) {int I, j; // output eight queens board if (n> num) {printf ("---------- % 06d ---------- \ n ", ++ K); for (I = 0; I <row; I ++) {for (j = 0; j <Col; j ++) printf ("% s", a [I] [J] = 1? "○": "●"); // 0 indicates that it is not a queen. 1 indicates that it is a queen printf ("\ n ");} printf ("-------------------------- \ n"); return back; // indicates normal return and backtracking} // If the traversal is not complete, for (j = 0; j <Col; j ++) // traverse the Y value 0-> Col {int flag = true; // This loop traverses the X value, that is, the Y value remains unchanged, X value variation for (I = 0; I <n; I ++) // Based on the passed column subscript x value, row value, traverse {// if there is a queen in the left corner, Northeast southwest direction, right corner, and northwest southeast direction, this cycle will be launched, perform the next loop if (a [I] [J] = Yes | (I + J-N)> = 0 & A [I] [I + J-N] = Yes) | (J + n-I <col) & A [I] [J + n-I] = Yes) {flag = false; break ;}} if (flag = true) // if the previous cycle does not show this situation {// place queen a [n] [J] = Yes here; if (Queens (n + 1) = back) {// if the lower-layer traversal requires backtracking a [n] [J] = no; // do not place the queen here} else {// if the lower-layer traversal does not require backtracking, return no_back ;}}// when traversing the Y value, there is no suitable one, and flag = false; it indicates that the lower-level cannot place the queen, return back to return back;} int main (void) {queens (0); Return 0 ;}

There is a single-bit operation solution, which is also very good. If you are interested, please take a look. Http://blog.csdn.net/wang6279026/article/details/8661904

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.