Número mínimo de obstáculos circulares requeridos para obstruir el camino en una cuadrícula

Considere una cuadrícula de dimensiones NxM y una array R que consta de obstáculos circulares disponibles, la tarea es encontrar el número mínimo de obstáculos circulares de radios dados necesarios para obstruir el camino entre el origen [0, 0] y el destino [N-1, M -1] . Si no es posible, imprima -1. 
Nota: Los obstáculos circulares pueden superponerse como se muestra en la imagen del ejemplo 1.

Ejemplos: 

Entrada: N = 4, M = 5, R[] = {1,0, 1,5, 1,25} 
Salida:
 

Entrada: N = 10, M = 12, R[] = {1,0, 1,25} 
Salida: -1  

Acercarse: 

  • Determina si colocar los obstáculos en filas o en columnas.
  • Ordena el radio en orden decreciente.
  • Dado que los obstáculos cubren un círculo completo con radio R[i], por lo tanto, para una línea recta, cubre el diámetro.
  • Disminuya el valor en 2 * Ri hasta que se convierta en cero usando valores más grandes en la array R[].
  • Después de usar todos los obstáculos, cuando val <= 0 devuelve el conteo de obstáculos usados, y si val > 0 después de usar todos los obstáculos, imprime -1 .

A continuación se muestra la implementación del enfoque anterior. 

CPP

// 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;
}

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

<script>
  
// Javascript program to find the minimum
// number of obstacles required
// Function to find the minimum
// number of obstacles required
function solve(n, m, obstacles, range)
{
    // Find the minimum range required
    // to put obstacles
    var val = Math.min(n, m);
 
    // Sorting the radius
    range.sort((a,b)=>a-b)
 
    var c = 1;
    for (var 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
var n = 4, m = 5, obstacles = 3;
var range = [1.0, 1.25, 1.15];
document.write( solve(n, m, obstacles, range) + "<br>");
 
 
</script>
Producción: 

2

 

Publicación traducida automáticamente

Artículo escrito por Subash23 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *