Minimum number of Circular obstacles required to obstruct the path in a Grid

Consider a grid of dimensions NxM and an array R consisting of available circular obstacles, the task is to find the minimum number of circular obstacles of given radiuses required to obstruct the path between source [0, 0] and destination [N-1, M-1]. If not possible print -1.
Note: The circular obstacles can overlap as shown in the image of example 1.

Examples:

Input: N = 4, M = 5, R[] = {1.0, 1.5, 1.25}
Output: 2

Input: N = 10, M = 12, R[] = {1.0, 1.25}
Output: -1

Approach:



  • Find whether to put the obstacles row-wise or column-wise.
  • Sort the radius in decreasing order.
  • Since the obstacles cover an entire circle with radius R[i], therefore for a straight line, it covers the diameter.
  • Decrease the val by 2 * Ri until it becomes zero using larger values in array R[].
  • After using all the obstacles, when val ≤ 0 return the count of obstacles used and if the val > 0 after using all the obstacles print -1.

Below is the implementation of the above approach.

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the minimum
// number of obstacles required
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the minimum
// number of obstacles required
int solve(int n, int m, int obstacles,
          double range[])
{
    // Find the minimum range required
    // to put obstacles
    double val = min(n, m);
  
    // Sorting the radius
    sort(range, range + obstacles);
  
    int c = 1;
    for (int i = obstacles - 1; i >= 0; i--) {
        range[i] = 2 * range[i];
        val -= range[i];
  
        // If val is less than zero
        // then we have find the number of
        // obstacles required
        if (val <= 0) {
            return c;
        }
        else {
            c++;
        }
    }
  
    if (val > 0) {
        return -1;
    }
}
  
// Driver function
int main()
{
    int n = 4, m = 5, obstacles = 3;
    double range[] = { 1.0, 1.25, 1.15 };
    cout << solve(n, m, obstacles, range) << "\n";
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the minimum
// number of obstacles required
import java.util.*;
  
class GFG
{
  
// Function to find the minimum
// number of obstacles required
static int solve(int n, int m, int obstacles,
                double range[])
{
    // Find the minimum range required
    // to put obstacles
    double val = Math.min(n, m);
  
    // Sorting the radius
    Arrays.sort(range);
  
    int c = 1;
    for (int i = obstacles - 1; i >= 0; i--)
    {
        range[i] = 2 * range[i];
        val -= range[i];
  
        // If val is less than zero
        // then we have find the number of
        // obstacles required
        if (val <= 0)
        {
            return c;
        }
        else
        {
            c++;
        }
    }
  
    if (val > 0
    {
        return -1;
    }
    return 0;
}
  
// Driver code
public static void main(String[] args)
{
    int n = 4, m = 5, obstacles = 3;
    double range[] = { 1.0, 1.25, 1.15 };
    System.out.print(solve(n, m, obstacles, range)+ "\n");
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the minimum
// number of obstacles required
using System;
  
class GFG
{
      
    // Function to find the minimum
    // number of obstacles required
    static int solve(int n, int m, int obstacles,
                    double []range)
    {
        // Find the minimum range required
        // to put obstacles
        double val = Math.Min(n, m);
      
        // Sorting the radius
        Array.Sort(range);
      
        int c = 1;
        for (int i = obstacles - 1; i >= 0; i--)
        {
            range[i] = 2 * range[i];
            val -= range[i];
      
            // If val is less than zero
            // then we have find the number of
            // obstacles required
            if (val <= 0)
            {
                return c;
            }
            else
            {
                c++;
            }
        }
      
        if (val > 0) 
        {
            return -1;
        }
        return 0;
    }
      
    // Driver code
    public static void Main()
    {
        int n = 4, m = 5, obstacles = 3;
        double []range = { 1.0, 1.25, 1.15 };
        Console.WriteLine(solve(n, m, obstacles, range));
    }
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the minimum
# number of obstacles required
  
# Function to find the minimum
# number of obstacles required
def solve(n, m, obstacles,rangee):
      
    # Find the minimum rangee required
    # to put obstacles
    val = min(n, m)
  
    # Sorting the radius
    rangee = sorted(rangee)
    c = 1
    for i in range(obstacles - 1, -1, -1):
        rangee[i] = 2 * rangee[i]
        val -= rangee[i]
          
        # If val is less than zero
        # then we have find the number of
        # obstacles required
        if (val <= 0):
            return c
        else:
            c += 1
  
    if (val > 0):
        return -1
  
# Driver code
n = 4
m = 5
obstacles = 3
rangee = [1.0, 1.25, 1.15]
print(solve(n, m, obstacles, rangee))
  
# This code is contributed by mohit kumar 29

chevron_right


Output:

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.