📌  相关文章
📜  可以放置在N * M棋盘上的最大无攻击骑士

📅  最后修改于: 2021-04-22 08:33:51             🧑  作者: Mango

给定一个N * M的棋盘。任务是找到可放置在给定棋盘上的最大骑士数,以使任何骑士都不会攻击其他骑士。

例子

方法:众所周知,骑士可以通过两种方式进攻。这是他可以攻击的地方。

在这里,图片中的骑士是白色,只攻击黑色。因此。我们得出的结论是,骑士只能攻击其他颜色的人。
我们可以利用这一事实,并将其用于我们的目的。现在,我们知道骑士会攻击不同的颜色,因此我们可以使所有骑士保持相同的颜色,即全部为白色或全部为黑色。从而使可以放置的骑士数量最多。
要找到黑色或白色的数量,它仅是板上总色块的一半。

角落案例:

  • 如果只有一行或一列。然后,所有骑士都可以填满所有障碍物,因为骑士无法在同一行或同一列中进行攻击。
  • 如果只有两行或两列。然后,每两列(或行)将被骑士填充,并且每连续两列(或行)将保持为空。如图所示。

      下面是上述方法的实现:

      C++
      // C++ implementation of the approach
      #include 
      using namespace std;
        
      // Function to return the maximum number of
      // knights that can be placed on the given
      // chessboard such that no two
      // knights attack each other
      int max_knight(int n, int m)
      {
        
          // Check for corner case #1
          // If row or column is 1
          if (m == 1 || n == 1) {
        
              // If yes, then simply print total blocks
              // which will be the max of row or column
              return max(m, n);
          }
        
          // Check for corner case #2
          // If row or column is 2
          else if (m == 2 || n == 2) {
        
              // If yes, then simply calculate
              // consecutive 2 rows or columns
              int c = 0;
              c = (max(m, n) / 4) * 4;
        
              if (max(m, n) % 4 == 1) {
                  c += 2;
              }
              else if (max(m, n) % 4 > 1) {
                  c += 4;
              }
              return c;
          }
        
          // For general case, just print the
          // half of total blocks
          else {
              return (((m * n) + 1) / 2);
          }
      }
        
      // Driver code
      int main()
      {
          int n = 4, m = 5;
        
          cout << max_knight(n, m);
        
          return 0;
      }


      Java
      // Java implementation of the approach 
      import java.io.*;
        
      class GFG 
      {
            
      // Function to return the maximum number of 
      // knights that can be placed on the given 
      // chessboard such that no two 
      // knights attack each other 
      static int max_knight(int n, int m) 
      { 
        
          // Check for corner case #1 
          // If row or column is 1 
          if (m == 1 || n == 1) 
          { 
        
              // If yes, then simply print total blocks 
              // which will be the max of row or column 
              return Math.max(m, n); 
          } 
        
          // Check for corner case #2 
          // If row or column is 2 
          else if (m == 2 || n == 2) 
          { 
        
              // If yes, then simply calculate 
              // consecutive 2 rows or columns 
              int c = 0; 
              c = (Math.max(m, n) / 4) * 4; 
        
              if (Math.max(m, n) % 4 == 1)
              { 
                  c += 2; 
              } 
              else if (Math.max(m, n) % 4 > 1) 
              { 
                  c += 4; 
              } 
              return c; 
          } 
        
          // For general case, just print the 
          // half of total blocks 
          else 
          { 
              return (((m * n) + 1) / 2); 
          } 
      } 
        
      // Driver code 
      public static void main (String[] args) 
      {
          int n = 4, m = 5; 
          System.out.println (max_knight(n, m)); 
      }
      }
        
      // This code is contributed by ajit


      Python3
      # Python3 implementation of the approach 
        
      # Function to return the maximum number of 
      # knights that can be placed on the given 
      # chessboard such that no two 
      # knights attack each other 
      def max_knight(n, m) : 
        
          # Check for corner case #1 
          # If row or column is 1 
          if (m == 1 or n == 1) :
        
              # If yes, then simply print total blocks 
              # which will be the max of row or column 
              return max(m, n); 
        
          # Check for corner case #2 
          # If row or column is 2 
          elif (m == 2 or n == 2) :
        
              # If yes, then simply calculate 
              # consecutive 2 rows or columns 
              c = 0; 
              c = (max(m, n) // 4) * 4; 
        
              if (max(m, n) % 4 == 1) :
                  c += 2; 
                
              elif (max(m, n) % 4 > 1) :
                  c += 4; 
        
              return c; 
        
          # For general case, just print the 
          # half of total blocks 
          else :
              return (((m * n) + 1) // 2); 
        
      # Driver code 
      if __name__ == "__main__" : 
        
          n = 4; m = 5; 
        
          print(max_knight(n, m)); 
        
      # This code is contributed by AnkitRai01


      C#
      // C# implementation of the approach 
      using System;
        
      class GFG
      {
                
      // Function to return the maximum number of 
      // knights that can be placed on the given 
      // chessboard such that no two 
      // knights attack each other 
      static int max_knight(int n, int m) 
      { 
        
          // Check for corner case #1 
          // If row or column is 1 
          if (m == 1 || n == 1) 
          { 
        
              // If yes, then simply print total blocks 
              // which will be the max of row or column 
              return Math.Max(m, n); 
          } 
        
          // Check for corner case #2 
          // If row or column is 2 
          else if (m == 2 || n == 2) 
          { 
        
              // If yes, then simply calculate 
              // consecutive 2 rows or columns 
              int c = 0; 
              c = (Math.Max(m, n) / 4) * 4; 
        
              if (Math.Max(m, n) % 4 == 1)
              { 
                  c += 2; 
              } 
              else if (Math.Max(m, n) % 4 > 1) 
              { 
                  c += 4; 
              } 
              return c; 
          } 
        
          // For general case, just print the 
          // half of total blocks 
          else
          { 
              return (((m * n) + 1) / 2); 
          } 
      } 
        
      // Driver code 
      static public void Main ()
      {
          int n = 4, m = 5; 
          Console.Write(max_knight(n, m)); 
      }
      }
        
      // This code is contributed by Tushil.


      输出:
      10