矩阵中沙漏的最大总和
给定一个二维矩阵,任务是找到一个沙漏的最大和。
An hour glass is made of 7 cells
in following form.
A B C
D
E F G
例子:
Input : 1 1 1 0 0
0 1 0 0 0
1 1 1 0 0
0 0 0 0 0
0 0 0 0 0
Output : 7
Below is the hour glass with
maximum sum:
1 1 1
1
1 1 1
Input : 0 3 0 0 0
0 1 0 0 0
1 1 1 0 0
0 0 2 4 4
0 0 0 2 4
Output : 11
Below is the hour glass with
maximum sum
1 0 0
4
0 2 4
方法:
从沙漏的定义可以看出,行数和列数必须等于3。如果我们计算一个矩阵中沙漏的总数,我们可以说这个计数等于可能的顶部的计数将细胞留在沙漏中。沙漏中左上角的单元格数等于 (R-2)*(C-2)。因此,在矩阵中,沙漏的总数为(R-2)*(C-2)。
mat[][] = 2 3 0 0 0
0 1 0 0 0
1 1 1 0 0
0 0 2 4 4
0 0 0 2 0
Possible hour glass are :
2 3 0 3 0 0 0 0 0
1 0 0
1 1 1 1 1 0 1 0 0
0 1 0 1 0 0 0 0 0
1 1 0
0 0 2 0 2 4 2 4 4
1 1 1 1 1 0 1 0 0
0 2 4
0 0 0 0 0 2 0 2 0
逐一考虑沙漏的所有左上角单元格。对于每个单元,我们计算由它形成的沙漏的总和。最后,返回最大总和。
下面是上述思想的实现:
C++
// C++ program to find maximum sum of hour
// glass in matrix
#include
using namespace std;
const int R = 5;
const int C = 5;
// Returns maximum sum of hour glass in ar[][]
int findMaxSum(int mat[R][C])
{
if (R<3 || C<3)
return -1;
// Here loop runs (R-2)*(C-2) times considering
// different top left cells of hour glasses.
int max_sum = INT_MIN;
for (int i=0; iC/* C program to find the maximum
sum of hour glass in a Matrix */
#include
// Fixing the size of the matrix
// ( Here it is of the order 6
// x 6 )
#define R 5
#define C 5
// Function to find the maximum
// sum of the hour glass
int MaxSum(int arr[R][C])
{
int i, j, sum;
if (R < 3 || C < 3)
return -1;
int max_sum
= -500000; /* Considering the matrix also contains
negative values , so initialized with
-50000. It can be any value but very
smaller.*/
// int max_sum=0 -> Initialize with 0 only if your
// matrix elements are positive
// Here loop runs (R-2)*(C-2) times considering
// different top left cells of hour glasses.
for (i = 0; i < R - 2; i++) {
for (j = 0; j < C - 2; j++) {
// Considering arr[i][j] as top left cell of
// hour glass.
sum = (arr[i][j] + arr[i][j + 1]
+ arr[i][j + 2])
+ (arr[i + 1][j + 1])
+ (arr[i + 2][j] + arr[i + 2][j + 1]
+ arr[i + 2][j + 2]);
// If previous sum is less then current sum then
// update new sum in max_sum
if (sum > max_sum)
max_sum = sum;
else
continue;
}
}
return max_sum;
}
// Driver Code
int main()
{
int arr[][C] = { { 1, 2, 3, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 2, 1, 4, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 1, 1, 0, 1, 0 } };
int res = MaxSum(arr);
if (res == -1)
printf("Not Possible");
else
printf("Maximum sum of hour glass = %d", res);
return 0;
}
// This code is written by Akshay Prakash
Java
// Java program to find maximum
// sum of hour glass in matrix
import java.io.*;
class GFG {
static int R = 5;
static int C = 5;
// Returns maximum sum of
// hour glass in ar[][]
static int findMaxSum(int [][]mat)
{
if (R < 3 || C < 3)
return -1;
// Here loop runs (R-2)*(C-2)
// times considering different
// top left cells of hour glasses.
int max_sum = Integer.MIN_VALUE;
for (int i = 0; i < R - 2; i++)
{
for (int j = 0; j < C - 2; j++)
{
// Considering mat[i][j] as top
// left cell of hour glass.
int sum = (mat[i][j] + mat[i][j + 1] +
mat[i][j + 2]) + (mat[i + 1][j + 1]) +
(mat[i + 2][j] + mat[i + 2][j + 1] +
mat[i + 2][j + 2]);
// If previous sum is less then
// current sum then update
// new sum in max_sum
max_sum = Math.max(max_sum, sum);
}
}
return max_sum;
}
// Driver code
static public void main (String[] args)
{
int [][]mat = {{1, 2, 3, 0, 0},
{0, 0, 0, 0, 0},
{2, 1, 4, 0, 0},
{0, 0, 0, 0, 0},
{1, 1, 0, 1, 0}};
int res = findMaxSum(mat);
if (res == -1)
System.out.println("Not possible");
else
System.out.println("Maximum sum of hour glass = "
+ res);
}
}
// This code is contributed by vt_m .
Python3
# Python 3 program to find the maximum
# sum of hour glass in a Matrix
# Fixing the size of the Matrix.
# Here it is of order 6 x 6
R = 5
C = 5
# Function to find the maximum sum of hour glass
def MaxSum(arr):
# Considering the matrix also contains
max_sum = -50000
# Negative values , so initialized with
# -50000. It can be any value but very
# smaller.
# max_sum=0 -> Initialize with 0 only if your
# matrix elements are positive
if(R < 3 or C < 3):
return -1
# Here loop runs (R-2)*(C-2) times considering
# different top left cells of hour glasses.
for i in range(0, R-2):
for j in range(0, C-2):
# Considering arr[i][j] as top
# left cell of hour glass.
SUM = (arr[i][j] + arr[i][j + 1] + arr[i][j + 2]) + (arr[i + 1][j + 1]) + (arr[i + 2][j] +
arr[i + 2][j + 1] + arr[i + 2][j + 2])
# If previous sum is less
# then current sum then
# update new sum in max_sum
if(SUM > max_sum):
max_sum = SUM
else:
continue
return max_sum
# Driver Code
arr = [[1, 2, 3, 0, 0],
[0, 0, 0, 0, 0],
[2, 1, 4, 0, 0],
[0, 0, 0, 0, 0],
[1, 1, 0, 1, 0]]
res = MaxSum(arr)
if(res == -1):
print("Not possible")
else:
print(f"Maximum sum of hour glass = {res}")
# This code is written by Akshay Prakash
C#
// C# program to find maximum
// sum of hour glass in matrix
using System;
class GFG {
static int R = 5;
static int C = 5;
// Returns maximum sum of
// hour glass in ar[][]
static int findMaxSum(int [,]mat)
{
if (R < 3 || C < 3)
return -1;
// Here loop runs (R-2)*(C-2)
// times considering different
// top left cells of hour glasses.
int max_sum = int.MinValue;
for (int i = 0; i < R - 2; i++)
{
for (int j = 0; j < C - 2; j++)
{
// Considering mat[i][j] as top
// left cell of hour glass.
int sum = (mat[i, j] + mat[i, j + 1] +
mat[i, j + 2]) + (mat[i + 1, j + 1]) +
(mat[i + 2, j] + mat[i + 2, j + 1] +
mat[i + 2, j + 2]);
// If previous sum is less then
// current sum then update
// new sum in max_sum
max_sum = Math.Max(max_sum, sum);
}
}
return max_sum;
}
// Driver code
static public void Main(String[] args)
{
int [,]mat = {{1, 2, 3, 0, 0},
{0, 0, 0, 0, 0},
{2, 1, 4, 0, 0},
{0, 0, 0, 0, 0},
{1, 1, 0, 1, 0}};
int res = findMaxSum(mat);
if (res == -1)
Console.WriteLine("Not possible");
else
Console.WriteLine("Maximum sum of hour glass = "
+ res);
}
}
// This code is contributed by vt_m .
PHP
Javascript
输出:
Maximum sum of hour glass = 13