Open In App

Check if is possible to catch the player A before it reaches to bottom right cell

Last Updated : 06 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Consider a matrix with N rows and M columns. Initially, two players A and B are standing in the cell (X, Y) and (A, B) respectively. Then the task is to output YES, If A can reach the bottom-right cell before the interruption of Player B in his path else NO. A makes the first move of the game.

  • Player A can move either right or down. It is essential to move for this player and wants to reach cell (N, M).
  • Player B can move either right, down, or right + down (diagonally bottom-right) and it is not essential to move for this player in his turn. Formally, Player B can wait in its cell or make its move in any direction.

Note: Initially no player will be in cell (N, M), Both players will play Optimally and have knowledge of each other’s position at any moment of time.

Examples:

Input: N = 5, M = 4, X = 1, Y = 2, A = 4, B = 2
Output: NO
Explanation: Both are in the same column (col = 2) initially.

  • Player A: A can’t go one step down because it will make him closer to player B, which will interrupt and catch him. Therefore, he moves one step right. Then A’s current position is (1, 3).
  • Player B: B will make a move optimally move one step right and will place himself in the cell (4, 3).
  • Player A: It will not be optimal to make a move downwards, because eventually A will be closer to Player B. Therefore, A moves one step right, i.e., now in (1, 4).
  • Player B: Makes one step right and places self in the cell (4, 4).

Now, A can only make down moves. Both are in the same column. So, B will wait in his current cell (4, 4), and A has to make moves and will catch B after two moves.

Input: N = 4, M = 3, X = 3, Y = 3, A = 4, B = 2
Output: YES
Explanation:
Player A: Moves one step downwards and places himself in the cell (4, 3). Thus, he reached his desired cell without interruption of B.

Approach: To solve the problem follow the below observations:

It is an observation-based problem and can be solved using Greedy approach. As Player B can move one step diagonally as well, which consists of two steps combine into one so we will take min. In this we minus the min of both the steps as it can be moved in single step for example (1, 2) takes 3 steps in case of player A but for player B, he can move (1,1) in single step and then from there (1,2) which is 2 steps, so it will be total moves – min (n-a, m-b).

A Brief Explanation of formulas used:

  • The formula (N + M – X – Y) calculates the minimum number of steps the A needs to reach cell (N, M) from his current position (X, Y). Since the A can only move right or down, each step will either increase his row or column by 1. Therefore, the total number of steps is the difference in rows plus the difference in columns.
  • The formula (N + M – A – B – D) calculates the minimum number of steps the B needs to reach cell (N, M) from his current position (A, B). B can move right, down, or diagonally (right + down). The variable D is the minimum of (N – A) and (M – B), representing the number of diagonal steps B can take. After taking these diagonal steps, B will either be on row N or column M, and can reach cell (N, M) by moving right or down. Therefore, the total number of steps is D plus the remaining difference in rows and columns.

Illustration:

Grid size: N = 6, M = 6

  • Initial position of A: X = 2, Y = 3
  • Initial position of B: A = 3, B = 3

Let’s consider a test case:

    • Using the formulas:
      • Steps for A: (N + M – X – Y) = 6 + 6 – 2 – 3 = 7
      • Steps for B: (N + M – A – B – D) = 6 + 6 – 3 – 3 – min(6-3, 6-3) = 6 + 6 – 3 – 3 – 3 = 3

    Since the B requires fewer steps than A to reach cell (5, 4), the output is NO, indicating that A cannot escape.

    In this case, B requires fewer steps than A to reach cell (6, 6), so the output is NO, indicating that A cannot escape.

    This example demonstrates that A’s chances of escape can be significantly affected by the initial positions. A is closer to the B and therefore has less chance of escaping. It also shows that the B’s ability to move diagonally can be a significant advantage. In this case, B can reach cell (6,6) in just three steps by moving diagonally, while A requires seven steps because he can only move right or down.

    Steps were taken to solve the problem:

    • Create a variable let say D and initialize it equal to Math.min(N-A, M-B)
    • Create a variable J and initialize it equal to N+M-A-B-D.
    • Create a variable K and initialize it equal to N+M-X-Y.
    • If (J < K), then output NO else YES.

    Implementation of above approach:

    C++




    #include <iostream>
    using namespace std;
     
    // Function for checking if it is possible or not
    void Check(int N, int M, int X, int Y, int A, int B) {
        // Applying the logic discussed in the approach section
        int D = min(N - A, M - B);
        int stepp = N + M - A - B - D;
        int stept = N + M - X - Y;
        if (stepp < stept) {
            cout << "NO" << endl;
        } else {
            cout << "YES" << endl;
        }
    }
     
    int main() {
        int N = 5, M = 4;
        int X = 1, Y = 2;
        int A = 4, B = 2;
     
        // Function call
        Check(N, M, X, Y, A, B);
     
        return 0;
    }

    
    

    Java




    // Java code to implement the approach
     
    import java.io.*;
    import java.lang.*;
    import java.util.*;
     
    class Main {
     
        // Driver Function
        public static void main(String[] args)
            throws java.lang.Exception
        {
            // Inputs
            int N = 5, M = 4;
            int X = 1, Y = 2;
            int A = 4, B = 2;
     
            // Function call
            Check(N, M, X, Y, A, B);
        }
     
        // Method for checking that if
        // it is possible or not
        public static void Check(int N, int M, int X, int Y,
                                 int A, int B)
        {
     
            // Applying discussed logic
            // in approach section
            int D = Math.min(N - A, M - B);
            int stepp = N + M - A - B - D;
            int stept = N + M - X - Y;
            if (stepp < stept) {
                System.out.println("NO");
            }
            else {
                System.out.println("YES");
            }
        }
    }

    
    

    Python3




    # Function for checking if it is possible or not
    def check(N, M, X, Y, A, B):
        # Applying the logic discussed in the approach section
        D = min(N - A, M - B)
        stepp = N + M - A - B - D
        stept = N + M - X - Y
        if stepp < stept:
            print("NO")
        else:
            print("YES")
     
    if __name__ == "__main__":
        N, M = 5, 4
        X, Y = 1, 2
        A, B = 4, 2
     
        # Function call
        check(N, M, X, Y, A, B)
     
    # This code is contributed by shivamgupta0987654321

    
    

    C#




    using System;
     
    class GFG
    {
        // Driver Function
        public static void Main(string[] args)
        {
            // Inputs
            int N = 5, M = 4;
            int X = 1, Y = 2;
            int A = 4, B = 2;
     
            // Function call
            Check(N, M, X, Y, A, B);
        }
     
        // Method for checking that if
        // it is possible or not
        public static void Check(int N, int M, int X, int Y, int A, int B)
        {
            // Applying discussed logic
            // in approach section
            int D = Math.Min(N - A, M - B);
            int stepp = N + M - A - B - D;
            int stept = N + M - X - Y;
             
            if (stepp < stept)
            {
                Console.WriteLine("NO");
            }
            else
            {
                Console.WriteLine("YES");
            }
        }
    }

    
    

    Javascript




    // JavaScript code for the above approach
     
    // Function for checking if it is possible or not
    function Check(N, M, X, Y, A, B) {
        // Applying the logic discussed in the approach section
        let D = Math.min(N - A, M - B);
        let stepp = N + M - A - B - D;
        let stept = N + M - X - Y;
        if (stepp < stept) {
            console.log("NO");
        } else {
            console.log("YES");
        }
    }
     
    // Driver code
    let N = 5, M = 4;
    let X = 1, Y = 2;
    let A = 4, B = 2;
     
    // Function call
    Check(N, M, X, Y, A, B);
     
    // This code is contributed by Abhinav Mahajan (abhinav_m22)

    
    

    Output

    NO
    
    
    
    
    
    
    
    

    Time Complexity: O(1)
    Auxiliary Space: O(1)



    Like Article
    Suggest improvement
    Share your thoughts in the comments

    Similar Reads