📌  相关文章
📜  仅通过加号运动检查是否可以精确地Z步从原点到达(x,y)

📅  最后修改于: 2021-05-04 15:56:37             🧑  作者: Mango

给定点(x,y) ,任务是检查是否有可能以正Z步从原点到达(x,y) 。从给定点(x,y),我们只能在四个方向上向左(x – 1,y) ,向右(x + 1,y) ,向上(x,y + 1)和向下(x,y – 1)移动)
例子:

方法:

  1. 从原点到(x,y)的最短路径是| x | + | y |
  2. 因此,很明显,如果Z小于| x | + | y | ,那么我们就无法精确地在Z步中从原点到达(x,y)
  3. 如果步数不小于| x | + | y |然后,我们必须检查以下两个条件,以检查是否可以达到(x,y)
    • 如果Z≥| x | + | y | , 和
    • 如果(Z – | x | + | y |)%20
  4. 对于上述步骤中的第二个条件,如果达到(x,y) ,则可以再执行两个步骤,例如(x,y)–>(x,y + 1)–>(x,y)到相同位置(x,y) 。而且只有当它们之间的差异相等时才有可能。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if it is possible to
// reach (x, y) from origin in exactly z steps
void possibleToReach(int x, int y, int z)
{
 
    // Condition if we can't reach in Z steps
    if (z < abs(x) + abs(y)
        || (z - abs(x) - abs(y)) % 2) {
        cout << "Not Possible" << endl;
    }
    else
        cout << "Possible" << endl;
}
 
// Driver Code
int main()
{
    // Destination point coordinate
    int x = 5, y = 5;
 
    // Number of steps allowed
    int z = 11;
 
    // Function Call
    possibleToReach(x, y, z);
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to check if it is possible
// to reach (x, y) from origin in
// exactly z steps
static void possibleToReach(int x, int y, int z)
{
     
    // Condition if we can't reach in Z steps
    if (z < Math.abs(x) + Math.abs(y) ||
       (z - Math.abs(x) - Math.abs(y)) % 2 == 1)
    {
        System.out.print("Not Possible" + "\n");
    }
    else
        System.out.print("Possible" + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Destination point coordinate
    int x = 5, y = 5;
 
    // Number of steps allowed
    int z = 11;
 
    // Function Call
    possibleToReach(x, y, z);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to check if it is possible to
# reach (x, y) from origin in exactly z steps
def possibleToReach(x, y, z):
 
    #Condition if we can't reach in Z steps
    if (z < abs(x) + abs(y) or
       (z - abs(x) - abs(y)) % 2):
        print("Not Possible")
    else:
        print("Possible")
 
# Driver Code
if __name__ == '__main__':
     
    # Destination pocoordinate
    x = 5
    y = 5
 
    # Number of steps allowed
    z = 11
 
    # Function call
    possibleToReach(x, y, z)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to check if it is possible
// to reach (x, y) from origin in
// exactly z steps
static void possibleToReach(int x, int y, int z)
{
     
    // Condition if we can't reach in Z steps
    if (z < Math.Abs(x) + Math.Abs(y) ||
       (z - Math.Abs(x) - Math.Abs(y)) % 2 == 1)
    {
        Console.Write("Not Possible" + "\n");
    }
    else
        Console.Write("Possible" + "\n");
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Destination point coordinate
    int x = 5, y = 5;
 
    // Number of steps allowed
    int z = 11;
 
    // Function Call
    possibleToReach(x, y, z);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
Not Possible

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