📌  相关文章
📜  将每个单元格的硬币移动到矩阵的任何一个单元格所需的最小移动

📅  最后修改于: 2021-09-06 11:31:00             🧑  作者: Mango

给定一个奇数N代表一个大小为N x N的网格,最初由硬币填充,任务是找到所有硬币移动到网格的任何单元格所需的最小移动次数,使得在每一步中,网格中间的一些任意硬币可以移动到周围八个单元格中的任何一个。

例子:

方法:只有当所有的硬币都移动到网格的中心时,才能获得最少的步数。因此,想法是将整个网格划分为多个层。

  • 网格 K 的每一层都需要 K 次移动才能到达中心。那是:
    1. 第 1 层的硬币一步移动到中心。
    2. 第 2 层的硬币需要两个步骤才能移动到中心,依此类推。
  • 例如,让 N = 5。那么,网格划分为如下层:
  • 在上图中,标记为红色的硬币位于第 1 层,标记为蓝色的硬币位于第 2 层。
  • 类似地,对于大小为 N x N 的网格,我们可以将硬币分成 N//2 层。
  • 在每一层 K 中,存在的硬币数量为 (8 * K)。并且,所需的步数为 K。因此,遍历所有层并找到总步数为8 * K 2

下面是上述方法的实现:

C++
// C++ program to find the minimum number
// of moves taken to move the element of
// each cell to any one cell of the
// square matrix of odd length
#include 
using namespace std;
 
// Function to find the minimum number
// of moves taken to move the element 
// of each cell to any one cell of the
// square matrix of odd length
int calculateMoves(int n)
{
     
    // Initializing count to 0
    int count = 0;
 
    // Number of layers that are
    // around the centre element
    int layers = n / 2;
 
    // Iterating over ranger of layers
    for(int k = 1; k < layers + 1; k++)
    {
 
       // Increase the value of count 
       // by 8 * k * k
       count += 8 * k * k;
    }
    return count;
}
 
// Driver code
int main()
{
    int N = 5;
     
    cout << calculateMoves(N);
}
 
// This code is contributed by coder001


Java
// Java program to find the minimum number
// of moves taken to move the element of
// each cell to any one cell of the
// square matrix of odd length
class GFG{
     
// Function to find the minimum number
// of moves taken to move the element
// of each cell to any one cell of the
// square matrix of odd length
public static int calculateMoves(int n)
{
         
    // Initializing count to 0
    int count = 0;
     
    // Number of layers that are
    // around the centre element
    int layers = n / 2;
     
    // Iterating over ranger of layers
    for(int k = 1; k < layers + 1; k++)
    {
         
       // Increase the value of count
       // by 8 * k * k
       count += 8 * k * k;
    }
    return count;
}
 
// Driver code   
public static void main(String[] args)
{
    int N = 5;
     
    System.out.println(calculateMoves(N));
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program to find the minimum number
# of moves taken to move the element of
# each cell to any one cell of the
# square matrix of odd length
 
# Function to find the minimum number
# of moves taken to move the element of
# each cell to any one cell of the
# square matrix of odd length
def calculateMoves(n):
 
    # Initializing count to 0
    count = 0
 
    # Number of layers that are
    # around the centre element
    layers = n//2
 
    # Iterating over ranger of layers
    for k in range(1, layers + 1):
 
        # Increase the value of count by
        # 8 * k * k
        count+= 8 * k*k
 
    return count
 
# Driver code
if __name__ == "__main__":
 
    N = 5
  
    print(calculateMoves(N))


C#
// C# program to find the minimum number
// of moves taken to move the element of
// each cell to any one cell of the
// square matrix of odd length
using System;
class GFG{
     
// Function to find the minimum number
// of moves taken to move the element
// of each cell to any one cell of the
// square matrix of odd length
public static int calculateMoves(int n)
{
         
    // Initializing count to 0
    int count = 0;
     
    // Number of layers that are
    // around the centre element
    int layers = n / 2;
     
    // Iterating over ranger of layers
    for(int k = 1; k < layers + 1; k++)
    {
         
        // Increase the value of count
        // by 8 * k * k
        count += 8 * k * k;
    }
    return count;
}
 
// Driver code
public static void Main()
{
    int N = 5;
     
    Console.Write(calculateMoves(N));
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
40

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