📜  检查矩阵是否为反向双音

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

给定一个矩阵m [] [] ,任务是检查给定的矩阵是否为Reverse Bitonic。如果给定的矩阵是反向双音,则打印“是” 。否则,打印No。

例子

方法
请按照以下步骤解决问题:

  • 逐一检查矩阵每一行的元素,如果它们形成一个 反双音 顺序与否。如果发现任何行不是“反向双向发音” ,请打印“否”。
  • 同样,如果每一列的元素是否形成了反向双音序列,则应一一检查。如果发现任何行不是“反向双向发音” ,请打印“否”。
  • 如果发现所有行和列都是Reverse Bitonic ,则打印“是”。

下面是上述方法的实现:

C++
// C++ Program to check if a
// matrix is Reverse Bitonic or not
#include 
using namespace std;
  
const int N = 3, M = 3;
  
// Function to check if an
// array is Reverse Bitonic or not
bool checkReverseBitonic(int arr[], int n)
{
    int i, j, f = 0;
  
    // Check for decreasing sequence
    for (i = 1; i < n; i++) {
        if (arr[i] < arr[i - 1])
            continue;
  
        if (arr[i] == arr[i - 1])
            return false;
  
        else {
            f = 1;
            break;
        }
    }
  
    if (i == n)
        return true;
  
    // Check for increasing sequence
    for (j = i + 1; j < n; j++) {
        if (arr[j] > arr[j - 1])
            continue;
  
        if (arr[i] == arr[i - 1])
            return false;
  
        else {
            if (f == 1)
                return false;
        }
    }
  
    return true;
}
  
// Function to check whether given
// matrix is bitonic or not
void check(int arr[N][M])
{
    int f = 0;
  
    // Check row-wise
    for (int i = 0; i < N; i++) {
        if (!checkReverseBitonic(arr[i], M)) {
            cout << "No" << endl;
            return;
        }
    }
  
    // Check column wise
    for (int i = 0; i < N; i++) {
        // Generate an array
        // consisting of elements
        // of the current column
        int temp[N];
  
        for (int j = 0; j < N; j++) {
            temp[j] = arr[j][i];
        }
  
        if (!checkReverseBitonic(temp, N)) {
            cout << "No" << endl;
            return;
        }
    }
  
    cout << "Yes";
}
  
// Driver Code
int main()
{
    int m[N][M] = { { 2, 3, 4 },
                    { 1, 2, 3 },
                    { 4, 5, 6 } };
  
    check(m);
  
    return 0;
}


Java
// Java Program to check if a
// matrix is Reverse Bitonic or not
import java.util.*;
class GFG{
  
static int N = 3, M = 3;
  
// Function to check if an
// array is Reverse Bitonic or not
static boolean checkReverseBitonic(int arr[], int n)
{
    int i, j, f = 0;
  
    // Check for decreasing sequence
    for (i = 1; i < n; i++)
    {
        if (arr[i] < arr[i - 1])
            continue;
  
        if (arr[i] == arr[i - 1])
            return false;
  
        else 
        {
            f = 1;
            break;
        }
    }
  
    if (i == n)
        return true;
  
    // Check for increasing sequence
    for (j = i + 1; j < n; j++) 
    {
        if (arr[j] > arr[j - 1])
            continue;
  
        if (arr[i] == arr[i - 1])
            return false;
  
        else
        {
            if (f == 1)
                return false;
        }
    }
  
    return true;
}
  
// Function to check whether given
// matrix is bitonic or not
static void check(int arr[][])
{
    int f = 0;
  
    // Check row-wise
    for (int i = 0; i < N; i++) 
    {
        if (!checkReverseBitonic(arr[i], M))
        {
            System.out.print("No" + "\n");
            return;
        }
    }
  
    // Check column wise
    for (int i = 0; i < N; i++) 
    {
        // Generate an array
        // consisting of elements
        // of the current column
        int temp[] = new int[N];
  
        for (int j = 0; j < N; j++) 
        {
            temp[j] = arr[j][i];
        }
  
        if (!checkReverseBitonic(temp, N)) 
        {
            System.out.print("No" + "\n");
            return;
        }
    }
    System.out.print("Yes");
}
  
// Driver Code
public static void main(String[] args)
{
    int m[][] = { { 2, 3, 4 },
                  { 1, 2, 3 },
                  { 4, 5, 6 } };
  
    check(m);
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 program to check if a
# matrix is Reverse Bitonic or not
N = 3
M = 3
  
# Function to check if an
# array is Reverse Bitonic or not
def checkReverseBitonic(arr, n):
  
    f = 0
  
    # Check for decreasing sequence
    for i in range(1, n):
        if (arr[i] < arr[i - 1]):
            continue
  
        if (arr[i] == arr[i - 1]):
            return False
  
        else:
            f = 1
            break
  
    if (i == n):
        return True
  
    # Check for increasing sequence
    for j in range(i + 1, n):
        if (arr[j] > arr[j - 1]):
            continue
  
        if (arr[i] == arr[i - 1]):
            return False
  
        else:
            if (f == 1):
                return False
      
    return True
  
# Function to check whether given
# matrix is bitonic or not
def check(arr):
  
    f = 0
  
    # Check row-wise
    for i in range (N):
        if (not checkReverseBitonic(arr[i], M)): 
            print("No")
            return
  
    # Check column wise
    for i in range(N):
          
        # Generate an array
        # consisting of elements
        # of the current column
        temp = [0] * N
  
        for j in range(N):
            temp[j] = arr[j][i]
  
        if (not checkReverseBitonic(temp, N)):
            print("No")
            return
  
    print("Yes")
  
# Driver Code
if __name__ == "__main__":
  
    m = [ [ 2, 3, 4 ],
          [ 1, 2, 3 ],
          [ 4, 5, 6 ] ]
  
    check(m)
  
# This code is contributed by chitranayal


C#
// C# Program to check if a
// matrix is Reverse Bitonic or not
using System;
class GFG{
  
static int N = 3, M = 3;
  
// Function to check if an
// array is Reverse Bitonic or not
static bool checkReverseBitonic(int []arr, int n)
{
    int i, j, f = 0;
  
    // Check for decreasing sequence
    for (i = 1; i < n; i++)
    {
        if (arr[i] < arr[i - 1])
            continue;
  
        if (arr[i] == arr[i - 1])
            return false;
  
        else 
        {
            f = 1;
            break;
        }
    }
  
    if (i == n)
        return true;
  
    // Check for increasing sequence
    for (j = i + 1; j < n; j++) 
    {
        if (arr[j] > arr[j - 1])
            continue;
  
        if (arr[i] == arr[i - 1])
            return false;
  
        else
        {
            if (f == 1)
                return false;
        }
    }
  
    return true;
}
  
// Function to check whether given
// matrix is bitonic or not
static void check(int [,]arr)
{
    // Check row-wise
    for (int i = 0; i < N; i++) 
    {
        if (!checkReverseBitonic(GetRow(arr, i), M))
        {
            Console.Write("No" + "\n");
            return;
        }
    }
  
    // Check column wise
    for (int i = 0; i < N; i++) 
    {
        // Generate an array
        // consisting of elements
        // of the current column
        int []temp = new int[N];
  
        for (int j = 0; j < N; j++) 
        {
            temp[j] = arr[j,i];
        }
  
        if (!checkReverseBitonic(temp, N)) 
        {
            Console.Write("No" + "\n");
            return;
        }
    }
    Console.Write("Yes");
}
    
public static int[] GetRow(int[,] matrix, int row)
{
    var rowLength = matrix.GetLength(1);
    var rowVector = new int[rowLength];
  
    for (var i = 0; i < rowLength; i++)
      rowVector[i] = matrix[row, i];
  
    return rowVector;
}
    
// Driver Code
public static void Main(String[] args)
{
    int [,]m = {{ 2, 3, 4 },
                { 1, 2, 3 },
                { 4, 5, 6 }};
  
    check(m);
}
}
  
// This code is contributed by Rajput-Ji


输出:
Yes

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