Write algorithms step by step (Queen of eight)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

 

 

 

Queen Eight is a very typical topic. Its basic requirements are as follows: eight objects are placed on an 8*8 matrix. Only one object can be placed on a matrix point, and any two points cannot be placed on a row, it cannot be on a column, or on a left oblique line, or on a right diagonal line.

 

When we first saw this question, our first impression was traversal. However, after practice, we found that traversal is not easy to write and complex. Not only does it need to traverse 8*8*8*8*8*8*8*8 = 2 ^ 24 times of data, but it also needs to judge various conditions, the actual computing complexity is even higher. In fact, let's take a closer look. A lot of calculations in the middle are actually not needed, because if we don't have data that can be inserted in a row, then, you don't need to consider the following lines. That is to say, we can perform the next object insertion only when the objects inserted previously are valid and valid. Meaningless traversal will only be useless.

 

So what should we do? In fact, the steps are not difficult:

 

(1) Search for the Insertion Location in row n, which involves determining the validity of the location.

 

(2) If there is no position that can be inserted, return

 

(3) Insert data if any data can be inserted. In this case, determine whether it is the last row. If it is the last row, print the output and return the result. Otherwise, test the next row of data.

 

With the above steps, we can write the code. Old Rules, friends can try it first.

 

A) define global stack and print Functions

 

 

Static int gEightQueen [8] = {0 };

Static int gCount = 0;

 

Void print ()

{

Int outer;

Int inner;

 

For (outer = 0; outer <8; outer ++ ){

For (inner = 0; inner <gEightQueen [outer]; inner ++)

Printf ("*");

 

Printf ("#");

 

For (inner = gEightQueen [outer] + 1; inner <8; inner ++)

Printf ("*");

 

Printf ("\ n ");

}

 

Printf ("========================================== \ n ");

}

Static int gEightQueen [8] = {0 };

Static int gCount = 0;

 

Void print ()

{

Int outer;

Int inner;

 

For (outer = 0; outer <8; outer ++ ){

For (inner = 0; inner <gEightQueen [outer]; inner ++)

Printf ("*");

 

Printf ("#");

 

For (inner = gEightQueen [outer] + 1; inner <8; inner ++)

Printf ("*");

 

Printf ("\ n ");

}

 

Printf ("========================================== \ n ");

}

B) function judgment for adding location legitimacy

 

 

Int check_pos_valid (int loop, int value)

{

Int index;

Int data;

 

For (index = 0; index <loop; index ++ ){

Data = gEightQueen [index];

 

If (value = data)

Return 0;

 

If (index + data) = (loop + value ))

Return 0;

 

If (index-data) = (loop-value ))

Return 0;

}

 

Return 1;

}

Int check_pos_valid (int loop, int value)

{

Int index;

Int data;

 

For (index = 0; index <loop; index ++ ){

Data = gEightQueen [index];

 

If (value = data)

Return 0;

 

If (index + data) = (loop + value ))

Return 0;

 

If (index-data) = (loop-value ))

Return 0;

}

 

Return 1;

} C) Queen eight Traversal

 

 

Void eight_queen (int index)

{

Int loop;

 

For (loop = 0; loop <8; loop ++ ){

If (check_pos_valid (index, loop )){

GEightQueen [index] = loop;

 

If (7 = index ){

GCount ++, print ();

GEightQueen [index] = 0;

Return;

}

Eight_queen (index + 1 );

GEightQueen [index] = 0;

}

}

}

Void eight_queen (int index)

{

Int loop;

 

For (loop = 0; loop <8; loop ++ ){

If (check_pos_valid (index, loop )){

GEightQueen [index] = loop;

 

If (7 = index ){

GCount ++, print ();

GEightQueen [index] = 0;

Return;

}

Eight_queen (index + 1 );

GEightQueen [index] = 0;

}

}

}

Summary:

 

(1) iterative recursion is a difficult part of programming. You need to practice it yourself. If you want to write it one hundred times, you can write it again.

 

(2) When recursion, pay attention to the exit of function return.

 

(3) do not change the order of statements in recursive functions.

 

(4) Save and restore Data in recursive functions

 

(5) recursive functions also need to be verified. They can be verified by program verification or other functions.

Related Article

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.