📜  检查矩阵是否为二元矩阵的程序

📅  最后修改于: 2022-05-13 01:57:21.268000             🧑  作者: Mango

检查矩阵是否为二元矩阵的程序

给定一个矩阵,任务是检查该矩阵是否为Binary Matrix 。二元矩阵是所有元素为0 或 1 的矩阵它也称为逻辑矩阵、布尔矩阵、关系矩阵。
例子:

Input: 
{{1, 0, 1, 1},
{0, 1, 0, 1}
{1, 1, 1, 0}}
Output: Yes

Input:
{{1, 0, 1, 1},
{1, 2, 0, 1},
{0, 0, 1, 1}}
Output: No

方法:遍历矩阵,检查每个元素是否为0或1。如果有除0和1之外的任何元素,则打印No else print Yes。
下面是上述方法的实现:

C++
// C++ code to check if a matrix
// is binary matrix or not.
#include 
using namespace std;
 
#define M 3
#define N 4
 
// function to check if a matrix
// is binary matrix or not
bool isBinaryMatrix(int mat[][N])
{
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) {
            // Returns false if element is other than 0 or 1.
            if (!(mat[i][j] == 0 || mat[i][j] == 1))
                return false;
        }
    }
 
    // Returns true if all the elements
    // are either 0 or 1.
    return true;
}
 
// Driver code
int main()
{
    int mat[M][N] = { { 1, 0, 1, 1 },
                      { 0, 1, 0, 1 },
                      { 1, 1, 1, 0 } };
 
    if (isBinaryMatrix(mat))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// JAVA code to check if a matrix
// is binary matrix or not.
 
import java.io.*;
class GFG {
    static int M = 3;
    static int N = 4;
 
    // function to check if a matrix is binary matrix
    // or not
    static boolean isBinaryMatrix(int mat[][])
    {
        for (int i = 0; i < M; i++) {
            for (int j = 0; j < N; j++) {
                // Returns false if element is other than 0 or 1.
                if (!(mat[i][j] == 0 || mat[i][j] == 1))
                    return false;
            }
        }
 
        // Returns true if all the elements
        // are either 0 or 1.
        return true;
    }
 
    // Driver code
    public static void main(String args[])
    {
        int mat[][] = { { 1, 0, 1, 1 },
                        { 0, 1, 0, 1 },
                        { 1, 1, 1, 0 } };
 
        if (isBinaryMatrix(mat))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}


Python3
# Python3 code to check if a matrix
# is binary matrix or not.
 
M = 3;
N = 4;
 
# function to check if a matrix
# is binary matrix or not
def isBinaryMatrix(mat):
    for i in range(M):
        for j in range(N):
            # Returns false if element
            # is other than 0 or 1.
            if ((mat[i][j] == 0 or mat[i][j] == 1)==False):
                return False;
 
    # Returns true if all the
    # elements are either 0 or 1.
    return True;
 
# Driver code
if __name__=='__main__':
    mat = [[ 1, 0, 1, 1 ],[0, 1, 0, 1 ],[ 1, 1, 1, 0 ]];
 
    if (isBinaryMatrix(mat)):
        print("Yes");
    else:
        print("No");
 
# This code is contributed by mits


C#
// C# code to check if a matrix
// is binary matrix or not.
 
using System;
class GFG {
    static int M = 3;
    static int N = 4;
 
    // function to check if a matrix is binary matrix
    // or not
    static bool isBinaryMatrix(int [,]mat)
    {
        for (int i = 0; i < M; i++) {
            for (int j = 0; j < N; j++) {
                // Returns false if element is other than 0 or 1.
                if (!(mat[i,j] == 0 || mat[i,j] == 1))
                    return false;
            }
        }
 
        // Returns true if all the elements
        // are either 0 or 1.
        return true;
    }
 
    // Driver code
    public static void Main()
    {
        int [,]mat = { { 1, 0, 1, 1 },
                        { 0, 1, 0, 1 },
                        { 1, 1, 1, 0 } };
 
        if (isBinaryMatrix(mat))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
// This code is contributed by anuj_67.


PHP


Javascript


输出:
Yes

时间复杂度: O(MXN)
空间复杂度: O(1)