Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;二维地图找到给定索引的所有相邻正方形_C++_Algorithm - Fatal编程技术网

C++ C++;二维地图找到给定索引的所有相邻正方形

C++ C++;二维地图找到给定索引的所有相邻正方形,c++,algorithm,C++,Algorithm,我很难想出一种有效的方法来找到2d容器中给定值的所有相邻正方形。假设我有一个容器,表示为: . . . . . . G . . . . . . . . . . . . . . . . . G 现在,在我的程序中生成容器后(如上图所示),我需要将G的所有相邻正方形设置为X,因此贴图应该如下所示: X X X . . X G X . . X X X . . . . . X X . . . X G 我觉得有一个比我目前如何解决这个问题简单得多的方法。以下是我的思考过程: for r to cont

我很难想出一种有效的方法来找到2d容器中给定值的所有相邻正方形。假设我有一个容器,表示为:

. . . . .
. G . . .
. . . . .
. . . . .
. . . . G
现在,在我的程序中生成容器后(如上图所示),我需要将
G
的所有相邻正方形设置为
X
,因此贴图应该如下所示:

X X X . .
X G X . .
X X X . .
. . . X X
. . . X G
我觉得有一个比我目前如何解决这个问题简单得多的方法。以下是我的思考过程:

for r to container.size()
    for c to container.size()
        if r == 0                            //if we're at the first row, don't check above
            if c == 0                        //if we're at the first column, don't check to the left

            else if c == container.size() -1 //if we're at the last column, don't check to the right

        else if r == container.size()        //if we're at the last row, don't check below
            if c == 0                        //if we're at the first column, don't check to the left

            else if c == container.size() -1 //if we're at the last column, don't check to the right

        else if c == 0                       //if we're at the first column, don't check to the left

        else if c == container.size() - 1    //if we're at the last column, don't check to the right

        else                                 //check everything

这看起来确实是重复的,但是,我必须检查很多条件,以避免意外检查超出我的地图范围。我将为上面写的每个单独的
if/else
语句编写一批
if(map[r][c+1]=“G”)
语句是否有不同的方法来检查一个正方形是否与我没有想到的
G
相邻?

避免检出边界的一种可能方法是将矩阵在每个方向上扩展一个:

所以你有点像

0 0 0 0 0 0 0
0 . . . . . 0
0 . G . . . 0
0 . . . . . 0
0 . . . . . 0
0 . . . . G 0
0 0 0 0 0 0 0
如果你真的想检查,你只需要检查你的特殊值 或者不检查您的算法,如上所述:

// index 0 and N + 1 are the surrounding
for (int x = 1; x != N + 1; ++x) {
    for (int y = 1; y != N + 1; ++y) {
        if (map[x][y] != 'G') {
            continue;
        }
        const int x_offsets[] = {-1, 0, 1, -1, 1, -1, 0, 1};
        const int y_offsets[] = {-1, -1, -1, 0, 0, 1, 1, 1};

        for (int i = 0; i != 8; ++i) {
#if 0 // if you don't want to modify surrounding
            if (map[x + x_offsets[i]][y + y_offsets[i]] == BORDER_SPECIAL_VALUE) {
                continue;
            }
#endif
            map[x + x_offsets[i]][y + y_offsets[i]] = 'X';
        }
    }
}

一种简化的方法是在数据结构中添加一个边框,例如

char map[N+2][N+2];
然后像这样在地图上迭代:

for (i = 1; i <= N; ++i)
{
    for (j = 1; j <= N; ++j)
    {
        if (map[i][j] == 'G')
        {
            for (di = -1; di <= 1; ++di)
            {
                for (dj = -1; dj <= 1; ++dj)
                {
                    if (di == 0 && dj == 0)      // skip central value
                        continue;
                    map[i + di][j + dj] = 'X';   // set neighbouring values to 'X'
                }
            }
        }
    }
}
for(i=1;i
#定义检查(x,y)x<0 | | x>=xsize | | y<0 | | y>=ysize?false:map[x,y]
对于r到container.size()
对于c到container.size()
如果检查(r-1,c-1)
...
如果检查(r-1,c)
...
...

<>代码>如果你投票失败,告诉我你在我的文章中不同意什么,至少可以保证。使用C++中的预处理器宏几乎总是代码气味。
#define CHECK(x,y) x < 0 || x >= xsize || y < 0 || y >= ysize ? false : map[x,y]

for r to container.size()
    for c to container.size() 
        if CHECK(r-1,c-1)
            ...
        if CHECK(r-1,c)
            ...
        ...