📜  二维矩阵中鸡尾酒杯的最大总和

📅  最后修改于: 2021-05-08 16:49:26             🧑  作者: Mango

给定2D矩阵mat [] [] ,任务是找到鸡尾酒杯的最大和。

A Cocktail glass is made of 6 cells in the following form:
X   X
  X
X X X 

例子:

Input: mat[][] = {
{1, 0, 4, 0, 0},
{0, 3, 0, 0, 0},
{1, 1, 1, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}}
Output: 11
Below is the cocktail glass with
maximum sum:
1   4 
  3
1 1 1
                                                      
Input: mat[][] = {
{0, 3, 0, 6, 0},
{0, 1, 1, 0, 0},
{1, 1, 1, 0, 0},
{0, 0, 2, 0, 1},
{0, 2, 0, 1, 3}}
Output: 12

方法:从鸡尾酒杯的定义中可以明显看出,行数和列数必须大于或等于3。如果我们对矩阵中的鸡尾酒杯总数进行计数,则可以说计数等于鸡尾酒杯中左上角可能存在的细胞数。鸡尾酒杯中左上角的细胞数等于(R – 2)*(C – 2)。因此,在一个矩阵中,鸡尾酒杯的总数为(R – 2)*(C – 2)

For mat[][] = {
{0, 3, 0, 6, 0},
{0, 1, 1, 0, 0},
{1, 1, 1, 0, 0},
{0, 0, 2, 0, 1},
{0, 2, 0, 1, 3}}
Possible cocktail glasses are:
0   0  3   6   0   0
  1      1       0 
1 1 1  1 1 0   1 0 0 

0   1  1   0  1   0
  1      1      0  
0 0 2  0 2 0  2 0 1 

1   1  1   0  1   0
  0      2      0
0 2 0  2 0 1  0 1 3

我们将鸡尾酒杯的所有左上角单元格一一考虑。对于每个单元格,我们计算由此形成的鸡尾酒杯的总和。最后,我们返回最大和。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
const int R = 5;
const int C = 5;
 
// Function to return the maximum sum
// of a cocktail glass
int findMaxCock(int ar[R][C])
{
 
    // If no cocktail glass is possible
    if (R < 3 || C < 3)
        return -1;
 
    // Initialize max_sum with the mini
    int max_sum = INT_MIN;
 
    // Here loop runs (R-2)*(C-2) times considering
    // different top left cells of cocktail glasses
    for (int i = 0; i < R - 2; i++) {
        for (int j = 0; j < C - 2; j++) {
 
            // Considering mat[i][j] as the top left
            // cell of the cocktail glass
            int sum = (ar[i][j] + ar[i][j + 2])
                      + (ar[i + 1][j + 1])
                      + (ar[i + 2][j] + ar[i + 2][j + 1]
                         + ar[i + 2][j + 2]);
 
            // Update the max_sum
            max_sum = max(max_sum, sum);
        }
    }
    return max_sum;
}
 
// Driver code
int main()
{
    int ar[][C] = { { 0, 3, 0, 6, 0 },
                    { 0, 1, 1, 0, 0 },
                    { 1, 1, 1, 0, 0 },
                    { 0, 0, 2, 0, 1 },
                    { 0, 2, 0, 1, 3 } };
 
    cout << findMaxCock(ar);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
static int R = 5;
static int C = 5;
 
// Function to return the maximum sum
// of a cocktail glass
static int findMaxCock(int ar[][])
{
 
    // If no cocktail glass is possible
    if (R < 3 || C < 3)
        return -1;
 
    // Initialize max_sum with the mini
    int max_sum = Integer.MIN_VALUE;
 
    // Here loop runs (R-2)*(C-2) times considering
    // different top left cells of cocktail glasses
    for (int i = 0; i < R - 2; i++)
    {
        for (int j = 0; j < C - 2; j++)
        {
 
            // Considering mat[i][j] as the top left
            // cell of the cocktail glass
            int sum = (ar[i][j] + ar[i][j + 2])
                    + (ar[i + 1][j + 1])
                    + (ar[i + 2][j] + ar[i + 2][j + 1]
                        + ar[i + 2][j + 2]);
 
            // Update the max_sum
            max_sum = Math.max(max_sum, sum);
        }
    }
    return max_sum;
}
 
// Driver code
public static void main (String[] args)
{
 
    int ar[][] = { { 0, 3, 0, 6, 0 },
                    { 0, 1, 1, 0, 0 },
                    { 1, 1, 1, 0, 0 },
                    { 0, 0, 2, 0, 1 },
                    { 0, 2, 0, 1, 3 } };
 
    System.out.println(findMaxCock(ar));
}
}
 
// This code is contributed by mits


Python3
# Python 3 implementation of the approach
import sys
 
R = 5
C = 5
 
# Function to return the maximum sum
# of a cocktail glass
def findMaxCock(ar):
     
    # If no cocktail glass is possible
    if (R < 3 or C < 3):
        return -1
 
    # Initialize max_sum with the mini
    max_sum = -sys.maxsize - 1
 
    # Here loop runs (R-2)*(C-2) times considering
    # different top left cells of cocktail glasses
    for i in range(R - 2):
        for j in range(C - 2):
             
            # Considering mat[i][j] as the top left
            # cell of the cocktail glass
            sum = ((ar[i][j] + ar[i][j + 2]) +
                   (ar[i + 1][j + 1]) +
                   (ar[i + 2][j] + ar[i + 2][j + 1] +
                    ar[i + 2][j + 2]))
 
            # Update the max_sum
            max_sum = max(max_sum, sum)
 
    return max_sum;
 
# Driver code
if __name__ == '__main__':
    ar = [[0, 3, 0, 6, 0],
          [0, 1, 1, 0, 0],
          [1, 1, 1, 0, 0],
          [0, 0, 2, 0, 1],
          [0, 2, 0, 1, 3]]
 
    print(findMaxCock(ar))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
    static int R = 5;
    static int C = 5;
     
    // Function to return the maximum sum
    // of a cocktail glass
    static int findMaxCock(int [,]ar)
    {
     
        // If no cocktail glass is possible
        if (R < 3 || C < 3)
            return -1;
     
        // Initialize max_sum with the mini
        int max_sum = int.MinValue;
     
        // Here loop runs (R-2)*(C-2) times considering
        // different top left cells of cocktail glasses
        for (int i = 0; i < R - 2; i++)
        {
            for (int j = 0; j < C - 2; j++)
            {
     
                // Considering mat[i][j] as the top left
                // cell of the cocktail glass
                int sum = (ar[i,j] + ar[i,j + 2])
                        + (ar[i + 1,j + 1])
                        + (ar[i + 2,j] + ar[i + 2,j + 1]
                            + ar[i + 2,j + 2]);
     
                // Update the max_sum
                max_sum = Math.Max(max_sum, sum);
            }
        }
        return max_sum;
    }
     
    // Driver code
    public static void Main ()
    {
     
        int [,]ar = { { 0, 3, 0, 6, 0 },
                        { 0, 1, 1, 0, 0 },
                        { 1, 1, 1, 0, 0 },
                        { 0, 0, 2, 0, 1 },
                        { 0, 2, 0, 1, 3 } };
     
        Console.WriteLine(findMaxCock(ar));
    }
}
 
// This code is contributed by Ryuga


PHP


Javascript


输出:
12