📌  相关文章
📜  检查是否可以N步从(0,0)移至(x,y)

📅  最后修改于: 2021-06-25 20:48:21             🧑  作者: Mango

给定一个点(x,y)。查找是否可以精确地以n步从(0,0)移至(x,y)。 4种类型的步骤有效,您可以从点(a,b)移至(a,b + 1),(a,b-1),(a-1,b),(a + 1, b)

例子

Input: x = 0, y = 0, n = 2
Output: POSSIBLE

Input: x = 1, y = 1, n = 3 
Output: IMPOSSIBLE

方法

因此,如果

下面是上述方法的实现:

C++
// CPP program to check whether it is possible
// or not to move from (0, 0) to (x, y)
// in exactly n steps
#include 
using namespace std;
  
// Function to check whether it is possible
// or not to move from (0, 0) to (x, y)
// in exactly n steps
bool Arrive(int a, int b, int n)
{
    if (n >= abs(a) + abs(b) and (n - (abs(a) + abs(b))) % 2 == 0)
        return true;
  
    return false;
}
  
// Driver code
int main()
{
    int a = 5, b = 5, n = 11;
  
    if (Arrive(a, b, n))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java program to check whether it is possible
// or not to move from (0, 0) to (x, y)
// in exactly n steps
import java.io.*;
  
public class GFG {
  
// Function to check whether it is possible
// or not to move from (0, 0) to (x, y)
// in exactly n steps
static boolean Arrive(int a, int b, int n)
{
    if (n >= Math.abs(a) + Math.abs(b) && (n - (Math.abs(a) + Math.abs(b))) % 2 == 0)
        return true;
  
    return false;
}
  
// Driver code
int main()
{
  
    return 0;
}
  
    public static void main (String[] args) {
          
    int a = 5, b = 5, n = 11;
  
    if (Arrive(a, b, n))
        System.out.println( "Yes");
    else
        System.out.println( "No");
    }
}
//This code is contributed by shs..


Python3
# Python3 program to check whether 
# it is possible or not to move from 
# (0, 0) to (x, y) in exactly n steps 
  
# Function to check whether it is 
# possible or not to move from 
# (0, 0) to (x, y) in exactly n steps 
def Arrive(a, b, n):
      
    if (n >= abs(a) + abs(b) and 
       (n - (abs(a) + abs(b))) % 2 == 0):
        return True
  
    return False
  
# Driver code 
a = 5
b = 5
n = 11
  
if (Arrive(a, b, n)): 
    print("Yes") 
else:
    print("No") 
  
# This code is contributed
# by Yatin Gupta


C#
// C# program to check whether 
// it is possible or not to 
// move from (0, 0) to (x, y)
// in exactly n steps
using System;
  
class GFG 
{
  
// Function to check whether it 
// is possible or not to move 
// from (0, 0) to (x, y) in
// exactly n steps
static bool Arrive(int a, int b, int n)
{
    if (n >= Math.Abs(a) + Math.Abs(b) && 
       (n - (Math.Abs(a) + Math.Abs(b))) % 2 == 0)
        return true;
  
    return false;
}
  
// Driver code
public static void Main () 
{
    int a = 5, b = 5, n = 11;
  
    if (Arrive(a, b, n))
        Console.WriteLine( "Yes");
    else
        Console.WriteLine( "No");
    }
}
  
// This code is contributed by shashank


PHP
= abs($a) + abs($b) and
       ($n - (abs($a) + abs($b))) % 2 == 0)
        return true;
  
    return false;
}
  
// Driver code
$a = 5; $b = 5; $n = 11;
  
if (Arrive($a, $b, $n))
    echo "Yes";
else
    echo "No";
  
// This code is contributed 
// by Akanksha Rai(Abby_akku)


输出:
No

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。