📜  检查给定矩阵是否在逐行和逐列递增

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

给定矩阵mat [] [] ,任务是检查给定矩阵是否严格增加。如果矩阵的所有行和所有列都严格增加,则称矩阵严格增加。

例子:

方法:对每个元素进行线性遍历,并检查是否逐行和逐列增加。这两个条件是(a [i] [j]> a [i – 1] [j])(a [i] [j]> a [i] [j – 1]) 。如果两个条件中的任何一个失败,则矩阵不会严格增加。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define N 2
#define M 2
  
// Function that returns true if the matrix
// is strictly increasing
bool isMatrixInc(int a[N][M])
{
  
    // Check if the matrix
    // is strictly increasing
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
  
            // Out of bound condition
            if (i - 1 >= 0) {
                if (a[i][j] <= a[i - 1][j])
                    return false;
            }
  
            // Out of bound condition
            if (j - 1 >= 0) {
                if (a[i][j] <= a[i][j - 1])
                    return false;
            }
        }
    }
  
    return true;
}
  
// Driver code
int main()
{
  
    int a[N][M] = { { 2, 10 },
                    { 11, 20 } };
  
    if (isMatrixInc(a))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
  
class GFG 
{
  
static int N = 2;
static int M = 2;
  
// Function that returns true if the matrix
// is strictly increasing
static boolean isMatrixInc(int a[][])
{
  
    // Check if the matrix
    // is strictly increasing
    for (int i = 0; i < N; i++) 
    {
        for (int j = 0; j < M; j++) 
        {
  
            // Out of bound condition
            if (i - 1 >= 0)
            {
                if (a[i][j] <= a[i - 1][j])
                    return false;
            }
  
            // Out of bound condition
            if (j - 1 >= 0) 
            {
                if (a[i][j] <= a[i][j - 1])
                    return false;
            }
        }
    }
  
    return true;
}
  
// Driver code
  
public static void main (String[] args) 
{
      
    int a[][] = { { 2, 10 },
                    { 11, 20 } };
      
    if (isMatrixInc(a))
        System.out.print( "Yes");
    else
        System.out.print( "No");
}
}
  
// This code is contributed by anuj_67..


Python3
# Python3 implementation of the approach 
  
N, M = 2, 2
  
# Function that returns true if the matrix 
# is strictly increasing 
def isMatrixInc(a) : 
  
    # Check if the matrix 
    # is strictly increasing 
    for i in range(N) :
        for j in range(M) :
  
            # Out of bound condition 
            if (i - 1 >= 0) :
                if (a[i][j] <= a[i - 1][j]) : 
                    return False; 
  
            # Out of bound condition 
            if (j - 1 >= 0) : 
                if (a[i][j] <= a[i][j - 1]) :
                    return False; 
                      
    return True;
  
  
# Driver code 
if __name__ == "__main__" : 
  
    a = [ [ 2, 10 ], 
        [11, 20 ] ]; 
  
    if (isMatrixInc(a)) :
        print("Yes"); 
    else :
        print("No"); 
          
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System; 
      
class GFG 
{
  
static int N = 2;
static int M = 2;
  
// Function that returns true if the matrix
// is strictly increasing
static Boolean isMatrixInc(int [,]a)
{
  
    // Check if the matrix
    // is strictly increasing
    for (int i = 0; i < N; i++) 
    {
        for (int j = 0; j < M; j++) 
        {
  
            // Out of bound condition
            if (i - 1 >= 0)
            {
                if (a[i,j] <= a[i - 1,j])
                    return false;
            }
  
            // Out of bound condition
            if (j - 1 >= 0) 
            {
                if (a[i,j] <= a[i,j - 1])
                    return false;
            }
        }
    }
  
    return true;
}
  
// Driver code
public static void Main (String[] args) 
{
      
    int [,]a = { { 2, 10 },
                    { 11, 20 } };
      
    if (isMatrixInc(a))
        Console.WriteLine( "Yes");
    else
        Console.WriteLine( "No");
}
}
  
// This code is contributed by Princi Singh


输出:
Yes