Eight-Queen problem

In my opinion, learning is a kind of instinct, and everyone has their own learning strategies even though they do not know any learning theories. Today, I will share a story about how I overcame a learning challenge and what strategies I used.

That was my first year of university, I spend a lot time on programming and I always try to solve some problem using programming. Then I found a hard but fun problem, the Eight-Queen problem. First of all, what is a Eight-Queen problem?

From google image

On a chess board, there 8 rows and 8 columns and the queen can move in 8 directions as figure above shows. The question is asking how many plans we can put 8 queens on the board and they cannot touch each other.

To deal with this problem, I first started it by trying to find one solution. I tried to put queen at different position and I found something, a queen will occupy one row and one column, so each queen has to be in different row and column. I guess this stage is called cognitivism, I was trying to understand what this problem is talking about and how I could solve it.

Cognitivism can help us to understand problems better, but it is not enough to solve computer science problems. To solve these problems, we have to import math. Trying to transfer problem into a math model is the key. As the figure above shows, I wrote down the number of rows and columns, then I found something new. For all pieces at the same row/column, they have the same row/column number. For all pieces at the same sideways, their either satisfy that the sum of row number and column number is the same or the difference between the row number and column number is the same. For example, For example, the piece at bottom-left corner has row number 8 and column number 1, the piece at top-right corner has row number 1 and column number 8, the sum of row and column is for them is 9. The piece at top-left corner has row number 1 and column number 1, the piece at bottom-right corner has row number 8 and column number 8, the difference between row and column is for them is 0. I call this process constructivism, to find the construction of similar problems and make one solution to solve them all. By using these strategies, I finally overcame this learning challenge and the code is below and it has 92 solutions (I will show the last two of them).

Self screenshot
Self screenshot

The number 1 means we can put a queen there. Thanks for reading and hope you like it!

2 Responses

  • Hi Tony,

    Thank you for writing such a fascinating and instructive blog. I’ve never played chess and have no clue how it works. This blog article is beneficial for folks who are unfamiliar with the game, such as me. It also is beneficial to those who are inexperienced in programming.

    The only thing I would advise is that some people may struggle to comprehend what you meant in the code, so perhaps additional comments in the code might help readers understand better.

    This is a significant contribution to your first blog. Congratulations on your ingenuity in combining chess with coding!

Leave a Reply

Your email address will not be published. Required fields are marked *