📜  矩阵中的鞍点

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

矩阵中的鞍点

给定一个 nxn 大小的矩阵,任务是找到矩阵的鞍点。鞍点是矩阵的一个元素,它在其行中是最小元素,在其列中是最大值。
例子 :

Input: Mat[3][3] = { {1, 2, 3},
                  {4, 5, 6},
                  {7, 8, 9}}
Output: 7
7 is minimum in its row and maximum in its column.

Input: Mat[3][3] = {{1, 2, 3},
                    {4, 5, 6},
                    {10, 18, 4}}
Output: No saddle point

一个简单的解决方案是一个一个地遍历所有矩阵元素并检查该元素是否为鞍点。
一个有效的解决方案基于以下步骤。
逐行遍历所有行并对每一行 i 执行以下操作。

  1. 查找当前行的最小元素并存储最小元素的列索引。
  2. 检查行最小元素在其列中是否也是最大值。我们在这里使用存储的列索引。
  3. 如果是,则鞍点 else 一直持续到矩阵的末尾。

下面是上述步骤的实现。

C++
// C++ program to illustrate Saddle point
#include 
using namespace std;
 
const int MAX = 100;
 
// Function to find saddle point
bool findSaddlePoint(int mat[MAX][MAX], int n)
{
    // Process all rows one by one
    for (int i = 0; i < n; i++)
    {
        // Find the minimum element of row i.
        // Also find column index of the minimum element
        int min_row = mat[i][0], col_ind = 0;
        for (int j = 1; j < n; j++)
        {
            if (min_row > mat[i][j])
            {
                min_row = mat[i][j];
                col_ind = j;
            }
        }
 
        // Check if the minimum element of row is also
        // the maximum element of column or not
        int k;
        for (k = 0; k < n; k++)
 
            // Note that col_ind is fixed
            if (min_row < mat[k][col_ind])
                break;
 
        // If saddle point is present in this row then
        // print it
        if (k == n)
        {
           cout << "Value of Saddle Point " << min_row;
           return true;
        }
    }
 
    // If Saddle Point not found
    return false;
}
 
// Driver code
int main()
{
    int mat[MAX][MAX] = {{1, 2, 3},
                        {4, 5, 6},
                        {7, 8, 9}};
    int n = 3;
    if (findSaddlePoint(mat, n) == false)
       cout << "No Saddle Point ";
    return 0;
}


Java
// Java program to illustrate Saddle point
 
class Test
{
    // Method to find saddle point
    static boolean findSaddlePoint(int mat[][    ], int n)
    {
        // Process all rows one by one
        for (int i = 0; i < n; i++)
        {
            // Find the minimum element of row i.
            // Also find column index of the minimum element
            int min_row = mat[i][0], col_ind = 0;
            for (int j = 1; j < n; j++)
            {
                if (min_row > mat[i][j])
                {
                    min_row = mat[i][j];
                    col_ind = j;
                }
            }
      
            // Check if the minimum element of row is also
            // the maximum element of column or not
            int k;
            for (k = 0; k < n; k++)
      
                // Note that col_ind is fixed
                if (min_row < mat[k][col_ind])
                    break;
      
            // If saddle point is present in this row then
            // print it
            if (k == n)
            {
               System.out.println("Value of Saddle Point " + min_row);
               return true;
            }
        }
      
        // If Saddle Point not found
        return false;
    }
     
    // Driver method
    public static void main(String[] args)
    {
        int mat[][] = {{1, 2, 3},
                      {4, 5, 6},
                     {7, 8, 9}};
         
        int n = 3;
        if (findSaddlePoint(mat, n) == false)
            System.out.println("No Saddle Point ");
    }
}


Python3
# Python3 program to illustrate
# Saddle point
 
# Method to find saddle point
def findSaddlePoint(mat, n):
   
    # Process all rows one
    # by one
    for i in range(n):
       
        # Find the minimum element
        # of row i.
        # Also find column index of
        # the minimum element
        min_row = mat[i][0];
        col_ind = 0;
        for j in range(1, n):
            if (min_row > mat[i][j]):
                min_row = mat[i][j];
                col_ind = j;
 
        # Check if the minimum element
        # of row is also the maximum
        # element of column or not
        k = 0;
        for k in range(n):
 
            # Note that col_ind is fixed
            if (min_row < mat[k][col_ind]):
                break;
            k += 1;
 
        # If saddle point present in this
        # row then print
        if (k == n):
            print("Value of Saddle Point ",
                  min_row);
            return True;
 
    # If Saddle Point found
    return False;
 
# Driver method
if __name__ == '__main__':
   
    mat = [[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9]];
 
    n = 3;
    if (findSaddlePoint(mat, n) ==
        False):
        print("No Saddle Po");
 
# This code is contributed by 29AjayKumar


C#
// C# program to illustrate Saddle point
using System;
 
class GFG {
     
    // Method to find saddle point
    static bool findSaddlePoint(int [,] mat,
                                int n)
    {
         
        // Process all rows one by one
        for (int i = 0; i < n; i++)
        {
             
            // Find the minimum element of
            // row i. Also find column index
            // of the minimum element
            int min_row = mat[i, 0], col_ind = 0;
            for (int j = 1; j < n; j++)
            {
                if (min_row > mat[i, j])
                {
                    min_row = mat[i, j];
                    col_ind = j;
                }
            }
     
            // Check if the minimum element
            // of row is also the maximum
            // element of column or not
            int k;
            for (k = 0; k < n; k++)
     
                // Note that col_ind is fixed
                if (min_row < mat[k, col_ind])
                    break;
     
            // If saddle point is present in this row then
            // print it
            if (k == n)
            {
                Console.WriteLine("Value of Saddle Point "
                                                + min_row);
                return true;
            }
        }
     
        // If Saddle Point not found
        return false;
    }
     
    // Driver code
    public static void Main()
    {
        int [,] mat = {{1, 2, 3},
                       {4, 5, 6},
                       {7, 8, 9}};
         
        int n = 3;
        if (findSaddlePoint(mat, n) == false)
            Console.WriteLine("No Saddle Point ");
    }
}
 
// This code is contributed by KRV.


PHP
 $mat[$i][$j])
            {
                $min_row = $mat[$i][$j];
                $col_ind = $j;
            }
        }
 
        // Check if the minimum element of
        // row is also the maximum element
        // of column or not
        $k;
        for ($k = 0; $k < $n; $k++)
 
            // Note that col_ind is fixed
            if ($min_row < $mat[$k][$col_ind])
                break;
 
        // If saddle point is present in
        // this row then print it
        if ($k == $n)
        {
        echo "Value of Saddle Point " ,
                              $min_row;
        return true;
        }
    }
 
    // If Saddle Point not found
    return false;
}
 
// Driver code
$mat = array(array(1, 2, 3),
             array(4, 5, 6),
             array (7, 8, 9));
$n = 3;
if (findSaddlePoint($mat, $n) == false)
echo "No Saddle Point ";
 
// This code is contributed by anuj_67.
?>


Javascript


输出 :

Value of Saddle Point 7