Minimum number of Circular obstacles required to obstruct the path in a Grid
Last Updated :
03 Jan, 2023
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 in 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
#include <bits/stdc++.h>
using namespace std;
int solve( int n, int m, int obstacles,
double range[])
{
double val = min(n, m);
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 <= 0) {
return c;
}
else {
c++;
}
}
if (val > 0) {
return -1;
}
}
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
import java.util.*;
class GFG
{
static int solve( int n, int m, int obstacles,
double range[])
{
double val = Math.min(n, m);
Arrays.sort(range);
int c = 1 ;
for ( int i = obstacles - 1 ; i >= 0 ; i--)
{
range[i] = 2 * range[i];
val -= range[i];
if (val <= 0 )
{
return c;
}
else
{
c++;
}
}
if (val > 0 )
{
return - 1 ;
}
return 0 ;
}
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" );
}
}
|
C#
using System;
class GFG
{
static int solve( int n, int m, int obstacles,
double []range)
{
double val = Math.Min(n, m);
Array.Sort(range);
int c = 1;
for ( int i = obstacles - 1; i >= 0; i--)
{
range[i] = 2 * range[i];
val -= range[i];
if (val <= 0)
{
return c;
}
else
{
c++;
}
}
if (val > 0)
{
return -1;
}
return 0;
}
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));
}
}
|
Python3
def solve(n, m, obstacles, range ):
val = min (n, m)
range = sorted ( range )
c = 1
for i in range (obstacles - 1 , - 1 , - 1 ):
range [i] = 2 * range [i]
val - = range [i]
if (val < = 0 ):
return c
else :
c + = 1
if (val > 0 ):
return - 1
n = 4
m = 5
obstacles = 3
range = [ 1.0 , 1.25 , 1.15 ]
print (solve(n, m, obstacles, range ))
|
Javascript
<script>
function solve(n, m, obstacles, range)
{
var val = Math.min(n, m);
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 <= 0) {
return c;
}
else {
c++;
}
}
if (val > 0) {
return -1;
}
}
var n = 4, m = 5, obstacles = 3;
var range = [1.0, 1.25, 1.15];
document.write( solve(n, m, obstacles, range) + "<br>" );
</script>
|
Time Complexity: O(N * log(N)), where N is the number of given obstacles.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Share your thoughts in the comments
Please Login to comment...