📌  相关文章
📜  检查矩阵是否满足给定条件

📅  最后修改于: 2021-05-31 23:52:38             🧑  作者: Mango

给定矩阵mat [] [] ,任务是检查矩阵是否有效。有效矩阵是满足给定条件的矩阵:

  1. 对于每一行,它必须包含一个不同的字符。
  2. 没有两个连续的行具有相同的字符。

例子:

方法:首先检查第一行是否包含相同的字符。如果它包含相同的字符,则迭代第二行并将当前行的字符与上一行的第一个元素进行比较;如果两个元素相等或当前行的字符不同,则返回false。对所有连续的行重复上述过程。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Storing the size of the matrix
const int M = 5, N = 5;
  
// Function that returns true if
// the matrix is valid
bool isPerfect(char board[M][N + 1])
{
    char m;
  
    for (int i = 0; i < M; i++) {
  
        // Get the current row
        string s = board[i];
  
        // Comparing first element
        // of the row with the element
        // of previous row
        if (i > 0 && s[0] == m)
            return false;
  
        // Checking if all the characters of the
        // current row are same or not
        for (int j = 0; j < N; j++) {
  
            // Storing the first character
            if (j == 0)
                m = s[0];
  
            // Comparing all the elements
            // with the first element
            else {
                if (m != s[j])
                    return false;
            }
        }
    }
  
    return true;
}
  
// Driver code
int main()
{
    char board[M][N + 1] = { "88888",
                             "44444",
                             "66666",
                             "55555",
                             "88888" };
  
    if (isPerfect(board))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of the approach 
class GFG 
{
      
    // Storing the size of the matrix 
    static final int M = 5, N = 5; 
      
    // Function that returns true if 
    // the matrix is valid 
    static boolean isPerfect(String board[]) 
    { 
        char m = 0;
      
        for (int i = 0; i < M; i++)
        { 
      
            // Get the current row 
            String s = board[i]; 
      
            // Comparing first element 
            // of the row with the element 
            // of previous row 
            if (i > 0 && s.charAt(0) == m) 
                return false; 
      
            // Checking if all the characters of the 
            // current row are same or not 
            for (int j = 0; j < N; j++) 
            { 
      
                // Storing the first character 
                if (j == 0) 
                    m = s.charAt(0); 
      
                // Comparing all the elements 
                // with the first element 
                else 
                { 
                    if (m != s.charAt(j)) 
                        return false; 
                } 
            } 
        } 
        return true; 
    } 
      
    // Driver code 
    public static void main (String[] args) 
    { 
        String board[] = { "88888", "44444", 
                           "66666", "55555", 
                           "88888" }; 
      
        if (isPerfect(board)) 
            System.out.println("Yes"); 
        else
            System.out.println("No"); 
    } 
}
      
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
  
# Storing the size of the matrix
M = 5
N = 5
  
# Function that returns true if
# the matrix is valid
def isPerfect(board):
    m = ''
  
    for i in range(M):
  
        # Get the current row
        s = board[i]
  
        # Comparing first element
        # of the row with the element
        # of previous row
        if (i > 0 and s[0] == m):
            return False
  
        # Checking if all the characters of the
        # current row are same or not
        for j in range(N):
  
            # Storing the first character
            if (j == 0):
                m = s[0]
  
            # Comparing all the elements
            # with the first element
            else:
                if (m != s[j]):
                    return False
  
    return True
  
# Driver code
board = ["88888",
         "44444",
         "66666",
         "55555",
         "88888"]
  
if (isPerfect(board)):
    print("Yes")
else:
    print("No")
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
      
class GFG 
{
      
    // Storing the size of the matrix 
    static readonly int M = 5, N = 5; 
      
    // Function that returns true if 
    // the matrix is valid 
    static bool isPerfect(String []board) 
    { 
        char m = '0';
      
        for (int i = 0; i < M; i++)
        { 
      
            // Get the current row 
            String s = board[i]; 
      
            // Comparing first element 
            // of the row with the element 
            // of previous row 
            if (i > 0 && s[0] == m) 
                return false; 
      
            // Checking if all the characters of the 
            // current row are same or not 
            for (int j = 0; j < N; j++) 
            { 
      
                // Storing the first character 
                if (j == 0) 
                    m = s[0]; 
      
                // Comparing all the elements 
                // with the first element 
                else
                { 
                    if (m != s[j]) 
                        return false; 
                } 
            } 
        } 
        return true; 
    } 
      
    // Driver code 
    public static void Main (String[] args) 
    { 
        String []board = { "88888", "44444", 
                           "66666", "55555", 
                           "88888" }; 
      
        if (isPerfect(board)) 
            Console.WriteLine("Yes"); 
        else
            Console.WriteLine("No"); 
    } 
}
  
// This code is contributed by Rajput-Ji


输出:
Yes

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。