📌  相关文章
📜  检查是否可以通过在相邻的四个方向上移动来在给定时间内移动所有点

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

检查是否可以通过在相邻的四个方向上移动来在给定时间内移动所有点

给定大小为N的 3 个数组X[]、Y[]T[] ,其中X[i]Y[i]表示第i 个坐标, T[i]表示以秒为单位的时间。发现从起始坐标(0, 0)在时间T[i]内到达所有坐标(X[i], Y[i])是可能的。指针可以向四个方向移动(x +1, y)、(x - 1, y)、(x, y + 1)(x, y - 1)。从一个坐标到另一个坐标需要1秒,不能留在自己的宫殿。

例子:

方法:解决这个问题的想法是基于这样一个事实,即从第 i 个点移动到第(i+1) 个点需要abs(X[i+1] – X[i]) + abs(Y [i+1] – Y[i])时间。在第一个点的情况下,前一个点是(0, 0)。所以,如果这个时间小于T[i]那么它很好,否则它违反了条件。请按照以下步骤解决问题:

  • 使三个变量currentX、currentY、currentTime并初始化为零
  • 创建一个布尔变量isPossible初始化true
  • 使用变量i遍历范围[0, N)并执行以下任务:
    • 如果(abs(X[i] – currentX ) + abs( Y[i] – currentY ))大于(T[i] – currentTime)则使isPossible 为假。
    • 否则,如果((abs(X[i] – currentX ) + abs( Y[i] – currentY )) % 2不等于(T[i] – currentTime) % 2则使isPossible 为假。
    • 否则,用Xi、Yi 和 Ti更改currentX、currentYcurrentTime的先前值
  • 执行上述步骤后,返回isPossible的值作为答案。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if it is possible
// to traverse all the points.
bool CheckItisPossible(int X[], int Y[],
                       int T[], int N)
{
 
    // Make 3 variables to store given
    // ith values
    int currentX = 0, currentY = 0,
        currentTime = 0;
 
    // Also, make a bool variable to
    // check it is possible
    bool IsPossible = true;
 
    // Now, iterate on all the coordinates
    for (int i = 0; i < N; i++) {
 
        // check first condition
        if ((abs(X[i] - currentX)
             + abs(Y[i] - currentY))
            > (T[i] - currentTime)) {
            // means thats not possible to
            // reach current coordinate
            // at Ithtime from previous coordinate
            IsPossible = false;
            break;
        }
        else if (((abs(X[i] - currentX)
                   + abs(Y[i] - currentY))
                  % 2)
                 > ((T[i] - currentTime) % 2)) {
            // means thats not possible to
            // reach current coordinate
            // at Ithtime from previous coordinate
            IsPossible = false;
            break;
        }
        else {
            // If both above conditions are false
            // then we change the values of current
            // coordinates
            currentX = X[i];
            currentY = Y[i];
            currentTime = T[i];
        }
    }
 
    return IsPossible;
}
 
// Driver Code
int main()
{
    int X[] = { 1, 1 };
    int Y[] = { 2, 1 };
    int T[] = { 3, 6 };
    int N = sizeof(X[0]) / sizeof(int);
    bool ans = CheckItisPossible(X, Y, T, N);
 
    if (ans == true) {
        cout << "Yes"
             << "\n";
    }
    else {
        cout << "No"
             << "\n";
    }
    return 0;
}


Java
// Java program for the above approach
public class GFG {
     
    // Function to check if it is possible
    // to traverse all the points.
    static boolean CheckItisPossible(int X[], int Y[],
                        int T[], int N)
    {
     
        // Make 3 variables to store given
        // ith values
        int currentX = 0, currentY = 0,
            currentTime = 0;
     
        // Also, make a bool variable to
        // check it is possible
        boolean IsPossible = true;
     
        // Now, iterate on all the coordinates
        for (int i = 0; i < N; i++) {
     
            // check first condition
            if ((Math.abs(X[i] - currentX) +
                 Math.abs(Y[i] - currentY)) > (T[i] - currentTime)) {
                 
                // means thats not possible to
                // reach current coordinate
                // at Ithtime from previous coordinate
                IsPossible = false;
                break;
            }
            else if (((Math.abs(X[i] - currentX) +
                       Math.abs(Y[i] - currentY)) % 2) > ((T[i] - currentTime) % 2)) {
                // means thats not possible to
                // reach current coordinate
                // at Ithtime from previous coordinate
                IsPossible = false;
                break;
            }
            else {
                // If both above conditions are false
                // then we change the values of current
                // coordinates
                currentX = X[i];
                currentY = Y[i];
                currentTime = T[i];
            }
        }
     
        return IsPossible;
    }
     
    // Driver Code
    public static void main(String[] args)
    {
        int X[] = { 1, 1 };
        int Y[] = { 2, 1 };
        int T[] = { 3, 6 };
        int N = X.length;
        boolean ans = CheckItisPossible(X, Y, T, N);
     
        if (ans == true) {
            System.out.println("Yes");
        }
        else {
            System.out.println("No");
        }
    }
}
 
// This code is contributed by AnkThon


Python3
# python program for the above approach
 
# Function to check if it is possible
# to traverse all the points.
def CheckItisPossible(X, Y, T, N):
 
        # Make 3 variables to store given
        # ith values
    currentX = 0
    currentY = 0
    currentTime = 0
 
    # Also, make a bool variable to
    # check it is possible
    IsPossible = True
 
    # Now, iterate on all the coordinates
    for i in range(0, N):
 
                # check first condition
        if ((abs(X[i] - currentX)
             + abs(Y[i] - currentY))
                > (T[i] - currentTime)):
             # means thats not possible to
             # reach current coordinate
             # at Ithtime from previous coordinate
            IsPossible = False
            break
 
        elif (((abs(X[i] - currentX)
                + abs(Y[i] - currentY))
               % 2)
              > ((T[i] - currentTime) % 2)):
            # means thats not possible to
            # reach current coordinate
            # at Ithtime from previous coordinate
            IsPossible = False
            break
 
        else:
           # If both above conditions are false
           # then we change the values of current
           # coordinates
            currentX = X[i]
            currentY = Y[i]
            currentTime = T[i]
 
    return IsPossible
 
# Driver Code
if __name__ == "__main__":
 
    X = [1, 1]
    Y = [2, 1]
    T = [3, 6]
    N = len(X)
    ans = CheckItisPossible(X, Y, T, N)
 
    if (ans == True):
        print("Yes")
    else:
        print("No")
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
 
class GFG {
     
    // Function to check if it is possible
    // to traverse all the points.
    static bool CheckItisPossible(int []X, int []Y,
                        int []T, int N)
    {
     
        // Make 3 variables to store given
        // ith values
        int currentX = 0, currentY = 0,
            currentTime = 0;
     
        // Also, make a bool variable to
        // check it is possible
        bool IsPossible = true;
     
        // Now, iterate on all the coordinates
        for (int i = 0; i < N; i++) {
     
            // check first condition
            if ((Math.Abs(X[i] - currentX) +
                 Math.Abs(Y[i] - currentY)) > (T[i] - currentTime)) {
                 
                // means thats not possible to
                // reach current coordinate
                // at Ithtime from previous coordinate
                IsPossible = false;
                break;
            }
            else if (((Math.Abs(X[i] - currentX) +
                       Math.Abs(Y[i] - currentY)) % 2) > ((T[i] - currentTime) % 2)) {
                // means thats not possible to
                // reach current coordinate
                // at Ithtime from previous coordinate
                IsPossible = false;
                break;
            }
            else {
                // If both above conditions are false
                // then we change the values of current
                // coordinates
                currentX = X[i];
                currentY = Y[i];
                currentTime = T[i];
            }
        }
     
        return IsPossible;
    }
     
    // Driver Code
    public static void Main()
    {
        int []X = { 1, 1 };
        int []Y = { 2, 1 };
        int []T = { 3, 6 };
        int N = X.Length;
        bool ans = CheckItisPossible(X, Y, T, N);
     
        if (ans == true) {
            Console.Write("Yes");
        }
        else {
            Console.Write("No");
        }
    }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
Yes

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