Minimum number of Circular obstacles required to obstruct the path in a Grid | C ++ program to find the minimum number of obstacles required ; Function to find the minimum number of obstacles required ; Find the minimum range required to put obstacles ; Sorting the radius ; If val is less than zero then we have find the number of obstacles required ; Driver function
#include <bits/stdc++.h> NEW_LINE 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 ) << " STRNEWLINE " ; return 0 ; }