[Algorithm Review 2] backtracing

Source: Internet
Author: User

I. Problem Description

Eight queens are placed on 8x8 chess so that they cannot attack each other. That is, neither of them can be in the same row, column, or diagonal line, how many methods are available.

 

Ii. Analysis

Step-by-Step testing is adopted. First, you can move forward from one direction. If you can go in, you can go in. If you cannot go in, you can go back and try another path. First, let's analyze the rules of chess. These rules can restrict our progress, that is, the obstacles on our way forward. A queen Q (x, y) can be eaten by the Queen Q (row, col) that meets the following conditions

1) x = row (there cannot be two queens in the vertical direction)

2) y = COL (there cannot be two queens in the horizontal direction)

3) COL + ROW = Y + X; (oblique positive direction)

4) col-row = Y-X; (diagonal opposite direction)

When we encounter one of the above problems, it indicates that we have encountered obstacles and cannot continue to move forward. We need to return it and try other paths.

We regard the Board as an array of 8x8, so that we can use a dry way to solve this problem, in this case, 8 combinations are extracted from 8x8 = 64 grids, C (4426165368) =. Obviously, this number is very large. We can add backtracing on the basis of doing nothing, starting from column 1, we will perform this step one by one. From Row 1 to row 3, we will find a location that is not under any attack from any existing queen.

 

The placement of the first four columns is shown in the Fifth Column. We will find that the security location of the queen is not found.

In column 5, any row will be attacked by the Queen shown in the figure. At this time, we think we have hit the south wall. It is time to look back. We will move back the column, add the Queen in the fourth column

(3, 4) Remove. Starting from (3, 4), we will find the next safe location in column 4 as (7, 4) and continue to column 5, it is found that the fifth column still does not have a safe location, and goes back to the fourth column. At this time, the fourth column is also a dead end. We will go back to the third column, so that we can move forward a few steps and move back, the final result is that a security location (successful) is found in column 8th or the first column is a dead end, but the security location is still not found in column 8th.

To sum up, the steps to solve the problem of Queen 8 with Backtracking are as follows:

1> Start from the first column, locate the security location for the queen, and then jump to the next column.

2> if there is a dead end in column N, if the column is in the first column, the game fails. Otherwise, the system will go back to the previous column and perform backtracking.

3> If the security position is found in column 8th, the game is successful.

 

3. Source Code (selected from netizens)

Non-Recursive Backtracking Method

# Include <iostream> <br/> using namespace STD; </P> <p> # define N 8 <br/> // n represents the number of Queens <br/> void Queen () <br/>{< br/> int COUNT = 0; // calculate the total number of solutions <br/> int column [n + 1]; // column [m] = n indicates column M, and the Queen is placed in row n. The following table starts from 0. <br/> int row [n + 1]; // row [m] = 1 indicates that there is no queen in Row M, and = 0 indicates there is a queen <br/> int B [2 * n + 1]; // B [m] = 1 indicates that there is no queen on the main diagonal line of Article M. <br/> int C [2 * n + 1]; // C [m] = 1 indicates that there is no queen on the diagonal line of article m, and = 0 indicates that there is a queen <br/> int numqueen = 1; // count the number of queens already placed, when numqueen = N, it indicates that the test has been completed. <br/> int good = 1; // good = 1 indicates that no conflict has occurred, good = 0 indicates a conflict </P> <p> // initialize these tags <br/> for (Int J = 0; j <n + 1; ++ J) <br/>{< br/> row [J] = 1; // no Queen <br/>}< br/> for (Int J = 0; j <2 * n + 1; ++ J) <br/>{< br/> B [J] = C [J] = 1; <br/>}< br/> column [1] = 1; <br/> column [0] = 0; // initialize the first column of the first row, in the second row, place the Queen in the second column <br/> DO <br/> {<br/> // if no conflict occurs, continue to the downward detection, add the Queen or determine whether the current solution is <br/> If (good) <br/> {<br/> // the current number of Queens is solution, print, continue the downward test <br/> If (numqueen = N) <br/>{< br/> count ++; <br/> cout <"find solution" <Endl; <br/> for (Int J = 1; j <n + 1; ++ J) <br/> {<br/> cout <j <"column" <column [J] <"row" <Endl; <br/>}< br/> // move the last pawn down to the last one in this column <br/> while (column [numqueen] = N) <br/>{< br/> numqueen --; // The number of Queens minus 1, that is, the number of columns minus 1, <br/> // modify the status position of the column and the last row of the column <br/> // modify the status position of the column [numqueen] In the numqueen column <br/> row [column [numqueen] = 1; <br/> B [numqueen + column [numqueen] = 1; <br/> C [n + numqueen-column [numqueen] = 1; <br/>}< br/> column [numqueen] ++; // trace back to the first row, continue to test in the next column of the upper row <br/>}< br/> // The current is not the solution, then continue to the downward test <br/> else <br/>{< br/> // change the corresponding flag of the location <br/> row [column [numqueen] = 0; <br/> B [numqueen + column [numqueen] = 0; <br/> C [n + numqueen-column [numqueen] = 0; <br/> // The location is not correctly resolved because no conflict exists, in this case, the first row of the next column should be detected. <br/> column [++ numqueen] = 1; <br/>}< br/> // if a conflict occurs, continue to the next row in this column, back to the last column <br/> else <br/>{< br/> while (column [numqueen] = N) // to the last row of the column, or conflict, back to the previous column <br/>{< br/> numqueen --; <br/> row [column [numqueen] = 1; <br/> B [numqueen + column [numqueen] = 1; <br/> C [n + numqueen-column [numqueen] = 1; <br/>}< br/> column [numqueen] ++; // if a conflict occurs, the last row of the column does not exist, in this column, continue to the next row of detection <br/>}< br/> // check whether the position is conflicted after it is placed <br/> good = row [column [numqueen] & B [numqueen + column [numqueen] & C [n + numqueen-column [numqueen]; <br/>}while (numqueen); <br/> cout <n <" found a total of solutions:" <count <"" <Endl; <br/>}< br/> int main () <br/>{< br/> Queen (); <br/> system ("pause "); </P> <p> return 0; <br/>}< br/>

 

Better answers
 

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.