The article directories

    • What is the eight Queens problem?
    • graphic
    • One of the solution
    • The test results
    • Other solutions

What is the eight Queens problem?

The eight Queens problem is an ancient problem, proposed by a chess player in 1848: how to solve an 8×8 chess board with eight queens so that they cannot attack each other, that is, no two queens can be in the same row, column or slash?

graphic







Something like that.

One of the solution

#include<iostream>

using namespace std;

#define MAX_NUM 8		// Number of queens

int queen[MAX_NUM][MAX_NUM] = { 0 };

bool check(int x, int y) {	// Check if a coordinate can be placed
	for (int i = 0; i < y; i++) {
		if (queen[x][i] == 1) {	// Whether this line can exist
			return false;
		}
		if (x - 1 - i > 0 && queen[x - 1 - i][y - 1 - i] == 1) {	// Check the left oblique column
			return false;
		}
		if (x + 1 + i < MAX_NUM && queen[x + 1 + i][y + 1 + i] == 1) {	// Check the right oblique column
			return false;
		}
	}
	queen[x][y] = 1;
	return true;
}

void showQueen(a) {
	for (int i = 0; i < MAX_NUM; i++) {
		for (int j = 0; j < MAX_NUM; j++) {
			cout << queen[i][j] << "";
		}
		cout << endl;
	}
	cout << endl;
}

bool settleQueen(int x) {
	if (x == MAX_NUM) {	// Find the answer
		return true;
	}
	for (int i = 0; i < MAX_NUM; i++) {
		for (int y = 0; y < MAX_NUM; y++) {
			queen[y][x] = 0;	// Clear the current column to avoid disturbing backtracking
		}
		if (check(i,x)) {	// If this line is found
			queen[i][x] = 1;
			showQueen(a);// Visualize test results
			if (settleQueen(x + 1)) {	// It's time to go left
				return true;	// All the way to the left}}}return false;	// If not, return it
}
Copy the code

The test results

Test process print:

1 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

1 0 0 0 0 0 0 0

0 1 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 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1 0 0 0

1 0 0 0 0 0 0 0

0 1 0 0 0 0 0 0

0 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 1 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0

1 0 0 0 0 0 0 0

0 1 0 0 0 0 0 0

0 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0

0 0 0 0 0 0 1 0

0 0 0 1 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0

1 0 0 0 0 0 0 0

0 1 0 0 0 0 0 0

0 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0

0 0 0 0 0 0 1 0

0 0 0 1 0 0 0 0

0 0 0 0 0 0 0 1

0 0 0 0 1 0 0 0


Print in main

1 0 0 0 0 0 0 0

0 1 0 0 0 0 0 0

0 0 0 0 0 1 0 0

0 0 1 0 0 0 0 0

0 0 0 0 0 0 1 0

0 0 0 1 0 0 0 0

0 0 0 0 0 0 0 1

0 0 0 0 1 0 0 0

C:\Users\asus\source\repos\EightQuene\Debug\ eightquene.exe (process 9184) has exited with code 0. Press any key to close this window..

Other solutions

I’ve seen other solutions explained before, either horizontally, or using bitmaps. But don’t be fooled by the pictures above. Make sure you don’t set 1 for the places you can’t walk. You should only set 1 for the places you can’t walk, otherwise it will cause immeasurable trouble for backtracking.