📌  相关文章
📜  计算网格中的单元格,通过 K 次垂直或水平跳跃可以达到最大单元格数

📅  最后修改于: 2022-05-13 01:56:05.916000             🧑  作者: Mango

计算网格中的单元格,通过 K 次垂直或水平跳跃可以达到最大单元格数

给定一个维度为N*M的矩阵mat[][]和一个正整数K ,任务是找出网格中的单元格数量,从这些单元格中可以通过垂直或水平方向的K次跳跃达到最大单元格。

例子:

方法:给定的问题可以通过根据以下观察分别计算行和列的可能单元格的数量来解决:

  • 考虑任何行,比如i使得i <= K ,那么使用K的跳转可以到达i的行数是(N – i) / K + 1
  • 如果行说ii > K ,那么这些单元格连接到X <= K的某个行X ,因此它们已经被考虑在内。

因此,根据上述观察,我们的想法是找到与变量中的最大行数相关的行数,比如count_R 。同样,对于列,找到连接到变量中最大列的列数,比如count_C

现在,网格中的单元格数由 ( count_R )*(count_C)给出。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the number of cells
// in the grid such that maximum cell is
// reachable with a jump of K
long long countCells(int n, int m, int s)
{
    // Maximum reachable rows
    // from the current row
    int mx1 = -1;
 
    // Stores the count of cell that are
    // reachable from the current row
    int cont1 = 0;
 
    for (int i = 0; i < s && i < n; ++i) {
 
        // Count of reachable rows
        int aux = (n - (i + 1)) / s + 1;
 
        // Update the maximum value
        if (aux > mx1) {
            mx1 = cont1 = aux;
        }
 
        // Add it to the count
        else if (aux == mx1)
            cont1 += aux;
    }
 
    // Maximum reachable columns from
    // the current column
    int mx2 = -1;
 
    // Stores the count of cell that are
    // reachable from the current column
    int cont2 = 0;
 
    for (int i = 0; i < s && i < m; ++i) {
 
        // Count of rechable columns
        int aux = (m - (i + 1)) / s + 1;
 
        // Update the maximum value
        if (aux > mx2)
            mx2 = cont2 = aux;
 
        // Add it to the count
        else if (aux == mx2)
            cont2 += aux;
    }
 
    // Return the total count of cells
    return (long long)(cont1 * cont2);
}
 
// Driver Code
int main()
{
    int N = 5, M = 5, K = 2;
    cout << countCells(N, M, K);
 
    return 0;
}


Java
// Java program for the above approach
class GFG
{
 
  // Function to count the number of cells
  // in the grid such that maximum cell is
  // reachable with a jump of K
  public static long countCells(int n, int m, int s)
  {
     
    // Maximum reachable rows
    // from the current row
    int mx1 = -1;
 
    // Stores the count of cell that are
    // reachable from the current row
    int cont1 = 0;
 
    for (int i = 0; i < s && i < n; ++i) {
 
      // Count of reachable rows
      int aux = (n - (i + 1)) / s + 1;
 
      // Update the maximum value
      if (aux > mx1) {
        mx1 = cont1 = aux;
      }
 
      // Add it to the count
      else if (aux == mx1)
        cont1 += aux;
    }
 
    // Maximum reachable columns from
    // the current column
    int mx2 = -1;
 
    // Stores the count of cell that are
    // reachable from the current column
    int cont2 = 0;
 
    for (int i = 0; i < s && i < m; ++i) {
 
      // Count of rechable columns
      int aux = (m - (i + 1)) / s + 1;
 
      // Update the maximum value
      if (aux > mx2)
        mx2 = cont2 = aux;
 
      // Add it to the count
      else if (aux == mx2)
        cont2 += aux;
    }
 
    // Return the total count of cells
    return (long) (cont1 * cont2);
  }
 
  // Driver Code
  public static void main(String args[]) {
    int N = 5, M = 5, K = 2;
    System.out.println(countCells(N, M, K));
 
  }
 
}
 
// This code is contributed by gfgking.


Python3
# Python 3 program for the above approach
 
# Function to count the number of cells
# in the grid such that maximum cell is
# reachable with a jump of K
def countCells(n, m, s):
    # Maximum reachable rows
    # from the current row
    mx1 = -1
 
    # Stores the count of cell that are
    # reachable from the current row
    cont1 = 0
    i = 0
    while(i < s and i < n):
        # Count of reachable rows
        aux = (n - (i + 1)) // s + 1
 
        # Update the maximum value
        if (aux > mx1):
            mx1 = cont1 = aux
 
        # Add it to the count
        elif(aux == mx1):
            cont1 += aux
        i += 1
 
    # Maximum reachable columns from
    # the current column
    mx2 = -1
 
    # Stores the count of cell that are
    # reachable from the current column
    cont2 = 0
     
    i = 0
    while(i < s and i < m):
        # Count of rechable columns
        aux = (m - (i + 1)) // s + 1
 
        # Update the maximum value
        if (aux > mx2):
            mx2 = cont2 = aux
 
        # Add it to the count
        elif(aux == mx2):
            cont2 += aux
        i += 1
 
    # Return the total count of cells
    return cont1 * cont2
 
# Driver Code
if __name__ == '__main__':
    N = 5
    M = 5
    K = 2
    print(countCells(N, M, K))
     
    # This code is contributed by ipg2016107


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to count the number of cells
// in the grid such that maximum cell is
// reachable with a jump of K
static int countCells(int n, int m, int s)
{
   
    // Maximum reachable rows
    // from the current row
    int mx1 = -1;
 
    // Stores the count of cell that are
    // reachable from the current row
    int cont1 = 0;
 
    for (int i = 0; i < s && i < n; ++i) {
 
        // Count of reachable rows
        int aux = (n - (i + 1)) / s + 1;
 
        // Update the maximum value
        if (aux > mx1) {
            mx1 = cont1 = aux;
        }
 
        // Add it to the count
        else if (aux == mx1)
            cont1 += aux;
    }
 
    // Maximum reachable columns from
    // the current column
    int mx2 = -1;
 
    // Stores the count of cell that are
    // reachable from the current column
    int cont2 = 0;
 
    for (int i = 0; i < s && i < m; ++i) {
 
        // Count of rechable columns
        int aux = (m - (i + 1)) / s + 1;
 
        // Update the maximum value
        if (aux > mx2)
            mx2 = cont2 = aux;
 
        // Add it to the count
        else if (aux == mx2)
            cont2 += aux;
    }
 
    // Return the total count of cells
    return (cont1 * cont2);
}
 
// Driver Code
public static void Main()
{
    int N = 5, M = 5, K = 2;
    Console.Write(countCells(N, M, K));
}
}
 
// This code is contributed by ipg2016107.


Javascript


输出:
9

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