为什么它无法出正确答案?

img

img


为什么它无法出正确答案?
我先在整体行为0时,判断所有列中数最大的,再在其基础上判断同列不同行中取最小的,如果有就出来了,可是为什么时错的


// Requires C++17

#include <iostream>
#include <vector>

template <typename T>
class Matrix
{
public:
    typedef T value_type;
    typedef std::size_t size_type;

    Matrix(size_type m, size_type n) : _m(m), _n(n), _data(m * n) {}

    constexpr size_type num_rows() const { return _m; }
    constexpr size_type num_cols() const { return _n; }

    constexpr value_type operator()(size_type i, size_type j) const { return _data[i * _n + j]; }
    constexpr value_type &operator()(size_type i, size_type j) { return _data[i * _n + j]; }

    std::vector<std::pair<size_type, size_type>> find_saddle_points() const
    {
        std::vector<std::pair<size_type, size_type>> result;
        for (size_type i = 0; i < _m; i++)
            for (size_type j = 0; j < _n; j++)
                if (is_max_element_at_row(i, j) && is_min_element_at_col(i, j))
                    result.push_back({i, j});
        return result;
    }

private:
    constexpr bool is_max_element_at_row(size_type i, size_type j) const
    {
        auto x = (*this)(i, j);
        for (size_type k = 0; k < _n; k++)
        {
            if ((*this)(i, k) > x)
                return false;
        }
        return true;
    }

    constexpr bool is_min_element_at_col(size_type i, size_type j) const
    {
        auto x = (*this)(i, j);
        for (size_type k = 0; k < _m; k++)
            if ((*this)(k, j) < x)
                return false;
        return true;
    }

    size_type _m;
    size_type _n;
    std::vector<value_type> _data;
};

template <typename CharT, typename Traits, typename T>
std::basic_istream<CharT, Traits> &operator>>(std::basic_istream<CharT, Traits> &is, Matrix<T> &matrix)
{
    for (std::size_t i = 0; i < matrix.num_rows(); i++)
        for (std::size_t j = 0; j < matrix.num_cols(); j++)
            is >> matrix(i, j);
    return is;
}

int main()
{
    std::size_t m, n;
    std::cin >> m >> n;
    Matrix<int> matrix(m, n);
    std::cin >> matrix;
    auto saddle_points = matrix.find_saddle_points();
    if (saddle_points.empty())
        std::cout << "no" << std::endl;
    else
        for (auto [i, j] : saddle_points)
            std::cout << "a[" << i << "][" << j << "]=" << matrix(i, j) << std::endl;
    return 0;
}