给定一个N x N矩阵和两个整数S和K ,任务是找出是否存在总和等于S的K x K子矩阵。
例子:
Input: K = 2, S = 14, mat[][] = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 }}
Output: Yes
1 2
5 6
is the required 2 x 2 sub-matrix with element sum = 14
Input: K = 1, S = 5, mat[][] = {{1, 2}, {7, 8}}
Output: No
方法:动态规划可以用来解决这个问题,
- 创建一个数组dp[N + 1][N + 1] ,其中dp[i][j]存储行在1到i之间、列在1到j之间的所有元素的总和。
- 一旦生成了二维矩阵,现在假设我们希望找到从(i, j)到(i + x, j + x)的平方和。所需的总和将为dp[i + x][j + x] – dp[i][j + x] – dp[i + x][j] + dp[i][j]其中,
- 第一项表示1到i + x之间的行和1到j + x之间的列中存在的所有元素的总和。这个区域有我们需要的正方形。
- 后两项是去除我们所需区域之外但在第一步计算的区域内的区域。
- 1到i之间的行和1到j之间的列的元素和在第二步中减去两次,因此添加一次。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
#define ll long long int
#define N 4
// Function to return the sum of the sub-matrix
int getSum(int r1, int r2, int c1, int c2,
int dp[N + 1][N + 1])
{
return dp[r2][c2] - dp[r2][c1] - dp[r1][c2]
+ dp[r1][c1];
}
// Function that returns true if it is possible
// to find the sub-matrix with required sum
bool sumFound(int K, int S, int grid[N][N])
{
// 2-D array to store the sum of
// all the sub-matrices
int dp[N + 1][N + 1];
// Filling of dp[][] array
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
dp[i + 1][j + 1] = dp[i + 1][j] + dp[i][j + 1]
- dp[i][j] + grid[i][j];
// Checking for each possible sub-matrix of size k X k
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) {
int sum = getSum(i, i + K, j, j + K, dp);
if (sum == S)
return true;
}
// Sub-matrix with the given sum not found
return false;
}
// Driver code
int main()
{
int grid[N][N] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
int K = 2;
int S = 14;
// Function call
if (sumFound(K, S, grid))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
// Modified by Kartik Verma
Java
// Java implementation of the approach
class GfG {
static int N = 4;
// Function to return the sum of the sub-matrix
static int getSum(int r1, int r2, int c1, int c2,
int dp[][])
{
return dp[r2][c2] - dp[r2][c1] - dp[r1][c2]
+ dp[r1][c1];
}
// Function that returns true if it is possible
// to find the sub-matrix with required sum
static boolean sumFound(int K, int S, int grid[][])
{
// 2-D array to store the sum of
// all the sub-matrices
int dp[][] = new int[N + 1][N + 1];
// Filling of dp[][] array
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
dp[i + 1][j + 1] = dp[i + 1][j]
+ dp[i][j + 1] - dp[i][j]
+ grid[i][j];
}
}
// Checking for each possible sub-matrix of size k X
// k
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
int sum = getSum(i, i + K, j, j + K, dp);
if (sum == S) {
return true;
}
}
}
// Sub-matrix with the given sum not found
return false;
}
// Driver code
public static void main(String[] args)
{
int grid[][] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
int K = 2;
int S = 14;
// Function call
if (sumFound(K, S, grid)) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
}
// This code contributed by Rajput-Ji
// Modified by Kartik Verma
Python3
# Python implementation of the approach
N = 4
# Function to return the sum of the sub-matrix
def getSum(r1, r2, c1, c2, dp):
return dp[r2][c2] - dp[r2][c1] - dp[r1][c2] + dp[r1][c1]
# Function that returns true if it is possible
# to find the sub-matrix with required sum
def sumFound(K, S, grid):
# 2-D array to store the sum of
# all the sub-matrices
dp = [[0 for i in range(N+1)] for j in range(N+1)]
# Filling of dp[][] array
for i in range(N):
for j in range(N):
dp[i + 1][j + 1] = dp[i + 1][j] + \
dp[i][j + 1] - dp[i][j] + grid[i][j]
# Checking for each possible sub-matrix of size k X k
for i in range(0, N):
for j in range(0, N):
sum = getSum(i, i + K, j, j + K, dp)
if (sum == S):
return True
# Sub-matrix with the given sum not found
return False
# Driver code
grid = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]
K = 2
S = 14
# Function call
if (sumFound(K, S, grid)):
print("Yes")
else:
print("No")
# This code is contributed by ankush_953
# Modified by Kartik Verma
C#
// C# implementation of the approach
using System;
class GfG {
static int N = 4;
// Function to return the sum of the sub-matrix
static int getSum(int r1, int r2, int c1, int c2,
int[, ] dp)
{
return dp[r2, c2] - dp[r2, c1] - dp[r1, c2]
+ dp[r1, c1];
}
// Function that returns true if it is possible
// to find the sub-matrix with required sum
static bool sumFound(int K, int S, int[, ] grid)
{
// 2-D array to store the sum of
// all the sub-matrices
int[, ] dp = new int[N + 1, N + 1];
// Filling of dp[,] array
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
dp[i + 1, j + 1] = dp[i + 1, j]
+ dp[i, j + 1] - dp[i, j]
+ grid[i, j];
}
}
// Checking for each possible sub-matrix of size k X
// k
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
int sum = getSum(i, i + K, j, j + K, dp);
if (sum == S) {
return true;
}
}
}
// Sub-matrix with the given sum not found
return false;
}
// Driver code
public static void Main(String[] args)
{
int[, ] grid = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
int K = 2;
int S = 14;
// Funciton call
if (sumFound(K, S, grid)) {
Console.WriteLine("Yes");
}
else {
Console.WriteLine("No");
}
}
}
// This code has been contributed by 29AjayKumar
// Modified by Kartik Verma
PHP
Javascript
输出:
Yes
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。