📜  阻塞网格中的路径所需的最小圆形障碍物数

📅  最后修改于: 2021-04-26 07:53:48             🧑  作者: 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


输出:
2