Problem 306. Eight Queens Solution Checker
Write a function to verify whether an arrangement of queens on a chessboard is a valid solution to the classic eight queens problem.
In the eight queens problem, eight queens must be placed on a chessboard such that no two queens attack each other. That is, no two queens can share the same row, column, or diagonal. The diagram below is one possible solution:
Your function should take an 8-by-8 matrix of 0s and 1s, where the 1s represent the position of the queens, and return a logical 1 if the solution is valid or a logical 0 otherwise.
EXAMPLE 1
in1 = [ ...
0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 ];
isEightQueensSolution(in1)
returns 1.
EXAMPLE 2
in2 = [ ...
0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0 ];
isEightQueensSolution(in2)
returns 0. (Notice that the queens on the bottom two rows share a diagonal.)
Solution Stats
Problem Comments
-
2 Comments
@bmtran (Bryant Tran)
on 11 Feb 2012
This question is kind of a duplicate of Ned's N-queens checker: http://www.mathworks.com/matlabcentral/cody/problems/113-n-queens-checker
Steve Eddins
on 12 Feb 2012
Oops, I didn't know about that one!
Solution Comments
Show commentsGroup

Chess
- 18 Problems
- 4 Finishers
- Count the peaceful queens
- Valid Chess Moves
- The Dark Knight
- Knight's Watch
- N-Queens Checker
- Knight's Tour Checker
- Eight Queens Solution Checker
- Queen's move
- Queen's move - 02
- Checkmate
- Checkmate-02
- Can the knight take out the pawn?
- Castling-01
- Castling-02
- Castling-03
- chess position
- En passant - 01
- King's Cage
Problem Recent Solvers159
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!