📜  检查给定的矩阵是否平衡

📅  最后修改于: 2021-04-29 17:41:26             🧑  作者: Mango

给定尺寸为NxM的矩阵mat [] [] ,任务是检查给定的矩阵是否平衡。打印“平衡”,如果它是一个平衡矩阵否则打印“不平衡”。

例子:

方法:

  1. 遍历给定的矩阵mat [] []
  2. 对于矩阵的每个像元,检查是否所有相邻像元,即mat [i + 1] [j],mat [i] [j + 1],mat [i-1] [j],mat [i] [j -1]严格小于当前单元格。
  3. 对于矩阵的角单元,只有两个相邻的单元,即mat [i + 1] [j]和mat [i] [j + 1]检查所有这些相邻单元是否严格小于角单元。
  4. 对于矩阵的边界单元,有3个相邻单元,即mat [i-1] [j],mat [i + 1] [j]和mat [i] [j + 1]检查是否所有这些相邻单元严格小于边界单元。
  5. 如果对于矩阵的所有单元格都满足以上所有条件,则打印“平衡”,否则打印“不平衡”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Define size of matrix
#define N 4
#define M 4
 
// Function to check given matrix
// balanced or unbalanced
string balancedMatrix(int mat[][M])
{
 
    // Flag for check matrix is balanced
    // or unbalanced
    bool is_balanced = true;
 
    // Iterate row until condition is true
    for (int i = 0; i < N && is_balanced; i++) {
 
        // Iterate cols until condition is true
        for (int j = 0; j < M && is_balanced; j++) {
 
            // Check for corner edge elements
            if ((i == 0 || i == N - 1)
                && (j == 0 || j == M - 1)) {
                if (mat[i][j] >= 2)
                    is_balanced = false;
            }
 
            // Check for border elements
            else if (i == 0 || i == N - 1
                     || j == 0 || j == M - 1) {
                if (mat[i][j] >= 3)
                    is_balanced = false;
            }
            else {
 
                // Check for the middle ones
                if (mat[i][j] >= 4)
                    is_balanced = false;
            }
        }
    }
 
    // Return balanced or not
    if (is_balanced)
        return "Balanced";
    else
        return "Unbalanced";
}
 
// Driver Code
int main()
{
    // Given Matrix mat[][]
    int mat[N][M] = { { 1, 2, 3, 4 },
                      { 3, 5, 2, 6 },
                      { 5, 3, 6, 1 },
                      { 9, 5, 6, 0 } };
 
    // Function Call
    cout << balancedMatrix(mat);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
    // Define size of matrix
    static final int N = 4;
    static final int M = 4;
 
    // Function to check given matrix
    // balanced or unbalanced
    static String balancedMatrix(int mat[][])
    {
 
        // Flag for check matrix is balanced
        // or unbalanced
        boolean is_balanced = true;
 
        // Iterate row until condition is true
        for (int i = 0; i < N && is_balanced; i++)
        {
 
            // Iterate cols until condition is true
            for (int j = 0; j < M && is_balanced; j++)
            {
 
                // Check for corner edge elements
                if ((i == 0 || i == N - 1) &&
                    (j == 0 || j == M - 1))
                {
                    if (mat[i][j] >= 2)
                        is_balanced = false;
                }
 
                // Check for border elements
                else if (i == 0 || i == N - 1 ||
                         j == 0 || j == M - 1)
                {
                    if (mat[i][j] >= 3)
                        is_balanced = false;
                }
                else
                {
 
                    // Check for the middle ones
                    if (mat[i][j] >= 4)
                        is_balanced = false;
                }
            }
        }
 
        // Return balanced or not
        if (is_balanced)
            return "Balanced";
        else
            return "Unbalanced";
    }
 
    // Driver Code
    public static void main(String[] args)
    {
       
        // Given Matrix mat[][]
        int mat[][] = {{1, 2, 3, 4},
                       {3, 5, 2, 6},
                       {5, 3, 6, 1},
                       {9, 5, 6, 0}};
 
        // Function Call
        System.out.print(balancedMatrix(mat));
    }
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the above approach
# Define the size of the matrix
N = 4
M = 4
 
# Function to check given matrix
# balanced or unbalanced
def balancedMatrix(mat):
     
    # Flag for check matrix is balanced
    # or unbalanced
    is_balanced = True
     
    # Iterate row until condition is true
    i = 0
    while i < N and is_balanced:
         
        # Iterate cols until condition is true
        j = 0
        while j < N and is_balanced:
             
            # Check for corner edge elements
            if ((i == 0 or i == N - 1) and
                (j == 0 or j == M - 1)):
                if mat[i][j] >= 2:
                    isbalanced = False
             
            # Check for border elements
            elif (i == 0 or i == N - 1 or
                  j == 0 or j == M - 1):
                if mat[i][j] >= 3:
                    is_balanced = False
                     
            # Check for the middle ones
            else:
                if mat[i][j] >= 4:
                    is_balanced = False
             
            j += 1
        i += 1
         
    # Return balanced or not
    if is_balanced:
        return "Balanced"
    else:
        return "Unbalanced"
     
# Driver code
 
# Given matrix mat[][]
mat = [ [ 1, 2, 3, 4 ],
        [ 3, 5, 2, 6 ],
        [ 5, 3, 6, 1 ],
        [ 9, 5, 6, 0 ] ]
 
# Function call
print(balancedMatrix(mat))
 
# This code is contributed by Stuti Pathak


C#
// C# program for the above approach
using System;
class GFG{
 
    // Define size of matrix
    static readonly int N = 4;
    static readonly int M = 4;
 
    // Function to check given matrix
    // balanced or unbalanced
    static String balancedMatrix(int [, ]mat)
    {
        // Flag for check matrix is balanced
        // or unbalanced
        bool is_balanced = true;
 
        // Iterate row until condition is true
        for (int i = 0; i < N && is_balanced; i++)
        {
            // Iterate cols until condition is true
            for (int j = 0; j < M && is_balanced; j++)
            {
 
                // Check for corner edge elements
                if ((i == 0 || i == N - 1) &&
                    (j == 0 || j == M - 1))
                {
                    if (mat[i, j] >= 2)
                        is_balanced = false;
                }
 
                // Check for border elements
                else if (i == 0 || i == N - 1 ||
                         j == 0 || j == M - 1)
                {
                    if (mat[i, j] >= 3)
                        is_balanced = false;
                }
                else
                {
                    // Check for the middle ones
                    if (mat[i, j] >= 4)
                        is_balanced = false;
                }
            }
        }
 
        // Return balanced or not
        if (is_balanced)
            return "Balanced";
        else
            return "Unbalanced";
    }
 
    // Driver Code
    public static void Main(String[] args)
    {     
        // Given Matrix [,]mat
        int [, ]mat = {{1, 2, 3, 4},
                       {3, 5, 2, 6},
                       {5, 3, 6, 1},
                       {9, 5, 6, 0}};
 
        // Function Call
        Console.Write(balancedMatrix(mat));
    }
}
 
// This code is contributed by 29AjayKumar


输出:
Unbalanced





时间复杂度: O(N * M)
辅助空间: O(1)