📜  阻挡网格中路径所需的圆形障碍物的最少数量

📅  最后修改于: 2021-09-06 06:17:15             🧑  作者: Mango

考虑一个维度为NxM的网格和一个由可用圆形障碍物组成的数组R ,任务是找到阻挡源[0, 0]和目的地[N-1, M ]之间路径所需的给定半径的圆形障碍物的最小数量-1] 。如果不可能,则打印 -1。
注意:圆形障碍物可以重叠,如示例 1 中的图像所示。

例子:

方法:

  • 找出是按行还是按列放置障碍物。
  • 按降序对半径进行排序。
  • 由于障碍物覆盖了半径为 R[i] 的整个圆,因此,对于直线,它覆盖了直径。
  • 使用数组 R[] 中的较大值将 val 减少2 * Ri,直到它变为零。
  • 使用所有障碍物后,当val <= 0 时返回使用的障碍物计数,如果使用所有障碍物后val > 0 ,则打印-1

下面是上述方法的实现。

CPP
// C++ program to find the minimum
// number of obstacles required
 
#include 
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;
}


Java
// 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


C#
// 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


Python3
# 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


Javascript


输出:
2

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live