给定一个奇数N (代表大小为N x N的网格,该网格最初由硬币填充),任务是找到所有硬币移动到网格的任何单元格所需的最小移动数,以便在每一步中,网格中间的一些任意硬币可以移动到周围的八个单元中的任何一个。
例子:
Input: N = 3
Output: 8
Explanation:
There are a total of 9 cells in a 3 x 3 grid. On assuming that all the coins have to be brought to the central cell, 8 coins should be moved and the number of steps is 8.
Input: N = 5
Output: 40
方法:仅当所有硬币都移至网格中心时,才能获得最小步数。因此,该想法是将整个网格划分为多个层。
- 网格K的每一层都需要K移动才能到达中心。那是:
- 第1层的硬币需要迈出第一步才能移动到中心。
- 第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