Easy to understand Java code 8 queen question

Source: Internet
Author: User

Soon Blue Bridge Cup match, I these algorithms still not, indeed a bit of panic, today morning to sleep late not to get up, then began to study the 8 queen question. This is also a typical backtracking and recursion problem. In fact, and horse tread board problem very similar, eight Queen question, is to judge the main diagonal, diagonal, horizontal and vertical row can not have queen. This is the focus of the problem. Let's take a look at the eight Queens question.

1. Description of the problem:

Put 8 queens in the 8*8 's chessboard so that each queen cannot be placed on the same row, in the same column, on the same main diagonal (lower left), on the same diagonal (top right).

2. Input: None (of course, can also be optimized for any queen, this is not the focus)

3. Sample output:

1 * * * * * * *
* * * * 2 * * *
* * * * * * * * 3
* * * * * * 4 * *
* * 5 * * * * *
* * * * * * * 6 *
* 7 * * * * * *
* * * 8 * * * *

===============

1 * * * * * * *
* * * * * * 2 * *
* * * * * * * * 3
* * 4 * * * * *
* * * * * * * 5 *
* * * 6 * * * *
* 7 * * * * * *
* * * * 8 * * *

===============

...........

A total of 92 methods

4. Algorithm ideas:

The whole idea is to use recursion and backtracking, and horse tread board very similar, but more difficult than the horse tread board, because to determine whether the diagonal conflict, I judge here is a vis[3][20] array, why the beginning is 3, because it is to judge 4 directions can not have queen, but recursion is each column, Recursive x,x+1 for each column ... this inevitably does not cause a queen on the same column, only 3 additional directions are required, 0 means the same row has a Queen, 1 means the main diagonal has a queen, and 2 indicates a queen on the diagonal.  Besides, how do you judge if there are queens on these diagonal lines? If I put the element in column x, line I, then vis[0][i] = 1 means I put the element in line I. Vis[1][x+i] = 1, indicating that if I put the Queen in column X, line I, then the diagonal, column x+1, i-1 the number of rows can not put the Queen, the first x-1 column, the first i+1 line can not put the Queen, exactly is on the sub-diagonal. VIS[2][X-I+8] = 1, indicating that if x==i, then constant equals vis[2][8] = 1 can not be put, that is, the main diagonal. With these three conditions both are possible.

5. The code is as follows:
Import Java.util.Scanner;

public class Eightqueen {
static int qipan[][] = new INT[8][8];
static int count = 0;
static int step = 1;
static int vis[][] = new INT[3][20]; Three cases main diagonal, diagonal, row has not been occupied.
public static void Main (string[] args) {
Initialize the board
for (int i=0;i<8;i++) {
for (int j=0;j<8;j++) {
QIPAN[I][J] = 0;
}
}
for (int i=0;i<vis.length;i++) {
for (int j=0;j<vis[i].length;j++) {
VIS[I][J] = 0;
}
}

Move (0);
Starting from the No. 0 position to put the first Queen, not to say, only ask for the Queen's position, and the number of places to be placed.
System.out.println ("Count =" + count);
}
public static void Move (int x) {
int next_x = x+1;
if (step>8) {
for (int i=0;i<8;i++) {
for (int j=0;j<8;j++) {
if (qipan[i][j] = = 0) {
System.out.printf ("%3c", ' * '); Put no queen, the chessboard is 0 position with * output, look good-looking
}else{
System.out.printf ("%3d", Qipan[i][j]);
}

}
System.out.println ();
}
System.out.println ("========================");
count++;
}else{
for (int i=0;i<8;i++) {
if (vis[0][i]==0 && vis[1][x+i]==0 && vis[2][x-i+8]==0) {//Meet placing conditions
Qipan[x][i] = step;
step++;
Vis[0][i]=1; Vis[1][x+i]=1; Vis[2][x-i+8]=1;
Move (next_x);
Qipan[x][i] = 0;
vis[0][i]=0; vis[1][x+i]=0; vis[2][x-i+8]=0;
step--;

}
}
}
}
}

Easy to understand Java code 8 queen question

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.