在矩阵c ++中查找所有鞍点

时间:2017-04-29 17:13:40

标签: c++

我正在编写一个代码,用于查找矩阵中的所有鞍点。它们在行中最小,在列中最大,在行中最大,在列中最小,属于(我的大学)马鞍点的定义。作为一个初学者,我设法完成了一半的工作(通过复制我们在课堂上所做的部分并自己打字,找到其行中最小的马鞍点和列中最大的马鞍点)。我已经坚持了很长一段时间,并且无法想出如何在程序中添加行中最大且列数最小的马鞍点。

这是我到目前为止所做的:

#include <iostream>
#include <cstdlib>
using namespace std;

int a[10][10];
int x, y;

int pos_max(int j) //saddle points check
{
    int max = 0;
    for (int i = 1; i <= x - 1; i++) {
        if (a[i][j] > a[max][j]) {
            max = i;
        }
    }
    return max;
}

int main() {
    cout << "Enter the number of rows: ";
    cin >> x;
    cout << "Enter the number of columns: ";
    cin >> y;
    cout << "----------------------------" << endl;

    for (int i = 0; i <= x - 1; i++) //input of the matrix
        for (int j = 0; j <= y - 1; j++) {
            cout << "a[" << i + 1 << ", " << j + 1 << "] = ";
            cin >> a[i][j];
        }
    cout << "----------------------------\n";
    for (int i = 0; i <= x - 1; i++) //visualization of the matrix
    {
        for (int j = 0; j <= y - 1; j++)
            cout << a[i][j] << " ";
        cout << endl;
    }
    cout << "----------------------------\n";

    int r;
    int flag = 0;
    int i = y;

    for (int j = 0; j <= y - 1; j++) {
        r = pos_max(j);
        for (i = 0; i <= y - 1; i++) {
            if (a[r][i] < a[r][j]) {
                break;
            }
        }
        if (i == y) {
            cout << "Saddle points are: ";
            cout << "a[" << r + 1 << ", " << j + 1 << "] = " << a[r][j] << "\n";
            flag = 1;
        }
    }
    if (flag == 0) {
        cout << "No saddle points\n";
    }
    cout << "----------------------------\n";
    return 0;
}

3 个答案:

答案 0 :(得分:0)

首先,您的代码存在逻辑错误。在pos_max函数中,它将返回列中最大元素的索引。可能存在这样的情况,即列中存在多个具有相同值的最大值,但是,它返回行中不是最小值的那个,因此您的程序无法打印该鞍点。 / p>

要解决此问题,您可以返回列中最大的所有索引的数组,然后检查这些点中的每个点是否在各自的列中最小,但我认为它们是不是很优雅的解决方案。在任何情况下,您将再次编写鞍点的其他条件的整个代码,列中的最小值和行中的最大值。

因此,我建议改变策略。您创建了4个数组max_rowmax_colmin_rowmin_col,其中每个数组分别存储该行/列中的最小值/最大值。然后你可以遍历数组并检查该点是否满足鞍点条件。

以下是代码:

#include <iostream>
#include <cstdlib>
using namespace std;

int a[10][10];
int max_row[10], max_col[10], min_row[10], min_col[10];
int x, y;

bool is_saddle(int i, int j) {
    int x = a[i][j];
    return (max_row[i] == x && min_col[j] == x) || (min_row[i] == x && max_col[j] == x);
}

int main() {
    /* code to input x, y and the matrix
    ...
    */

    /* code to visualize the matrix
    ...
    */

    /* populating max and min arrays */
    for (int i = 0; i <= x-1; ++i) {
        max_row[i] = a[i][0], min_row[i] = a[i][0];
        for (int j = 0; j <= y-1; ++j) {
            max_row[i] = max(max_row[i], a[i][j]);
            min_row[i] = min(min_row[i], a[i][j]);
        }
    }

    for (int j = 0; j <= y-1; ++j) {
        max_col[j] = a[0][j], min_col[j] = a[0][j];
        for (int i = 0; i <= x-1; ++i) {
            max_col[j] = max(max_col[j], a[i][j]);
            min_col[j] = min(min_col[j], a[i][j]);
        }
    }

    /* Check for saddle point */
    for (int i = 0; i <= x-1; ++i) {
        for (int j = 0; j <= y-1; ++j) {
            if (is_saddle(i, j)) {
                cout << "Saddle points are: ";
                cout << "a[" << i + 1 << ", " << j + 1 << "] = " << a[i][j] << "\n";
                flag = 1;
            }
        }
    }

    if (flag == 0) {
        cout << "No saddle points\n";
    }
    cout << "----------------------------\n";
    return 0;
}

答案 1 :(得分:0)

#include <iostream>
using namespace std;
int getMaxInRow(int[][5], int, int, int);
int getMinInColumn(int[][5], int, int, int);
void getSaddlePointCordinates(int [][5],int ,int );
void getInputOf2dArray(int a[][5], int, int);
int main()
{
    int a[5][5] ;
    int rows, columns;
    cin >> rows >> columns;
    getInputOf2dArray(a, 5, 5);
    getSaddlePointCordinates(a,rows,columns);
}
void getInputOf2dArray(int a[][5], int rows, int columns)
{
    for (int i = 0; i < rows; i = i + 1)
    {
        for (int j = 0; j < columns; j = j + 1)
        {
            cin >> a[i][j];
        }
    }
}
void getSaddlePointCordinates(int a[][5],int rows,int columns)
{
    int flag = 0;
    for (int rowNo = 0; rowNo < 5; rowNo++)
    {
        for (int columnNo = 0; columnNo < 5; columnNo++)
        {
            if (getMaxInRow(a, rows, columns, rowNo) == getMinInColumn(a, rows, columns, columnNo))
            {
                flag = 1;
                cout << rowNo << columnNo;
            }
        }
    }
    if (flag == 0)
        cout << "no saddle point";
    cout << "\n";
}
int getMaxInRow(int a[][5], int row, int column, int rowNo)
{

    int max = a[rowNo][0];
    for (int i = 1; i < column; i = i + 1)
    {

        if (a[rowNo][i] > max)
            max = a[rowNo][i];
    }
    return max;
}
int  getMinInColumn(int a[][5], int row, int column, int columnNo)
{
    int min = a[0][columnNo];
    for (int i = 1; i < row; i = i + 1)
    {
        if (a[i][columnNo] < min)
            min = a[i][columnNo];
    }
    return min;
}

答案 2 :(得分:-1)

只需使用引用arr(ref [size])//记忆方法来检查其中的最小值和最大值。

这是时间复杂度为O(n * n)和空间复杂度为O(n)的代码实现:

#include <bits/stdc++.h>

using namespace std;
#define size 5

void util(int arr[size][size], int *count)
{
    int ref[size]; // array to hold all the max values of row's.

    for(int r = 0; r < size; r++)
    {
        int max_row_val = arr[r][0];
        for(int c = 1; c < size; c++)
        {
            if(max_row_val < arr[r][c])
                max_row_val = arr[r][c];
        }
        ref[r] = max_row_val;
    }

    for(int c = 0; c < size; c++) 
    {
        int min_col_val = arr[0][c];
        for(int r = 1; r < size; r++) // min_val of the column
        {   
            if(min_col_val > arr[r][c])
                min_col_val = arr[r][c];
        }
        for(int r = 0; r < size; r++) // now search if the min_val of col and the ref[r] is same and the position is same, if both matches then print.
        {   
            if(min_col_val == ref[r] && min_col_val == arr[r][c])
            {
                *count += 1;
                if((*count) == 1)
                    cout << "The cordinate's are: \n";
                cout << "(" << r << "," << c << ")" << endl;
            }
        }
    }   
}

// Driver function
int main()
{
    int arr[size][size];
    for(int i = 0; i < size; i++)
    {
        for(int j = 0; j < size; j++)
            cin >> arr[i][j];
    }
    int count = 0;
    util(arr, &count);
    if(!count)
        cout << "No saddle points" << endl;
}


// Test case -> Saddle Point
/*
Input1: 
1 2 3 4 5
6 7 8 9 10
1 2 3 4 5
6 7 8 9 10
0 2 3 4 5
Output1: 
The cordinate's are:
(0,4)
(2,4)
(4,4)
Input2:
1 2 3 4 5
6 7 8 9 1
10 11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
Output2:
No saddle points
*/