Maximum value in a matrix which contain intersecting concentric submatrix

Suppose a matrix of size N X N which contain concentric square submatrices centered at (xi, yi), where xi is the row number of center of ith concentric square matrix and yi is the column number of center of ith concentric square matrix. Concentric square matrix is of the form:

0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 0
0 1 . . . . . 1 0 
0 1 . b b b . 1 0
0 1 . b a b . 1 0
0 1 . b b b . 1 0
0 1 . . . . . 1 0
0 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0

where a is the center, b is a – 1, and the value will decrease as row or column increase.

Since there are multiple such sub-matrix, there are cells which are parts of more than one such submatrix. Those cells will have the value equal to the sum of values of intersecting submatrix. Given the value of N, m, (xi, yi, ai), where 1 <= i <= m and ai is the value at the center of ith concentric sub-matrix. The task is to find the maximum value in the matrix containing submatrices.
So, after
Examples:



Input : N = 10, m = 2
        (x1, y1, a1) = (3, 3, 3)
        (x2, y2, a2) = (7, 7, 4)
Output : 4
Maxtrix that will be form:


Input : N = 10, m = 1
        (x1, y1, a1) = (4, 5, 6)
Output : 6

The idea is to make a 2D matrix mat[][] and find the value of each cell including the cell having the intersection of multiple concentric submatrix. Now, observe value of each cell can be find by max(0, a – max(p – xi, q – yi)) where a is the value of at the center of ith concentric sub-matrix, p is the row number of the cell, q is the column number of the cell and (xi, yi) is the center location of ith concentric sub-matrix center. So, after finding the matrix mat[][], we will traverse the matrix to find the maximum value in the matrix.

Below is C++ implementation of this approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

//C++ Program to find the maximum value in a matrix
//which contain intersecting concentric submatrix
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100
  
// Return the maximum value in intersecting
// concentric submatrix.
int maxValue(int n, int m, int x[], int y[], int a[])
{
    int c[MAXN][MAXN] = { 0 };
  
    // For each center of concentric sub-matrix.
    for (int i = 0; i < m; ++i) {
  
        // for each row
        for (int p = 0; p < n; ++p) {
  
            // for each column
            for (int q = 0; q < n; ++q) {
  
                // finding x distance.
                int dx = abs(p - x[i]);
  
                // finding y distance.
                int dy = abs(q - y[i]);
  
                // maximum of x distance and y distance
                int d = max(dx, dy);
  
                // assigning the value.
                c[p][q] += max(0, a[i] - d);
            }
        }
    }
  
    // Finding the maximum value in the formed matrix.
    int res = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            res = max(res, c[i][j]);
        }
    }
    return res;
}
  
  
// Driven Program
int main()
{
    int n = 10;
    int m = 2;
    int x[] = { 3, 7 };
    int y[] = { 3, 7 };
    int a[] = { 4, 3 };
  
    cout << maxValue(n, m, x, y, a) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to find the 
// maximum value in a matrix 
// which contain intersecting 
// concentric submatrix
import java.io.*;
  
class GFG
{
static int MAXN = 100;
  
// Return the maximum value 
// in intersecting
// concentric submatrix.
static int maxValue(int n, int m, 
                    int x[], int y[], 
                    int a[])
{
    int c[][] = new int[MAXN][MAXN];
  
    // For each center of 
    // concentric sub-matrix.
    for (int i = 0; i < m; ++i)
    {
  
        // for each row
        for (int p = 0; p < n; ++p) 
        {
  
            // for each column
            for (int q = 0; q < n; ++q)
            {
  
                // finding x distance.
                int dx = Math.abs(p - x[i]);
  
                // finding y distance.
                int dy = Math.abs(q - y[i]);
  
                // maximum of x distance
                // and y distance
                int d = Math.max(dx, dy);
  
                // assigning the value.
                c[p][q] += Math.max(0, a[i] - d);
            }
        }
    }
  
    // Finding the maximum 
    // value in the formed matrix.
    int res = 0;
    for (int i = 0; i < n; ++i) 
    {
        for (int j = 0; j < n; ++j) 
        {
            res = Math.max(res, c[i][j]);
        }
    }
    return res;
}
  
  
// Driven Code
public static void main (String[] args) 
{
    int n = 10;
    int m = 2;
    int x[] = { 3, 7 };
    int y[] = { 3, 7 };
    int a[] = { 4, 3 };
  
    System.out.println(maxValue(n, m, x, 
                                 y, a));
}
}
  
// This code is contributed by anuj_67.

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 Program to find the maximum 
# value in a matrix which contain 
# intersecting concentric submatrix
MAXN = 100
  
# Return the maximum value in intersecting
# concentric submatrix.
def maxValue( n, m, x, y, a):
  
    c = [[0 for x in range(MAXN)]
            for y in range(MAXN)] 
  
    # For each center of concentric sub-matrix.
    for i in range( m):
  
        # for each row
        for p in range(n) :
  
            # for each column
            for q in range( n) :
  
                # finding x distance.
                dx = abs(p - x[i])
  
                # finding y distance.
                dy = abs(q - y[i])
  
                # maximum of x distance and y distance
                d = max(dx, dy)
  
                # assigning the value.
                c[p][q] += max(0, a[i] - d)
  
    # Finding the maximum value in
    # the formed matrix.
    res = 0
    for i in range(n) :
        for j in range(n) :
            res = max(res, c[i][j])
    return res
  
# Driver Code
if __name__ == "__main__":
      
    n = 10
    m = 2
    x = [ 3, 7 ]
    y = [ 3, 7 ]
    a = [ 4, 3 ]
  
    print(maxValue(n, m, x, y, a))
  
# This code is contributed by ita_c

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to find the maximum 
// value in a matrix which contain 
// intersecting concentric submatrix
using System;
  
class GFG
{
static int MAXN = 100;
  
// Return the maximum value in intersecting
// concentric submatrix.
static int maxValue(int n, int m, 
                    int[] x, int[] y, 
                    int[] a)
{
    int[,] c = new int[MAXN, MAXN];
  
    // For each center of 
    // concentric sub-matrix.
    for (int i = 0; i < m; ++i)
    {
  
        // for each row
        for (int p = 0; p < n; ++p) 
        {
  
            // for each column
            for (int q = 0; q < n; ++q)
            {
  
                // finding x distance.
                int dx = Math.Abs(p - x[i]);
  
                // finding y distance.
                int dy = Math.Abs(q - y[i]);
  
                // maximum of x distance
                // and y distance
                int d = Math.Max(dx, dy);
  
                // assigning the value.
                c[p,q] += Math.Max(0, a[i] - d);
            }
        }
    }
  
    // Finding the maximum 
    // value in the formed matrix.
    int res = 0;
    for (int i = 0; i < n; ++i) 
    {
        for (int j = 0; j < n; ++j) 
        {
            res = Math.Max(res, c[i, j]);
        }
    }
    return res;
}
  
// Driver Code
public static void Main () 
{
    int n = 10;
    int m = 2;
    int[] x = { 3, 7 };
    int[] y = { 3, 7 };
    int[] a = { 4, 3 };
  
    Console.Write(maxValue(n, m, x, y, a));
}
}

chevron_right


Output:

4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vt_m, chitranayal, nidhi_biet



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.