📌  相关文章
📜  检查点 (X, Y) 是否可以从原点 (0, 0) 到达同时垂直跳跃 1 和 N

📅  最后修改于: 2022-05-13 01:56:10.692000             🧑  作者: Mango

检查点 (X, Y) 是否可以从原点 (0, 0) 到达同时垂直跳跃 1 和 N

给定一个正整数N和坐标(X, Y) ,任务是检查是否有可能从(0, 0)到达(X, Y)并同时在垂直方向上跳跃1N。如果可以到达(X, Y)则打印Yes 。否则,打印No

例子:

方法:给定的问题可以基于观察到有 4 种可能的方式可以从任何坐标跳转来解决,它们是(1, N)、(1, -N)、(-1, N)(-1, -N) 。此外,问题可以分为3种不同的情况:

  • 情况 1 - 其中 N 是偶数在这种情况下,任何坐标 (X, Y) 都是可达的。让我们一次考虑 X 和 Y。对于坐标 X,可以遵循(1, N), (1, -N), (1, N), (1, -N), …的顺序跳转。结果坐标将是(X, 0) (即所需坐标)或(X, N) 。由于N是偶数,因此也可以遵循跳转序列(N, -1), (-N, -1), (N, -1), (N, -1), ...到达(X, 0) .同理, (0,Y)可以到达,结合跳跃的顺序到达(X,0)(0,Y),(X,Y)就可以到达。
  • 情况 2 – 其中 N 为奇数,X 和 Y 均为偶数或奇数这些情况可以通过遵循类似于情况 1的一系列操作来处理。
  • 情况 3 – 其中 N 为奇数,X 和 Y 具有不同的奇偶性在这种情况下,没有可能的跳转序列到达(X, Y),因为根据情况 2, (X + 1, Y)(X, Y + 1)必须是可达的,因为它们都将具有相同的奇偶校验,并且没有可能的操作序列来覆盖(1, 0)(0, 1)的距离。

因此,唯一不可能从(0, 0)到达(X, Y)的情况是N为奇数且XY具有不同的奇偶性,即X为偶数且Y为奇数,反之亦然。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if (X, Y) is reachable
// from (0, 0) using the jumps of given type
string checkReachability(int N, int X, int Y)
{
    // Case where source & destination
    // are the same
    if (X == 0 && Y == 0) {
        return "YES";
    }
 
    // Check for even N (X, Y) is
    // reachable or not
    if (N % 2 == 0) {
        return "YES";
    }
 
    // If N is odd and parity of X and
    // Y is different return, no valid
    // sequence of jumps exist
    else {
        if (X % 2 != Y % 2) {
            return "NO";
        }
        else {
            return "YES";
        }
    }
}
 
// Driver Code
int main()
{
    int N = 2;
    int X = 5, Y = 4;
    cout << checkReachability(N, X, Y);
 
    return 0;
}


Java
// Java code for the above approach
import java.io.*;
 
class GFG
{
   
  // Function to check if (X, Y) is reachable
// from (0, 0) using the jumps of given type
static String checkReachability(int N, int X, int Y)
{
   
    // Case where source & destination
    // are the same
    if (X == 0 && Y == 0) {
        return "YES";
    }
 
    // Check for even N (X, Y) is
    // reachable or not
    if (N % 2 == 0) {
        return "YES";
    }
 
    // If N is odd and parity of X and
    // Y is different return, no valid
    // sequence of jumps exist
    else {
        if (X % 2 != Y % 2) {
            return "NO";
        }
        else {
            return "YES";
        }
    }
}
 
// Driver Code
    public static void main (String[] args) {
      int N = 2;
    int X = 5, Y = 4;
    
        System.out.println(checkReachability(N, X, Y));
    }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python 3 program for the above approach
 
# Function to check if (X, Y) is reachable
# from (0, 0) using the jumps of given type
def checkReachability(N, X, Y):
   
    # Case where source & destination
    # are the same
    if (X == 0 and Y == 0):
        return "YES"
 
    # Check for even N (X, Y) is
    # reachable or not
    if (N % 2 == 0):
        return "YES"
 
    # If N is odd and parity of X and
    # Y is different return, no valid
    # sequence of jumps exist
    else:
        if (X % 2 != Y % 2):
            return "NO"
        else:
            return "YES"
 
# Driver Code
if __name__ == '__main__':
    N = 2
    X = 5
    Y = 4
    print(checkReachability(N, X, Y))
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# code for the above approach
using System;
 
class GFG
{
   
  // Function to check if (X, Y) is reachable
// from (0, 0) using the jumps of given type
static string checkReachability(int N, int X, int Y)
{
   
    // Case where source & destination
    // are the same
    if (X == 0 && Y == 0) {
        return "YES";
    }
 
    // Check for even N (X, Y) is
    // reachable or not
    if (N % 2 == 0) {
        return "YES";
    }
 
    // If N is odd and parity of X and
    // Y is different return, no valid
    // sequence of jumps exist
    else {
        if (X % 2 != Y % 2) {
            return "NO";
        }
        else {
            return "YES";
        }
    }
}
 
// Driver Code
    public static void Main (string[] args) {
      int N = 2;
      int X = 5, Y = 4;
    
    Console.WriteLine(checkReachability(N, X, Y));
    }
}
 
// This code is contributed by AnkThon


Javascript


输出:
YES

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