📌  相关文章
📜  以固定大小的跳跃在二维路径中从源到目标

📅  最后修改于: 2021-06-26 12:30:13             🧑  作者: Mango

给定路径的源点和目标点以及两个整数xy 。任务是检查是否可以通过以下动作从源移动到目标。如果当前位置是(a,b),则有效移动是:

  1. (a + x,b + y)
  2. (a – x,b + y)
  3. (a + x,b – y)
  4. (a – x,b – y)

例子:

方法:让我们来解决这个问题,就好像步骤是(a,b)->(a + x,0)或(a,b)->(a – x,0)或(a,b)->(0 ,b + y)或(a,b)->(0,b – y)。如果| Sx – Dx |,则答案为 。 mod x = 0并且| Sy – Dy | mod y = 0

不难发现,如果对这个问题的答案为“否”,那么对原始问题的答案也为“否”。

让我们回到最初的问题,看一些步骤序列。它在某个点(x e ,y e )结束。将cnt x定义为| x e – Sx | / xcnt y| y e – Sy | / ycnt x的奇偶性与cnt y的奇偶性相同,因为每种移动方式都会改变cnt xcnt y的奇偶性。

因此,如果| Sx – Dx | ,答案是肯定的。 mod x = 0| Sy – Dy | mod y = 0| Sx – Dx | / x mod 2 = | Sy – Dy | / y mod 2

下面是上述方法的实现。

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true if
// it is possible to move from source
// to the destination with the given moves
bool isPossible(int Sx, int Sy, int Dx, int Dy, int x, int y)
{
    if (abs(Sx - Dx) % x == 0
        and abs(Sy - Dy) % y == 0
        and (abs(Sx - Dx) / x) % 2 == (abs(Sy - Dy) / y) % 2)
        return true;
  
    return false;
}
  
// Driver code
int main()
{
    int Sx = 0, Sy = 0, Dx = 0, Dy = 0;
    int x = 3, y = 4;
  
    if (isPossible(Sx, Sy, Dx, Dy, x, y))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of the approach
import java .io.*;
  
class GFG
{
      
// Function that returns true if
// it is possible to move from source
// to the destination with the given moves
static boolean isPossible(int Sx, int Sy, int Dx,
                          int Dy, int x, int y)
{
    if (Math.abs(Sx - Dx) % x == 0 && 
        Math.abs(Sy - Dy) % y == 0 && 
       (Math.abs(Sx - Dx) / x) % 2 == 
       (Math.abs(Sy - Dy) / y) % 2)
        return true;
  
    return false;
}
  
// Driver code
public static void main(String[] args)
{
    int Sx = 0, Sy = 0, Dx = 0, Dy = 0;
    int x = 3, y = 4;
  
    if (isPossible(Sx, Sy, Dx, Dy, x, y))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by inder_verma..


Python3
# Python3 implementation of the approach
  
# Function that returns true if it is 
# possible to move from source to the 
# destination with the given moves
def isPossible(Sx, Sy, Dx, Dy, x, y):
    if (abs(Sx - Dx) % x == 0 and 
        abs(Sy - Dy) % y == 0 and 
       (abs(Sx - Dx) / x) % 2 == 
       (abs(Sy - Dy) / y) % 2):
        return True;
    return False;
  
# Driver code
Sx = 0; 
Sy = 0; 
Dx = 0; 
Dy = 0; 
x = 3; 
y = 4;
  
if (isPossible(Sx, Sy, Dx, Dy, x, y)):
    print("Yes");
else:
    print("No");
  
# This code is contributed by mits


C#
// C# implementation of the approach
using System;
  
class GFG
{
      
// Function that returns true if
// it is possible to move from source
// to the destination with the given moves
static bool isPossible(int Sx, int Sy, int Dx,
                        int Dy, int x, int y)
{
    if (Math.Abs(Sx - Dx) % x == 0 && 
        Math.Abs(Sy - Dy) % y == 0 && 
        (Math.Abs(Sx - Dx) / x) % 2 == 
        (Math.Abs(Sy - Dy) / y) % 2)
        return true;
  
    return false;
}
  
// Driver code
static void Main()
{
    int Sx = 0, Sy = 0, Dx = 0, Dy = 0;
    int x = 3, y = 4;
  
    if (isPossible(Sx, Sy, Dx, Dy, x, y))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
  
// This code is contributed by chandan_jnu


PHP


输出:
Yes

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