Thursday, February 9, 2023
HomeSoftware DevelopmentTest if the participant can attain the goal co-ordinates after performing given...

Test if the participant can attain the goal co-ordinates after performing given strikes from origin


// C++ code to implement the strategy
#embody <bits/stdc++.h>
utilizing namespace std;

// dp desk initialized with - 1
int dp[501][20001];

// recusive perform to inform whether or not
// given coordinate (X, Y) is reachable
// or not from origin
int recur(int i, int j, int T, vector<int>& A)
{

    // base case
    if (i == A.measurement()) {
        // if T is feasible return 1
        if (j == T)
            return 1;
        else
            return 0;
    }

    // if reply for present state is already
    // calculated then simply return dp[i][j]
    // + 10000 is offset worth to alos embody
    // unfavourable numbers in states
    if (dp[i][j + 10000] != -1)
        return dp[i][j + 10000];

    int ans = 0;

    // calling recursive perform to maneuver Proper
    // in case of X coordinate or UP in case of
    // Y coordinate by distance A[i]
    ans = max(ans, recur(i + 1, j + A[i], T, A));

    // calling recursive perform to maneuver Left
    // in case of X coordinate or Down in case of
    // Y coordinate by distance A[i]
    ans = max(ans, recur(i + 1, j - A[i], T, A));

    // save and return dp worth
    return dp[i][j + 10000] = ans;
}

// Operate to test whether or not given
// Coordinates (X, Y) is reachable
// from origin
void isReachable(int A[], int N, int X, int Y)
{
    // creating two completely different arrays A1
    // for fixing X coordinate and A2
    // for fixing Y coordinate
    vector<int> A1, A2;

    // filling each the A1 and A2
    for (int i = 0; i < N; i++) {
        if (i % 2 == 0)
            A1.push_back(A[i]);
        else
            A2.push_back(A[i]);
    }

    // filling dp desk with -1
    memset(dp, -1, sizeof(dp));

    // to test whether or not Reaching X
    // cordinate is feasible by strikes
    // {A[1], A[3], A[5], A[7], .....}
    int isPossibleX = recur(0, 0, X, A1);

    // filling dp desk with -1
    memset(dp, -1, sizeof(dp));

    // to test whether or not Reaching Y
    // coordinate is feasible by strikes
    // {A[2], A[4], A[6], ........}
    int isPossibleY = recur(0, 0, Y, A2);

    // if reaching X and Y coordinates Each
    // are potential then solely reaching on (X, Y)
    // is feasible by participant
    if (isPossibleX and isPossibleY)
        cout << "Sure" << endl;
    else
        cout << "No" << endl;
}

// Driver Code
int primary()
{
    // Enter 1
    int A[] = { 2, 1, 3 };
    int N = sizeof(A) / sizeof(A[0]);
    int X = -1, Y = 1;

    // Operate Name
    isReachable(A, N, X, Y);

    // Enter 2
    int A1[] = { 1, 2, 3, 4 };
    int N1 = sizeof(A1) / sizeof(A1[0]);
    int X1 = 5, Y1 = 5;

    // Operate Name
    isReachable(A1, N1, X1, Y1);
    return 0;
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments