Given integers current_row and current_col, representing the current position of a Rook on an 8 × 8 chessboard and two more integers destination_row and destination_col which represents the position to be reached by a Rook. The task is to check if it is possible or not for a Rook to reach the given destination in a single move from its current position. If found to be true, print “POSSIBLE”. Otherwise, print “NOT POSSIBLE”.
Input: current_row=8, current_col=8, destination_row=8, destination_col=4 Output: POSSIBLE Explanation:
Input: current_row=3, current_col=2, destination_row=2, destination_col=4 Output: NOT POSSIBLE Explanation:
Approach: The given problem can be solved using the following observation:
On a chessboard, a rook can move as many squares as possible, horizontally as well as vertically, in a single move. Therefore, it can move to any position present in the same row or the column as in its initial position.
Therefore, the problem reduces to simply checking for the following two conditions:
If destination_row and current_row are equal or not.
Otherwise, check if destination_col is equal to current_col or not.
If any of the above two conditions are satisfied, print “POSSIBLE“. Otherwise, print “NOT POSSIBLE”.
Below is the implementation of the above approach:
Please Login to comment...