使用所有 2×2 子矩阵的总和将给定矩阵减小 1
给定一个大小为N*N的方阵M[][] ,任务是使用以下操作将该矩阵缩减为大小为(N-1) * (N-1)的矩阵:
- 从N*N矩阵中取出所有2×2子矩阵,
- 将每个子矩阵的和插入到相应位置的大小为N-1 * N-1的结果矩阵L[ ][ ]中。
例子:
Input: M[][] = {{ 1, 2, 3, 4 },
{ 4, 5, 6, 7 },
{ 7, 8, 9, 10 },
{ 10, 11, 12, 13 }}
Output: L[][] = {{12, 16, 20},
{24, 28, 32},
{36, 40, 44}}
Explanation:
Input: M[][] = {{ 3, 6, 3},
{ 4, 7, 6},
{ 2, 1, 9}}
Output: L[][] = {{20, 22},
{14, 23}}
方法:可以通过模拟定义操作的过程来解决任务。请按照以下步骤解决问题:
- 声明空数组l[]来存储缩减矩阵的元素
- 声明c = s = 0 , s 用于获取总和, c用于更改列, p[ ]用于在每次迭代后存储总和。
- 现在 s 是, s = M[ i ][ c ] + M[ i ][ c + 1 ] + M[ i + 1 ][ c ] + M[ i+1 ][ c+1 ]
- 它将存储 4 个元素,例如索引 [00, 01, 10, 11] 处的元素,然后是 [01, 02, 11, 12] 类似地从第 2 行和第 3 行开始
- 将第一个总和存储在数组p[]中并递增c并再次分配s=0 。
- 重复步骤 3,因此我们将得到数组p [ ] 。
- 将 p 存储在矩阵l [ ]中并重复步骤 2。
以下是该方法的实现:
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to find the desired matrix
vector > checkMat(vector >& M,
int N)
{
// Empty array to store 2x2 matrix element
vector > l;
for (int i = 0; i < N - 1; i++) {
// Initialization
int c = 0, s = 0;
vector p;
for (int j = 0; j < N - 1; j++) {
// Taking sum of 4 elements
s += M[i][j] + M[i][j+1];
s += M[i + 1][j] + M[i + 1][j+1];
// Appending sum in array p
p.push_back(s);
// Assigning s to 0, to take sum
// of another 4 element
s = 0;
// Increment c
c++;
}
// Append array p[] to l
l.push_back(p);
}
return l;
}
// Driver code
int main()
{
// Original matrix
vector > M = { { 1, 2, 3, 4 },
{ 4, 5, 6, 7 },
{ 7, 8, 9, 10 },
{ 10, 11, 12, 13 } };
// Size of matrix
int N = M.size();
// Calling function
vector > res = checkMat(M, N);
// Printing the result
for (int i = 0; i < res.size(); i++) {
for (int j = 0; j < res[i].size(); j++) {
cout << res[i][j] << " ";
}
cout << "\n";
}
}
// This code is contributed by Taranpreet and improved by prophet1999
Java
// Java code for the above approach
import java.util.*;
class GFG
{
// Function to find the desired matrix
static ArrayList >
checkMat(int[][] M, int N)
{
// Empty array to store 2x2 matrix element
ArrayList > l
= new ArrayList >(2);
for (int i = 0; i < N - 1; i++) {
// Initialization
ArrayList p = new ArrayList();
int c = 0, s = 0;
for (int j = 0; j < N - 1; j++) {
// Taking sum of 4 elements
s += M[i][j] + M[i][j+1];
s += M[i + 1][j] + M[i + 1][j+1];
// Appending sum in array p
p.add(s);
// Assigning s to 0, to take sum
// of another 4 element
s = 0;
// Increment c
c++;
}
// Append array p[] to l
l.add(p);
}
return l;
}
public static void main(String[] args)
{
// Original matrix
int[][] M = { { 1, 2, 3, 4 },
{ 4, 5, 6, 7 },
{ 7, 8, 9, 10 },
{ 10, 11, 12, 13 } };
// Size of matrix
int N = M.length;
// Calling function
ArrayList > res = checkMat(M, N);
// Printing the result
for (int i = 0; i < res.size(); i++) {
for (int j = 0; j < res.get(i).size(); j++) {
System.out.print(res.get(i).get(j) + " ");
}
System.out.println();
}
}
}
// This code is contributed by Potta Lokesh and improved by prophet1999
Python3
# Python program for the above approach
# Function to find the desired matrix
def checkMat(M, N):
# Empty array to store 2x2 matrix element
l = []
for i in range(N-1):
# Initialization
c = s = 0
p = []
for j in range(N-1):
# Taking sum of 4 elements
s += M[i][j]+M[i][j+1]
s += M[i + 1][j]+M[i + 1][j+1]
# Appending sum in array p
p.append(s)
# Assigning s to 0, to take sum
# of another 4 element
s = 0
# Increment c
c += 1
# Append array p[] to l
l.append(p)
return l
# Driver code
if __name__ == "__main__":
# Original matrix
M = [[1, 2, 3, 4],
[4, 5, 6, 7],
[7, 8, 9, 10],
[10, 11, 12, 13]]
# Size of matrix
N = len(M)
# Calling function
res = checkMat(M, N)
# Printing the result
for i in res:
print(*i)
# this code is improved by prophet1999
C#
// C# code for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to find the desired matrix
static List >
checkMat(int[,] M, int N)
{
// Empty array to store 2x2 matrix element
List > l
= new List >(2);
for (int i = 0; i < N - 1; i++) {
// Initialization
List p = new List();
int c = 0, s = 0;
for (int j = 0; j < N - 1; j++) {
// Taking sum of 4 elements
s += M[i,c] + M[i,c + 1];
s += M[i + 1,c] + M[i + 1,c + 1];
// Appending sum in array p
p.Add(s);
// Assigning s to 0, to take sum
// of another 4 element
s = 0;
// Increment c
c++;
}
// Append array p[] to l
l.Add(p);
}
return l;
}
public static void Main(String[] args)
{
// Original matrix
int[,] M = { { 1, 2, 3, 4 },
{ 4, 5, 6, 7 },
{ 7, 8, 9, 10 },
{ 10, 11, 12, 13 } };
// Size of matrix
int N = M.GetLength(0);
// Calling function
List > res = checkMat(M, N);
// Printing the result
for (int i = 0; i < res.Count; i++) {
for (int j = 0; j < res[i].Count; j++) {
Console.Write(res[i][j] + " ");
}
Console.WriteLine();
}
}
}
// This code is contributed by 29AjayKumar and improved by prophet1999
Javascript
输出
12 16 20
24 28 32
36 40 44
时间复杂度: O(N*N)
辅助空间: O(N*N)