Check if a Rook can reach the given destination in a single move - GeeksforGeeks

03 May, 2021
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”.
Examples:
Input: current_row=8, current_col=8, destination_row=8, destination_col=4 Output: POSSIBLEExplanation:  
Possible
Input: current_row=3, current_col=2, destination_row=2, destination_col=4 Output: NOT POSSIBLE Explanation:  
Not Possible
 
 
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:
C++14
Java
Python3
C#
Javascript
// C++ program to implement// for the above approach#include <bits/stdc++.h>using namespace std; // Function to check if it is// possible to reach destination// in a single move by a rookstring check(int current_row, int current_col,             int destination_row, int destination_col){     if(current_row == destination_row)        return "POSSIBLE";    else if(current_col == destination_col)        return "POSSIBLE";    else        return "NOT POSSIBLE";} // Driver Codeint main(){     // Given arrays  int current_row = 8;  int current_col = 8;  int destination_row = 8;  int destination_col = 4;  string output = check(current_row, current_col,                        destination_row, destination_col);  cout << output;  return 0;} // This code is contributed by mohit kumar 29.
// Java program for the above approachimport java.util.*;import java.lang.*; class GFG{     // Function to check if it is// possible to reach destination// in a single move by a rookstatic String check(int current_row, int current_col,             int destination_row, int destination_col){     if(current_row == destination_row)        return "POSSIBLE";    else if(current_col == destination_col)        return "POSSIBLE";    else        return "NOT POSSIBLE";} // Driver codepublic static void main(String[] args){    // Given arrays  int current_row = 8;  int current_col = 8;  int destination_row = 8;  int destination_col = 4;  String output = check(current_row, current_col,                        destination_row, destination_col);  System.out.println(output);}} // This code is contributed by code_hunt.
# Python program to implement# for the above approach  # Function to check if it is# possible to reach destination# in a single move by a rookdef check(current_row, current_col,           destination_row, destination_col):         if(current_row == destination_row):        return("POSSIBLE")    elif(current_col == destination_col):        return("POSSIBLE")    else:        return("NOT POSSIBLE") # Driver Codecurrent_row = 8current_col = 8destination_row = 8destination_col = 4  output = check(current_row, current_col,               destination_row, destination_col)print(output)
// C# program to implement// the above approachusing System; class GFG{   // Function to check if it is  // possible to reach destination  // in a single move by a rook  static string check(int current_row, int current_col,                      int destination_row, int destination_col)  {     if(current_row == destination_row)      return "POSSIBLE";    else if(current_col == destination_col)      return "POSSIBLE";    else      return "NOT POSSIBLE";  }   // Driver Code  public static void  Main()  {    // Given arrays    int current_row = 8;    int current_col = 8;    int destination_row = 8;    int destination_col = 4;    string output = check(current_row, current_col,                          destination_row, destination_col);    Console.WriteLine(output);  }} // This code is contributed by susmitakundugoaldanga.
<script>// javascript program of the above approach // Function to check if it is// possible to reach destination// in a single move by a rookfunction check(current_row, current_col,             destination_row, destination_col){     if(current_row == destination_row)        return "POSSIBLE";    else if(current_col == destination_col)        return "POSSIBLE";    else        return "NOT POSSIBLE";}     // Driver Code        // Given arrays  let current_row = 8;  let current_col = 8;  let destination_row = 8;  let destination_col = 4;  let output = check(current_row, current_col,                        destination_row, destination_col);  document.write(output); </script>
POSSIBLE
 
Time complexity: O(1) Space complexity: O(1)
mohit kumar 29
code_hunt
susmitakundugoaldanga
avijitmondal1998
chessboard-problems
Greedy
Pattern Searching
Greedy
Pattern Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive)
Dijkstra’s Algorithm for Adjacency List Representation | Greedy Algo-8
Program for Least Recently Used (LRU) Page Replacement algorithm
Difference between Prim's and Kruskal's algorithm for MST
Split the given array into K sub-arrays such that maximum sum of all sub arrays is minimum
KMP Algorithm for Pattern Searching
Rabin-Karp Algorithm for Pattern Searching
Check if a string is substring of another
Naive algorithm for Pattern Searching
Boyer Moore Algorithm for Pattern Searching