试图在编码问题中理解此结构而迷失

Lost in trying to understand this Struct in coding problem

本文关键字:结构 迷失 编码 问题      更新时间:2023-10-16

我有一个小的编码问题,其中涉及在被0的矩阵包围的正方形矩阵中找到最大的子值。

我了解计算最大子值问题的算法。但是,我很难理解源代码。结构正在创建正方形,我对结构中的语句和变量(例如size_data_等(感到困惑。

此外,使用struct Square提供了main()功能中的Square s,因此我必须了解该结构如何工作,然后才能与Square s一起使用以找到方形矩阵中最大的子值。

我也不确定如何以其格式读取矩阵Square s的值。我想改用2D数组表示。

要了解outRowoutColoutSizefindSquare()中的意思是什么,我尝试使用cout将它们打印出来,但是我分别获得了值4197501、0和0的值。我不知道为什么或如何。

/******************************************************************************
Given a square matrix with values representing pixel colors black (0) and
white (1), implement a function that finds the largest subsquare with all four
borders filled with black pixels. The function should compute three values:
row and column of the top left corner of the subsquare, and its side length.
If no such subsquare exists, the return side length should be set to 0.
Note:
     - Do not change 'findSquare' function signature.
******************************************************************************/
#include <cmath>
#include <memory>
#include <iostream>
using namespace std;
// Square is defined as follows:
struct Square
{
public:
    explicit Square(size_t size) : size_(size), data_(new int[size * size])
    { }
    Square(std::initializer_list<int> init) : size_(init.size()), data_()
    {
        size_ = std::sqrt(init.size());
        if (init.size() != (size_ * size_))
            throw std::invalid_argument("Not enough initializer elements to complete the square");
        data_.reset(new int[size_ * size_]);
        std::move(std::begin(init), std::end(init), &data_[0]);
    }
    int& operator()(size_t row, size_t col)
    {
        if (row >= size_ || col >= size_)
            throw std::out_of_range("OOB");
        return data_[size_ * row + col];
    }
    const int& operator()(size_t row, size_t col) const
    {
        if (row >= size_ || col >= size_)
            throw std::out_of_range("OOB");
        return data_[size_ * row + col];
    }
    size_t Size() const { return size_; }
private:
    size_t size_;
    std::unique_ptr<int[]> data_;
};
void findSquare(const Square& square, size_t& outRow, size_t& outCol, size_t& outSize)
{
    // your code here
    // My code. Trying to understand what outRow, outCol, and outSize represent
    cout << "This is row: " << outRow << 'n';
    cout << "This is col: " << outCol << 'n';
    cout << "This is size: " << outSize << 'n';
}
int main()
{
        Square s
        {
         1, 0, 0, 0,
         1, 0, 0, 0,
         1, 0, 0, 0,
         1, 1, 1, 0
        };
    size_t row, col, size;
    findSquare(s, row, col, size);
}

outRow outColoutSize输出参数。当调用函数时,它们没有意义(这就是为什么您获得垃圾值的原因(。当您计算结果时,他们在那里供您分配。

outSize = size of largest subsquare
outRow = row of largest subsquare
outCol = column of largest subsquare

至于Square的实现只是一个方形2D矩阵,您不必了解它的工作原理,而只是了解如何使用它。它使用operator()进行索引和大小的方法Size。因此,您可以写

之类的东西
for (size_t i = 0; i < square.size(); ++i)
{
    for (size_t j = 0; j < square.size(); ++j)
    {
        cout << square(i, j);
    }
    cout << 'n';
}

打印出Square

之类的东西
size_t size;
cin >> size;
Square square(size);
for (size_t i = 0; i < size; ++i)
{
    for (size_t j = 0; j < size; ++j)
    {
        cin >> square(i, j);
    }
}

Square中阅读。