查找矩阵中的相邻位置

发布于 2024-08-10 07:27:40 字数 1472 浏览 3 评论 0原文

我很无聊,所以我创建了一个小型控制台扫雷游戏,在编写它时,我必须找到 size*size 矩阵中元素的相邻位置,该矩阵表示为元素向量和一个保存大小值的变量。我不想返回相邻元素的实际值,而是返回它们的位置,以便我可以将其用作公共函数(否则客户端可以看到地雷所在的位置:P)。

例如,对于字段 eq 0 和大小 eq 3,函数应返回 {1, 3, 4}:

1 0 0    0 1 0
0 0 0 => 1 1 0
0 0 0    0 0 0

嗯,基本上看起来像这样:

vector<int> adjecantPositions(int field, int size)
{   
    int row = field / size;
    int col = field % size;
    vector<int> result;

    /*
        1 0 0
        1 0 0
        1 0 0
    */    
    if (col > 0)
    {
        result.push_back(calcField(row, col-1, size));

        if (row > 0)    
            result.push_back(calcField(row-1, col-1, size));
        if (row < size - 1)
            result.push_back(calcField(row+1, col-1, size));
    }

    /* 
       0 0 1
       0 0 1
       0 0 1
    */
    if (col < size - 1)
    {
        result.push_back(calcField(row, col+1, size));

        if (row > 0)    
            result.push_back(calcField(row-1, col+1, size));
        if (row < size - 1)
            result.push_back(calcField(row+1, col+1, size));

    }

    /*
        0 1 0
        0 0 0
        0 1 0
    */
    if (row > 0)
        result.push_back(calcField(row-1, col, size));
    if (row < size - 1)
        result.push_back(calcField(row+1, col, size));

    return result;
}

calcField(int, int, int) 只是将坐标转换为字段编号 (row*size +上校)。

这是一个快速的解决方案,但它并不优雅,我敢打赌有更好的方法来做到这一点。有什么想法吗?

I'v been bored so I created a small console minesweeper game and while writting it I had to find the neighbor positions of an element in a size*size matrix which is represented as an vector of elements and one variable which holds the size value. I didn't want to return the actual values of the neighbor elements but their positions so that I could use that as a public function (otherwise the client could see where the mines are at :P).

For example for field eq 0 and size eq 3 the function should return {1, 3, 4}:

1 0 0    0 1 0
0 0 0 => 1 1 0
0 0 0    0 0 0

Well, basicaly it looks like this:

vector<int> adjecantPositions(int field, int size)
{   
    int row = field / size;
    int col = field % size;
    vector<int> result;

    /*
        1 0 0
        1 0 0
        1 0 0
    */    
    if (col > 0)
    {
        result.push_back(calcField(row, col-1, size));

        if (row > 0)    
            result.push_back(calcField(row-1, col-1, size));
        if (row < size - 1)
            result.push_back(calcField(row+1, col-1, size));
    }

    /* 
       0 0 1
       0 0 1
       0 0 1
    */
    if (col < size - 1)
    {
        result.push_back(calcField(row, col+1, size));

        if (row > 0)    
            result.push_back(calcField(row-1, col+1, size));
        if (row < size - 1)
            result.push_back(calcField(row+1, col+1, size));

    }

    /*
        0 1 0
        0 0 0
        0 1 0
    */
    if (row > 0)
        result.push_back(calcField(row-1, col, size));
    if (row < size - 1)
        result.push_back(calcField(row+1, col, size));

    return result;
}

calcField(int, int, int) is just converting coordinates into field number (row*size + col).

This is a fast solution but it's not elegant and I bet there's some better way to do this. Any ideas?

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

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

发布评论

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

评论(3

七秒鱼° 2024-08-17 07:27:40

是的,你的代码太糟糕了。这是一个更好的尝试(已修复,抱歉):

for (int dx=-1; dx<=1; dx++)
  for (int dy=-1; dy<=1; dy++) 
    if (dx || dy){
      int x = row+dx, y=col+dy;
      if (x >= 0 && x < size && y >= 0 && y < size)
         result.push_back(calcField(x, y, size));
    }

Yeah, your code is awful. Here's a better attempt (fixed, sorry):

for (int dx=-1; dx<=1; dx++)
  for (int dy=-1; dy<=1; dy++) 
    if (dx || dy){
      int x = row+dx, y=col+dy;
      if (x >= 0 && x < size && y >= 0 && y < size)
         result.push_back(calcField(x, y, size));
    }
橘味果▽酱 2024-08-17 07:27:40

为什么不使用对象作为坐标?我认为这会更具可读性:

struct Coords
{
  int row;
  int col;

  Case(row, col) : row(row), col(col) {}
  bool isValid(int size)
  {
    return row >= 0 && col >= 0 && row < size && col < size;
  }
}

vector<Coords> adjecantPositions(const Coords & field, int size)
{
  vector<Coords> result;
  for( int drow = -1; drow <= 1; ++drow )
  {
    for( int dcol = -1; dcol <= 1; ++dcol )
    {
      Coords current(field.row + drow, field.col + dcol);
      if( current.isValid(size) )
      {
        result.push_back(current);
      }
    }
  }
  return result;
}

Why don't using an object for your coordinates ? I think this will be more readable:

struct Coords
{
  int row;
  int col;

  Case(row, col) : row(row), col(col) {}
  bool isValid(int size)
  {
    return row >= 0 && col >= 0 && row < size && col < size;
  }
}

vector<Coords> adjecantPositions(const Coords & field, int size)
{
  vector<Coords> result;
  for( int drow = -1; drow <= 1; ++drow )
  {
    for( int dcol = -1; dcol <= 1; ++dcol )
    {
      Coords current(field.row + drow, field.col + dcol);
      if( current.isValid(size) )
      {
        result.push_back(current);
      }
    }
  }
  return result;
}
白鸥掠海 2024-08-17 07:27:40

这是 Pavel 解决方案的固定代码:

for (int drow = -1; drow <= 1; drow++)
{
    int rrow = row + drow;

    for (int dcol = -1; dcol <= 1; dcol++)
    {
        int rcol = col + dcol;

        if (rrow >= 0 && rrow < size && rcol >= 0 && rcol < size 
            && !(rrow == row && rcol == col))
            result.push_back(calcField(rrow, rcol, size));
    }
}

Here's the fixed code for Pavel's solution:

for (int drow = -1; drow <= 1; drow++)
{
    int rrow = row + drow;

    for (int dcol = -1; dcol <= 1; dcol++)
    {
        int rcol = col + dcol;

        if (rrow >= 0 && rrow < size && rcol >= 0 && rcol < size 
            && !(rrow == row && rcol == col))
            result.push_back(calcField(rrow, rcol, size));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文