Given the position of a Bishop and a Pawn on a 8 * 8 chessboard, the task is to check whether the Bishop can take down the Pawn in one move or not. The position of Bishop and Pawn is denoted using row and column number of the chessboard.
Input: bishopX = 5, bishopY = 5, pawnX = 1, pawnY = 1
Input: bishopX = 5, bishopY = 5, pawnX = 1, pawnY = 3
Approach: In a game of chess, the Bishop can only move diagonally and there is no restriction in the distance for each move. If we consider Bishop’s position as origin, it can be concluded that Bishop can attack at 45 degree, 135 degree, 225 degree and 315 degree angles only. So, the Bishop can attack the Pawn only if they are in such position that the slop obtained from their position is equal to tangent of 45 degree or 135 degree or 225 degree or 315 degree angle.
As we know the equation of slope for two points-
For all the attacking angles of Bishop (i.e 45°, 135°, 225°, and 315°) value of slope will be either 1 or -1.
tan(45°) = 1
tan(135°) = -1
tan(225°) = 1
tan(315°) = -1
So, for angle 45° and 225°, equation of slope will be
x2 – x1 = y2 – y1
and for angle 135° and 315°, equation of slope will be
-x2 + x1 = y2 – y1
- Considering the position of Bishop and Pawn as points on 2-D co-ordinate, Check if any of two equation is true
- x2 – x1 = y2 – y1
- -x2 + x1 = y2 – y1
- Otherwise print No.
Below is the implementation of the above approach:
- Chessboard Pawn-Pawn game
- Count the total number of squares that can be visited by Bishop in one move
- Check whether it is possible to convert A into B
- Check whether a given number is even or odd
- Check whether the exchange is possible or not
- Check n^2 - m^2 is prime or not
- Check whether N is a Factorion or not
- Check divisibility by 7
- Check if two numbers are co-prime or not
- Check whether a + b = c or not after removing all zeroes from a,b and c
- Check if the given chessboard is valid or not
- Check if the given string is K-periodic
- Check if a given matrix is Hankel or not
- Check if N is a Factorial Prime
- Check if a number is divisible by 41 or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : jit_t