Given the position of a Bishop on an 8 * 8 chessboard, the task is to count the total number of squares that can be visited by the Bishop in one move. The position of the Bishop is denoted using row and column number of the chessboard.
Examples:
Input: Row = 4, Column = 4
Output: 13
Input: Row = 1, Column = 1
Output: 7
Approach: In the game of chess, a Bishop can only move diagonally and there is no restriction in distance for each move.
So, We can also say that Bishop can move in four ways i.e. diagonally top left, top right, bottom left and bottom right from current position.
We can calculate the numbers of squares visited in each move by:
Total squares visited in Top Left move = Math.min(r, c) – 1
Total squares visited in Top Right move = Math.min(r, 9 – c) – 1
Total squares visited in Bottom Left move = 8 – Math.max(r, 9 – c)
Total squares visited in Bottom Right move = 8 – Math.max(r, c)
where, r and c are the coordinates of the current position of the Bishop on the chessboard.
Guys I cannot figure out the logic behind the above diagonal calculations. Please help me find out that by what logic the diagonals are calculated above ??? I JUST NEED THE LOGIC. No code required.
( Also If you can help with the basic behind logic of similar kinds of chess problems or matrix it would be fantastic)