将所有 1 移动到给定方阵的单个单元格的最小步骤
给定一个奇数正整数N ,它表示填充有 1 的N*N方阵的大小,任务是找到将所有 1 移动到矩阵的单个单元格的最小步数,其中,在一个步骤中,任何1 可以移动到与其水平、垂直或对角相邻的任何单元格。
例子:
Input: N = 3
Output: 8
Explanation: All the 8 1s present in the boundary can be brought to the centre of the matrix in one step.
So total required steps = 8
Input: N = 367
Output: 16476832
Explanation: The minimum number of steps required to put all the cookies in exactly one block of the tray is 16476832.
方法:可以根据以下观察解决问题。
To minimize the number of steps, all 1s should be moved to the centre of the matrix.
Any cell of the matrix is a part of ith zone if it’s distance from the centre is i (horizontally, vertically or diagonally).
All the elements from ith block can be moved to centre in i steps.
See the image below for a better idea about zones. (Here zones of a 7*7 matrix are shown)
按照下面提到的步骤使用上述观察解决问题:
- 可能的区域总数X = N/2 。
- 第 i 个区域中的单元格总数为2 * i * 4 = 8*i 。
- 因此,将第 i 个区域的所有 1 移动到中心所需的总步数为8*i*i 。
- 运行从i = 1 到 X的循环并且:
- 使用上述公式计算第 i 个区域的总步数。
- 将其与sum 相加。
- 返回最终总和作为答案。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum number
// of steps required to put
// all the cookies in exactly one cell
int minSteps(int N)
{
// Stores the minimum number of steps
int res = 0;
// Loop to iterate over
// all the zones present in the matrix
for (int i = 1; i <= N / 2; ++i) {
// Steps to move all 1s of ith zone
// to the centre of the matrix
res += 8 * i * i;
}
// Return the minimum steps
return res;
}
// Driver Code
int main()
{
// Given input
int N = 7;
// Function Call
cout << minSteps(N);
return 0;
}
Java
// JAVA program for the above approach
import java.util.*;
class GFG
{
// Function to find the minimum number
// of steps required to put
// all the cookies in exactly one cell
public static int minSteps(int N)
{
// Stores the minimum number of steps
int res = 0;
// Loop to iterate over
// all the zones present in the matrix
for (int i = 1; i <= N / 2; ++i) {
// Steps to move all 1s of ith zone
// to the centre of the matrix
res += 8 * i * i;
}
// Return the minimum steps
return res;
}
// Driver Code
public static void main(String[] args)
{
// Given input
int N = 7;
// Function Call
System.out.print(minSteps(N));
}
}
// This code is contributed by Taranpreet
Python
# Python program for the above approach
# Function to find the minimum number
# of steps required to put
# all the cookies in exactly one cell
def minSteps(N):
# Stores the minimum number of steps
res = 0
# Loop to iterate over
# all the zones present in the matrix
i = 1
while(i <= N//2):
# Steps to move all 1s of ith zone
# to the centre of the matrix
res += 8 * i * i
i += 1
# Return the minimum steps
return res
# Driver Code
# Given input
N = 7
# Function Call
print(minSteps(N))
# This code is contributed by Samim Hossain Mondal.
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the minimum number
// of steps required to put
// all the cookies in exactly one cell
static int minSteps(int N)
{
// Stores the minimum number of steps
int res = 0;
// Loop to iterate over
// all the zones present in the matrix
for (int i = 1; i <= N / 2; ++i) {
// Steps to move all 1s of ith zone
// to the centre of the matrix
res += 8 * i * i;
}
// Return the minimum steps
return res;
}
// Driver Code
public static void Main()
{
// Given input
int N = 7;
// Function Call
Console.Write(minSteps(N));
}
}
// This code is contributed by Samim Hossain Mondal
Javascript
112
时间复杂度: O(X),其中 X 是矩阵中存在的区域数
辅助空间: O(1)