Problem 45238. Checkmate
- The positions of different chess pieces are given in a cell p .
- The position of the king is given as x
Determine if the opponent king is in check for individual scenerios.
- x='Kg7'
- p={'Qe5','Qg5','Qb5','Qa7'}
output= [1,1,0,1]
N.b.any invalid position should produce 0. u may imagine p as the white pieces and x as the black king for clarity.
Solution Stats
Problem Comments
-
5 Comments
Asif,
Thanks, but my point is that the pawn is in row-1 in *both* cases, so they should both be in check or neither should be in check. Since you described 'x' as being the 'opponent King', I think that both should be in check.
william,
i might not have understood u totally.
what i meant to say is that -- there are some invalid position/moves. for example, the pawn can jump from a2 to a4 but not from a3 to a5-thats an invalid move. similarly a white pawn can never be at the location a1-- their position starts from a2-so placing a pawn in row-1 is an invalid move.
in test-6, the pawn in e1 could check the king - but its own position is invalid.it could not be in that position in the 1st place. thats why 0.
in test-2,3; the pawn is in h6. a pawn can be in the column-h or row-6.so thats a valid position.
https://en.wikipedia.org/wiki/Pawn_(chess)
Asif,
Aha! Tricky, and very clever. Thanks for posting this interesting problem!
Solution Comments
Show commentsProblem Recent Solvers8
Suggested Problems
-
281 Solvers
-
371 Solvers
-
Back to basics 22 - Rotate a matrix
909 Solvers
-
Make a run-length companion vector
642 Solvers
-
Sum the elements in either diagonal of a square matrix
213 Solvers
More from this Author165
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!