原文:

给定一个m x n矩阵的矩阵,我们需要打印所有子矩阵,这些子矩阵的总和在此单元结束,并且子矩阵从该单元开始等于其余元素。 为了更好地理解,请参见下图,

示例

input : mat[][] = {1, 2, 3, 5,
                   4, 1, 0, 2,
                   0, 1, 2, 0,
                   7, 1, 1, 0};
output : (1, 1), (2, 2)      
in above matrix, cell (1, 1) and cell (2, 2)
are our required cells because,
for cell (1, 1), sum of red and green areas is same
1 2 4 1 0 2 1 2 0 1 1 0 = 3 5 0 7        
same is true for cell (2, 2)
1 2 3 4 1 0 0 1 2 0 1 0 = 5 2 7 1    
we need to print all blue boundary cells for 
which sum of red area is equal to green area.

首先,我们。

我们构造两个矩阵sum[][]sumr[][],使得sum[i][j]表示从mat[0][0]mat[i][j]。 用于存储总和直到最后一个索引的求和器,即sumr[i][j]表示子矩阵mat[i][j]mat[m – 1][n – 1]的总和。

现在我们可以使用上述矩阵来解决此问题,可以通过将求和矩阵与求和矩阵相加相应的单元格来计算上图中的红色区域,因为在计算此和时要考虑两次mat[i][j]减去一次即可得到红色区域的总和。 获取剩余元素的总和(绿色部分)非常容易,我们将从给定矩阵的总和中减去红色部分的总和。

因此,要检查特定单元格是否满足给定条件,我们将如上所述计算红色部分的总和,并将其与矩阵的总和进行比较,如果该总和为矩阵总和的一半,则当前单元格满足条件,因此是结果的候选。

c/c

// c   program to print cells with same rectangular 
// sum diagonally 
#include  
using namespace std; 
#define r 4 
#define c 4 
// method prints cell index at which rectangular sum is 
// same at prime diagonal and other diagonal 
void printcellwithsamerectangulararea(int mat[r][c], 
                                     int m, int n) 
{ 
    /* sum[i][j] denotes sum of sub-matrix, mat[0][0] 
       to mat[i][j] 
       sumr[i][j] denotes sum of sub-matrix, mat[i][j] 
       to mat[m - 1][n - 1]  */
    int sum[m][n], sumr[m][n]; 
    //  initialize both sum matrices by mat 
    int totalsum = 0; 
    for (int i = 0; i < m; i  ) 
    { 
        for (int j = 0; j < n; j  ) 
        { 
            sumr[i][j] = sum[i][j] = mat[i][j]; 
            totalsum  = mat[i][j]; 
        } 
    } 
    //  updating first and last row separately 
    for (int i = 1; i < m; i  ) 
    { 
        sum[i][0]  = sum[i-1][0]; 
        sumr[m-i-1][n-1]  = sumr[m-i][n-1]; 
    } 
    //  updating first and last column separately 
    for (int j = 1; j < n; j  ) 
    { 
        sum[0][j]  = sum[0][j-1]; 
        sumr[m-1][n-j-1]  = sumr[m-1][n-j]; 
    } 
    //  updating sum and sumr indices by nearby indices 
    for (int i = 1; i < m; i  ) 
    { 
        for (int j = 1; j < n; j  ) 
        { 
            sum[i][j]  = sum[i-1][j]   sum[i][j-1] - 
                                      sum[i-1][j-1]; 
            sumr[m-i-1][n-j-1]  = sumr[m-i][n-j-1]   
                                  sumr[m-i-1][n-j] - 
                                  sumr[m-i][n-j]; 
        } 
    } 
    //  uncomment below code to print sum and reverse sum 
    // matrix 
    /* 
        for (int i = 0; i < m; i  ) 
        { 
            for (int j = 0; j < n; j  ) 
            { 
                cout << sum[i][j] << " "; 
            } 
            cout << endl; 
        } 
        cout << endl; 
        for (int i = 0; i < m; i  ) 
        { 
            for (int j = 0; j < n; j  ) 
            { 
                cout << sumr[i][j] << " "; 
            } 
            cout << endl; 
        } 
        cout << endl;    */
    /* print all those indices at which sum of prime diagonal 
       rectangles is half of the total sum of matrix  */
    for (int i = 0; i < m; i  ) 
    { 
        for (int j = 0; j < n; j  ) 
        { 
            int maindiagrectanglesum = sum[i][j]   sumr[i][j] - 
                                                   mat[i][j]; 
            if (totalsum == 2 * maindiagrectanglesum) 
                cout << "(" << i << ", " << j << ")" << endl; 
        } 
    } 
} 
//  driver code to test above methods 
int main() 
{ 
    int mat[r][c] = 
    { 
        1, 2, 3, 5, 
        4, 1, 0, 2, 
        0, 1, 2, 0, 
        7, 1, 1, 0 
    }; 
    printcellwithsamerectangulararea(mat, r, c); 
    return 0; 
} 

java

// java program to print cells with  
// same rectangular sum diagonally 
class gfg { 
static final int r = 4; 
static final int c = 4; 
// method prints cell index at which  
// rectangular sum is same at  
// prime diagonal and other diagonal 
static void printcellwithsamerectangulararea(int mat[][],  
                                             int m, int n)  
{ 
    /* sum[i][j] denotes sum of sub-matrix, mat[0][0] 
    to mat[i][j] 
    sumr[i][j] denotes sum of sub-matrix, mat[i][j] 
    to mat[m - 1][n - 1] */
    int sum[][] = new int[m][n]; 
    int sumr[][] = new int[m][n]; 
    // initialize both sum matrices by mat 
    int totalsum = 0; 
    for (int i = 0; i < m; i  ) { 
    for (int j = 0; j < n; j  ) { 
        sumr[i][j] = sum[i][j] = mat[i][j]; 
        totalsum  = mat[i][j]; 
    } 
    } 
    // updating first and last row separately 
    for (int i = 1; i < m; i  ) { 
    sum[i][0]  = sum[i - 1][0]; 
    sumr[m - i - 1][n - 1]  = sumr[m - i][n - 1]; 
    } 
    // updating first and last column separately 
    for (int j = 1; j < n; j  ) { 
    sum[0][j]  = sum[0][j - 1]; 
    sumr[m - 1][n - j - 1]  = sumr[m - 1][n - j]; 
    } 
    // updating sum and sumr indices by nearby indices 
    for (int i = 1; i < m; i  ) { 
    for (int j = 1; j < n; j  ) { 
        sum[i][j]  = sum[i - 1][j]   sum[i][j - 1] -  
                                     sum[i - 1][j - 1]; 
        sumr[m - i - 1][n - j - 1]  = sumr[m - i][n - j - 1]   
                                      sumr[m - i - 1][n - j] - 
                                      sumr[m - i][n - j]; 
    } 
    } 
    // uncomment below code to print sum and reverse sum 
    // matrix 
    /* 
        for (int i = 0; i < m; i  ) 
        { 
            for (int j = 0; j < n; j  ) 
            { 
                system.out.print( sum[i][j]   " "); 
            } 
            system.out.println(); 
        } 
        system.out.println(); 
        for (int i = 0; i < m; i  ) 
        { 
            for (int j = 0; j < n; j  ) 
            { 
                system.out.print(sumr[i][j]   " "); 
            } 
            system.out.println(); 
        } 
        system.out.println(); */
    /* print all those indices at which sum of prime diagonal 
    rectangles is half of the total sum of matrix */
    for (int i = 0; i < m; i  ) { 
    for (int j = 0; j < n; j  ) { 
        int maindiagrectanglesum = sum[i][j]    
                       sumr[i][j] - mat[i][j]; 
        if (totalsum == 2 * maindiagrectanglesum) 
        system.out.println("("   i   ", "   j   ")"); 
    } 
    } 
} 
// driver code 
public static void main(string[] args) 
{ 
    int mat[][] = {{1, 2, 3, 5}, 
                   {4, 1, 0, 2},  
                   {0, 1, 2, 0},  
                   {7, 1, 1, 0}}; 
    printcellwithsamerectangulararea(mat, r, c); 
} 
} 
// this code is contributed by anant agarwal. 

python

# python program to print cells with same rectangular 
# sum diagonally 
# r 4 
# c 4 
# method prints cell index at which rectangular sum is 
# same at prime diagonal and other diagonal 
def printcellwithsamerectangulararea(mat, m, n): 
    # sum[i][j] denotes sum of sub-matrix, mat[0][0] 
    #  to mat[i][j] 
    # sumr[i][j] denotes sum of sub-matrix, mat[i][j] 
    # to mat[m - 1][n - 1]   
    sum = [[0 for i in range(m)]for j in range(n)] 
    sumr = [[0 for i in range(m)]for j in range(n)] 
    #  initialize both sum matrices by mat 
    totalsum = 0
    for i in range(m): 
        for j in range(n): 
            sumr[i][j] = sum[i][j] = mat[i][j]; 
            totalsum  = mat[i][j] 
    #  updating first and last row separately 
    for i in range(1,m): 
        sum[i][0]  = sum[i-1][0] 
        sumr[m-i-1][n-1]  = sumr[m-i][n-1] 
    #  updating first and last column separately 
    for j in range(1,n): 
        sum[0][j]  = sum[0][j-1]; 
        sumr[m-1][n-j-1]  = sumr[m-1][n-j] 
    #  updating sum and sumr indices by nearby indices 
    for i in range(1,m): 
        for j in range(1,n): 
            sum[i][j]  = sum[i-1][j]   sum[i][j-1] - sum[i-1][j-1] 
            sumr[m-i-1][n-j-1]  = sumr[m-i][n-j-1]   sumr[m-i-1][n-j] - sumr[m-i][n-j] 
    #  uncomment below code to print sum and reverse sum 
    # matrix 
    # print all those indices at which sum of prime diagonal 
    #   rectangles is half of the total sum of matrix   
    for i in range(m): 
        for j in range(n): 
            maindiagrectanglesum = sum[i][j]   sumr[i][j] - mat[i][j] 
            if (totalsum == 2 * maindiagrectanglesum): 
                print "(", 
                print i, 
                print ",", 
                print j, 
                print ")", 
#  driver code to test above methods 
mat =[[1, 2, 3, 5,], 
     [4, 1, 0, 2,], 
     [0, 1, 2, 0], 
     [7, 1, 1, 0]] 
printcellwithsamerectangulararea(mat, 4, 4) 
# contributed by afzal 

c#

// c# program to print cells with  
// same rectangular sum diagonally 
using system; 
class gfg { 
static int r = 4; 
static int c = 4; 
// method prints cell index at which  
// rectangular sum is same at  
// prime diagonal and other diagonal 
static void printcellwithsamerectangulararea(int [,]mat,  
                                             int m, int n)  
{ 
    /* sum[i][j] denotes sum of sub- 
       matrix, mat[0][0] to mat[i][j] 
       sumr[i][j] denotes sum of sub-matrix, 
       mat[i][j] to mat[m - 1][n - 1] */
    int [,]sum = new int[m, n]; 
    int [,]sumr = new int[m, n]; 
    // initialize both sum matrices by mat 
    int totalsum = 0; 
    for (int i = 0; i < m; i  ) { 
    for (int j = 0; j < n; j  ) { 
        sumr[i, j] = sum[i, j] = mat[i, j]; 
        totalsum  = mat[i, j]; 
    } 
    } 
    // updating first and last row separately 
    for (int i = 1; i < m; i  ) 
    { 
    sum[i, 0]  = sum[i - 1, 0]; 
    sumr[m - i - 1, n - 1]  = sumr[m - i, n - 1]; 
    } 
    // updating first and last column separately 
    for (int j = 1; j < n; j  )  
    { 
    sum[0,j]  = sum[0,j - 1]; 
    sumr[m - 1,n - j - 1]  = sumr[m - 1,n - j]; 
    } 
    // updating sum and sumr indices by nearby indices 
    for (int i = 1; i < m; i  ) { 
    for (int j = 1; j < n; j  ) { 
        sum[i,j]  = sum[i - 1,j]   sum[i,j - 1] -  
                                   sum[i - 1,j - 1]; 
        sumr[m - i - 1,n - j - 1]  = sumr[m - i,n - j - 1]   
                                     sumr[m - i - 1,n - j] - 
                                     sumr[m - i,n - j]; 
    } 
    } 
    // uncomment below code to print sum and reverse sum 
    // matrix 
    /* 
        for (int i = 0; i < m; i  ) 
        { 
            for (int j = 0; j < n; j  ) 
            { 
                system.out.print( sum[i][j]   " "); 
            } 
            system.out.println(); 
        } 
        system.out.println(); 
        for (int i = 0; i < m; i  ) 
        { 
            for (int j = 0; j < n; j  ) 
            { 
                system.out.print(sumr[i][j]   " "); 
            } 
            system.out.println(); 
        } 
        system.out.println(); */
    /* print all those indices at which sum 
       of prime diagonal rectangles is half  
       of the total sum of matrix */
    for (int i = 0; i < m; i  ) { 
    for (int j = 0; j < n; j  ) { 
        int maindiagrectanglesum = sum[i,j]    
                    sumr[i,j] - mat[i,j]; 
        if (totalsum == 2 * maindiagrectanglesum) 
        console.writeline("("   i   ", "   j   ")"); 
    } 
    } 
} 
// driver code 
public static void main() 
{ 
    int [,]mat = {{1, 2, 3, 5}, 
                  {4, 1, 0, 2},  
                  {0, 1, 2, 0},  
                  {7, 1, 1, 0}}; 
    printcellwithsamerectangulararea(mat, r, c); 
} 
} 
// this code is contributed by vt_m. 

output:

(1, 1)
(2, 2)