📌  相关文章
📜  为矩形公园浇水所需的最少洒水器

📅  最后修改于: 2021-09-06 05:34:08             🧑  作者: Mango

给定N * M 个矩形公园,有N行和M列,公园的每个单元格都是一个单位面积的正方形,单元格之间的边界称为六边形,可以在六边形中间放置一个洒水器。任务是找到为整个公园浇水所需的最少洒水器数量。

例子:

方法:

  1. 在进行一些观察之后,可以指出一件事,即每列需要N 个喷水器,因为我们可以将它们放置在两列之间。
  2. 如果M是偶数,那么显然需要N* (M / 2) 个喷头。
  3. 但是,如果M是奇数,则可以使用偶数列公式计算M – 1列的解决方案,并且对于最后一列添加( N + 1) / 2喷头浇灌最后一列,无论 N 是奇数还是偶数。

      这是上述方法的实现:

      C++
      // C++ program to find the
      // minimum number sprinklers
      // reqired to water the park.
        
      #include 
      using namespace std;
      typedef long long int ll;
        
      // Function to find the
      // minimum number sprinklers
      // required to water the park.
      void solve(int N, int M)
      {
        
          // General requirements of
          // sprinklers
          ll ans = (N) * (M / 2);
        
          // if M is odd then add
          // one additional sprinklers
          if (M % 2 == 1) {
              ans += (N + 1) / 2;
          }
        
          cout << ans << endl;
      }
        
      // Driver code
      int main()
      {
          int N, M;
          N = 5;
          M = 3;
          solve(N, M);
      }


      Java
      // Java program to find minimum 
      // number sprinklers required 
      // to cover the park
      class GFG{ 
            
      // Function to find the minimum 
      // number sprinklers reqired 
      // to water the park. 
      public static int solve(int n, int m)
      {
            
          // General requirements of sprinklers
          int ans = n * (m / 2);
                
          // If M is odd then add one
          // additional sprinklers 
          if (m % 2 == 1)
          {
              ans += (n + 1) / 2;
          } 
          return ans;
      }
        
      // Driver code
      public static void main(String args[]) 
      { 
          int N = 5;
          int M = 3;
            
          System.out.println(solve(N, M)); 
      } 
      } 
        
      // This code is contributed by grand_master


      Python3
      # Python3 program to find the 
      # minimum number sprinklers 
      # required to water the park. 
        
      # Function to find the 
      # minimum number sprinklers 
      # reqired to water the park. 
      def solve(N, M) :
            
          # General requirements of 
          # sprinklers 
          ans = int((N) * int(M / 2))
        
          # if M is odd then add 
          # one additional sprinklers 
          if (M % 2 == 1):
              ans += int((N + 1) / 2)
        
          print(ans)
        
      # Driver code 
      N = 5
      M = 3
      solve(N, M)
        
      # This code is contributed by yatinagg


      C#
      // C# program to find minimum 
      // number sprinklers required 
      // to cover the park
      using System;
        
      class GFG{ 
            
      // Function to find the minimum 
      // number sprinklers reqired 
      // to water the park. 
      public static int solve(int n, int m)
      {
            
          // General requirements of sprinklers
          int ans = n * (m / 2);
                
          // If M is odd then add one
          // additional sprinklers 
          if (m % 2 == 1)
          {
              ans += (n + 1) / 2;
          } 
          return ans;
      }
        
      // Driver code
      public static void Main(String []args) 
      { 
          int N = 5;
          int M = 3;
            
          Console.WriteLine(solve(N, M)); 
      } 
      } 
        
      // This code is contributed by 29AjayKumar


      输出:
      8
      

      时间复杂度: O(1)
      辅助空间: O(1)

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