📌  相关文章
📜  检查白嘴鸦是否可以单步到达给定的目的地

📅  最后修改于: 2021-04-17 17:08:11             🧑  作者: Mango

给定整数current_rowcurrent_col ,它们表示Rook在8 X 8棋盘上的当前位置,以及另外两个整数destination_rowdestination_col ,它们表示Rook将到达的位置。任务是检查Rook是否有可能从其当前位置进行一次移动到达指定目的地。如果发现是真的,则打印“ POSSIBLE” 。否则,打印“不可能”

例子:

方法:可以通过以下观察来解决给定的问题:

因此,问题可以简化为简单地检查以下两个条件:

  • 如果destination_row和current_row是否相等。
  • 否则,请检查destination_col是否等于current_col。
  • 如果满足以上两个条件中的任何一个,则打印“可能”。否则,打印“不可能”

下面是上述方法的实现:

C++14
// C++ program to implement
// for the above approach
#include 
using namespace std;
 
// Function to check if it is
// possible to reach destination
// in a single move by a rook
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
int 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
// Java program for the above approach
import java.util.*;
import java.lang.*;
 
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(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.


Python3
# Python program to implement
# for the above approach
 
 
# Function to check if it is
# possible to reach destination
# in a single move by a rook
def 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 Code
current_row = 8
current_col = 8
destination_row = 8
destination_col = 4
 
 
output = check(current_row, current_col,
               destination_row, destination_col)
print(output)


C#
// C# program to implement
// the above approach
using 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.


输出:
POSSIBLE

时间复杂度: O(1)
空间复杂度:O(1)