📜  检查矩阵是否是Bitonic的

📅  最后修改于: 2021-05-19 17:56:07             🧑  作者: Mango

给定一个矩阵m [] [] ,任务是检查给定的矩阵是否是Bitonic的。如果给定的矩阵是Bitonic ,则打印YES 。否则,打印NO

例子:

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

  • 逐一检查矩阵的每一行的元素(如果它们未形成Bitonic序列)。如果发现任何行不是Bitonic,请打印NO
  • 同样,如果每一列的元素是否形成双音序列,则一一检查。如果发现任何行不是Bitonic,请打印NO
  • 如果发现所有行和列都是Bitonic ,则打印YES

下面是上述方法的实现:

C++
// C++ Program to check if a
// matrix is Bitonic or not
#include 
using namespace std;
  
const int N = 3, M = 3;
  
// Function to check if an
// array is Bitonic or not
bool checkBitonic(int arr[], int n)
{
    int i, j, f = 0;
  
    // Check for increasing 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 decreasing 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 (!checkBitonic(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 (!checkBitonic(temp, N)) {
            cout << "NO" << endl;
            return;
        }
    }
  
    cout << "YES";
}
  
// Driver Code
int main()
{
    int m[N][M] = { { 1, 2, 3 },
                    { 3, 4, 5 },
                    { 2, 6, 4 } };
  
    check(m);
  
    return 0;
}


Java
// Java program to check if a
// matrix is Bitonic or not
class GFG{
  
final static int N = 3, M = 3;
  
// Function to check if an
// array is Bitonic or not
static boolean checkBitonic(int arr[], int n)
{
    int i, j, f = 0;
  
    // Check for increasing 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 decreasing 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 (!checkBitonic(arr[i], M))
        {
            System.out.println("NO");
            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 (!checkBitonic(temp, N)) 
        {
            System.out.println("NO");
            return;
        }
    }
    System.out.println("YES");
}
      
// Driver Code
public static void main(String[] args) 
{
    int m[][] = { { 1, 2, 3 },
                  { 3, 4, 5 },
                  { 2, 6, 4 } };
                    
    check(m);
}
}
  
// This code is contributed by rutvik_56


Python3
# Python3 program to check if a
# matrix is Bitonic or not
N = 3
M = 3
  
# Function to check if an 
# array is Bitonic or not
def checkBitonic(arr, n):
  
    i, j, f = 0, 0, 0
  
    # Check for increasing 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 decreasing 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 checkBitonic(arr[i], M)):
            print("NO")
            return
  
    # Check column wise
    i = 0
    for i in range(N):
  
        # Generate an array consisting
        # of elements of current column
        temp = [0] * N
        for j in range(N):
            temp[j] = arr[j][i]
  
        if (not checkBitonic(temp, N)):
            print("NO")
            return
  
    print("YES")
  
# Driver Code
if __name__ == '__main__':
      
    m = [ [ 1, 2, 3 ],
          [ 3, 4, 5 ],
          [ 2, 6, 4 ] ]
  
    check(m)
  
# This code is contributed by himanshu77


C#
// C# program to check if a
// matrix is Bitonic or not
using System;
class GFG{
  
readonly static int N = 3, M = 3;
  
// Function to check if an
// array is Bitonic or not
static bool checkBitonic(int []arr, int n)
{
    int i, j, f = 0;
  
    // Check for increasing 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 decreasing 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 (!checkBitonic(GetRow(arr, i), M))
        {
            Console.WriteLine("NO");
            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 (!checkBitonic(temp, N)) 
        {
            Console.WriteLine("NO");
            return;
        }
    }
    Console.WriteLine("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 = { { 1, 2, 3 },
                 { 3, 4, 5 },
                 { 2, 6, 4 } };
                    
    check(m);
}
}
  
// This code is contributed by sapnasingh4991


输出:
YES

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