矩阵的固定元素

发布于 2025-01-26 14:23:23 字数 1272 浏览 2 评论 0原文

我正在尝试编写一个功能,该功能将检查矩阵是否具有(至少一个)固定元素。如果矩阵的元素值等于位于左侧,右,上方和下方的元素的值的值,则是静止的。

#include <iostream>
#include <vector>
bool Stationary(std::vector < std::vector < int >> a) {
  int total_elements = 0, rows = a.size();
  int up, down, left, right;
  for (auto i: a)
    for (auto j: i)
      total_elements++;
  if (total_elements % rows)
    throw std::range_error("Ragged matrix");
  int columns = total_elements / rows;
  int count = 0;
  for (int i = 0; i < rows; i++)
    for (int j = 0; j < columns; j++) {
      up = a[i][j + 1];
      down = a[i][j - 1];
      right = a[i + 1][j];
      left = a[i - 1][j];
      std::cout << up << " " << down << " " << right << " " << left << " " << a[i][j] << "\n";
      if (up == down == right == left == a[i][j]) return true;
    }
  return false;
}
int main() {
   std::vector<std::vector<int>>a{
      {2,1,3},
      {1,1,1},
      {4,1,5}};
  try {
    if (Stationary(a))
      std::cout << "Yes";
    else std::cout << "No";
  } catch (std::range_error e) {
    std::cout << e.what();
  }
  return 0;
}

我的代码问题是访问不是矩阵中不可或缺的部分的随机元素,因为使用命令i+1或j+1,我会超越矩阵的框架。您能帮我修改它而不离开矩阵框架吗?

I'm trying to write a function which will check if matrix has (at least one) stationary elements. An element of a matrix is stationary if its value is equal to the value of the elements located to the left, right, above and below it.

#include <iostream>
#include <vector>
bool Stationary(std::vector < std::vector < int >> a) {
  int total_elements = 0, rows = a.size();
  int up, down, left, right;
  for (auto i: a)
    for (auto j: i)
      total_elements++;
  if (total_elements % rows)
    throw std::range_error("Ragged matrix");
  int columns = total_elements / rows;
  int count = 0;
  for (int i = 0; i < rows; i++)
    for (int j = 0; j < columns; j++) {
      up = a[i][j + 1];
      down = a[i][j - 1];
      right = a[i + 1][j];
      left = a[i - 1][j];
      std::cout << up << " " << down << " " << right << " " << left << " " << a[i][j] << "\n";
      if (up == down == right == left == a[i][j]) return true;
    }
  return false;
}
int main() {
   std::vector<std::vector<int>>a{
      {2,1,3},
      {1,1,1},
      {4,1,5}};
  try {
    if (Stationary(a))
      std::cout << "Yes";
    else std::cout << "No";
  } catch (std::range_error e) {
    std::cout << e.what();
  }
  return 0;
}

Problem with my code is access to random elements that are not integral parts of the matrix, because with the command i+1 or j+1 I go beyond the frame of the matrix. Could you help me to modify this without leaving the matrix frame?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下凄凉 2025-02-02 14:23:23

问题是您检查矩阵的边缘,并在a [0] [0]之类的位置中检查a [-1] [0] 和a [0] [ - 1]。而是从1开始循环,然后以size()-2(包含)结束。

另一个建议是不使用复制整个矩阵的矩阵 by-by-by-by-value 。用const&amp;/code>取而代之的是。

例子:

#include <iostream>
#include <stdexcept>
#include <vector>

bool Stationary(const std::vector<std::vector<int>>& a) {
    // with less than 3 rows, it can't have 4 neighbours (left, right, above, below):
    if (a.size() < 3) return false; 

    size_t cols = a[0].size();
    
    for (size_t i = 1; i < a.size(); ++i)
        if (a[i].size() != cols) throw std::range_error("Ragged matrix");

    // start at 1 and end at size() - 2:
    for (size_t y = 1; y < a.size() - 1; ++y) {
        for (size_t x = 1; x < cols - 1; ++x) {

            int value = a[y][x];

            if (value == a[y - 1][x] &&
                value == a[y + 1][x] &&
                value == a[y][x - 1] &&
                value == a[y][x + 1]) return true;
        }
    }
    return false;
}

int main() {
    std::vector<std::vector<int>> a{{2, 1, 3},
                                    {1, 1, 1},
                                    {4, 1, 5}};
                                    
    std::cout << Stationary(a) << '\n';
}

The problem is that you check the edges of the matrix and in a position like a[0][0] you step out of bounds when checking a[-1][0] and a[0][-1]. Instead start your loops at 1 and end at size() - 2 (inclusive).

Another suggestion is to not take the matrix by-value which copies the whole matrix. Take it by const& instead.

Example:

#include <iostream>
#include <stdexcept>
#include <vector>

bool Stationary(const std::vector<std::vector<int>>& a) {
    // with less than 3 rows, it can't have 4 neighbours (left, right, above, below):
    if (a.size() < 3) return false; 

    size_t cols = a[0].size();
    
    for (size_t i = 1; i < a.size(); ++i)
        if (a[i].size() != cols) throw std::range_error("Ragged matrix");

    // start at 1 and end at size() - 2:
    for (size_t y = 1; y < a.size() - 1; ++y) {
        for (size_t x = 1; x < cols - 1; ++x) {

            int value = a[y][x];

            if (value == a[y - 1][x] &&
                value == a[y + 1][x] &&
                value == a[y][x - 1] &&
                value == a[y][x + 1]) return true;
        }
    }
    return false;
}

int main() {
    std::vector<std::vector<int>> a{{2, 1, 3},
                                    {1, 1, 1},
                                    {4, 1, 5}};
                                    
    std::cout << Stationary(a) << '\n';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文