C检查二维数组中元素的所有邻居

C检查二维数组中元素的所有邻居,c,multidimensional-array,C,Multidimensional Array,我一直在想,如果我想检查C中仅二进制数的2D数组中任意元素的所有八个邻居,是否有更聪明的解决方案 我所做的是: Psudo代码: //return how many neighbor of an element at x,y equals 1. int neighbor(int** array, int x, int y) if x>WIDTH error if y>HIEGHT error if x==0 i

我一直在想,如果我想检查C中仅二进制数的2D数组中任意元素的所有八个邻居,是否有更聪明的解决方案

我所做的是:

Psudo代码:

//return how many neighbor of an element at x,y equals 1.
int neighbor(int** array, int x, int y)
    if x>WIDTH
        error
    if y>HIEGHT 
        error
    if x==0
        ignore west, nw, sw, and calculate the rest.....
    etc..

这很无聊,有没有更聪明的解决方案?

如果你想知道单元格的邻居数量比你想改变单元格的数量多得多,一种可能的优化方法是预处理每个单元格的邻居数量,并将结果保存在另一个数组中

int** actualArray;
// fill in with 0s and 1s
int** numberOfNeighbors;
// write a function to calculate the number of neighbors for cell x,y in actualArray and
// store the result in numberOfNeighbors[x][y]
preprocess(actualArray, numberOfNeighbors); // call this whenever actualArray changes
// now you can get the number of neighbors of a cell in constant time
// from numberOfNeighbors[x][y]

如果您想知道单元的邻居数量远多于更改单元的数量,一种可能的优化方法是预处理每个单元的邻居数量,并将结果保存在另一个数组中

int** actualArray;
// fill in with 0s and 1s
int** numberOfNeighbors;
// write a function to calculate the number of neighbors for cell x,y in actualArray and
// store the result in numberOfNeighbors[x][y]
preprocess(actualArray, numberOfNeighbors); // call this whenever actualArray changes
// now you can get the number of neighbors of a cell in constant time
// from numberOfNeighbors[x][y]

我使用了一种类似的方法,在扫雷游戏中为一个特定的单元获取相邻的地雷。我所做的是使用这样的数组(最大单元数=8):

考虑到我们正在讨论矩阵中
位置0,0
处的
单元格。我们只需将这些偏移值添加到
单元格
,以检查相邻单元格是否为有效单元格(即,它位于矩阵内)。如果有效,则我们将查看它是否包含
1
,如果是,则通过
1
增加
,否则不包含

//rest of the values represent x and y that we are calculating
(-1, -1)           (-1, 0)               (-1, 1)
           -------------------------
 (0, -1)   |(0, 0(This is i and j))|     (0, 1)
           -------------------------
 (1, -1)           (1, 0)                (1, 1)

sum=0;
对于(k=0;k<单元的最大数量;k++)
{
indexX=i+偏移量[k][0];
indexY=j+偏移量[k][1];
if(isValidCell(indexX,indexY,model))//此处检查新单元格是否有效
//是否indexX>=0&&indexX<行
//和indexY>=0&&indexY<列
{
flag=1;
如果(arr[indexX][indexY]==1))
总和+=1;
}
}
编辑1: 下面是一个工作示例(C不是我的语言,但我仍然尝试使用它来给您一个想法:-):

#包括
#包括
int findAdjacent(int[4][4],int,int,int,int,int);
内部主(空)
{
int arr[4][4]={
{0, 1, 0, 0},
{1, 0, 1, 1},
{0, 1, 0, 0},
{0, 0, 0, 0}
};
int i=2,j=2;
int sum=findAdjacent(arr,i,j,4,4);
printf(“值为1:%d\n的(%d,%d)中的相邻单元格”,i,j,sum);
返回退出成功;
}
int findAdjacent(int arr[4][4],int i,int j,int行,int列)
{
整数和=0,k=0;
int x=-1,y=-1;//新单元格的位置
//我们将在添加偏移后找到
//至i和j的现值
整数偏移量[8][2]={
{-1, -1},
{-1, 0},
{-1, 1},
{0, -1},
{0, 1},
{1, -1},
{1, 0},
{1, 1}
};
对于(k=0;k<8;k++)
{
x=i+偏移量[k][0];
y=j+偏移量[k][1];
if(isValidCell(x,y,行,列))
{
如果(arr[x][y]==1)
总和+=1;
}
}
回报金额;
}
int-isValidCell(int x、int y、int行、int列)
{
if((x>=0&&x<行)&&&(y>=0&&y<列))
返回1;
返回0;
}

扫雷游戏
中,我使用了类似的方法为一个特定的单元获取
相邻的地雷
。我所做的是使用这样的数组(最大单元数=8):

考虑到我们正在讨论矩阵中
位置0,0
处的
单元格。我们只需将这些偏移值添加到
单元格
,以检查相邻单元格是否为有效单元格(即,它位于矩阵内)。如果有效,则我们将查看它是否包含
1
,如果是,则通过
1
增加
,否则不包含

//rest of the values represent x and y that we are calculating
(-1, -1)           (-1, 0)               (-1, 1)
           -------------------------
 (0, -1)   |(0, 0(This is i and j))|     (0, 1)
           -------------------------
 (1, -1)           (1, 0)                (1, 1)

sum=0;
对于(k=0;k<单元的最大数量;k++)
{
indexX=i+偏移量[k][0];
indexY=j+偏移量[k][1];
if(isValidCell(indexX,indexY,model))//此处检查新单元格是否有效
//是否indexX>=0&&indexX<行
//和indexY>=0&&indexY<列
{
flag=1;
如果(arr[indexX][indexY]==1))
总和+=1;
}
}
编辑1: 下面是一个工作示例(C不是我的语言,但我仍然尝试使用它来给您一个想法:-):

#包括
#包括
int findAdjacent(int[4][4],int,int,int,int,int);
内部主(空)
{
int arr[4][4]={
{0, 1, 0, 0},
{1, 0, 1, 1},
{0, 1, 0, 0},
{0, 0, 0, 0}
};
int i=2,j=2;
int sum=findAdjacent(arr,i,j,4,4);
printf(“值为1:%d\n的(%d,%d)中的相邻单元格”,i,j,sum);
返回退出成功;
}
int findAdjacent(int arr[4][4],int i,int j,int行,int列)
{
整数和=0,k=0;
int x=-1,y=-1;//新单元格的位置
//我们将在添加偏移后找到
//至i和j的现值
整数偏移量[8][2]={
{-1, -1},
{-1, 0},
{-1, 1},
{0, -1},
{0, 1},
{1, -1},
{1, 0},
{1, 1}
};
对于(k=0;k<8;k++)
{
x=i+偏移量[k][0];
y=j+偏移量[k][1];
if(isValidCell(x,y,行,列))
{
如果(arr[x][y]==1)
总和+=1;
}
}
回报金额;
}
int-isValidCell(int x、int y、int行、int列)
{
if((x>=0&&x<行)&&&(y>=0&&y<列))
返回1;
返回0;
}

是否正在尝试查找数组中元素的当前位置?如果是,可以定义宏,如:

#define OFFSET(x,y) ((GridWidth*y)+x)
或者,如果您试图找到周围哪些“框”可能包含元素(即哪些邻居“在边界内”)

当k 对于m=0,而m#define OFFSET(x,y) ((GridWidth*y)+x)
for k = 0 while k < GridWidth
    for m = 0 while m < GridWidth
        if k < GridWidth
            toRight = true
        if m < GridWidth
            toDown = true
        if k > 1
            toLeft = true
        if m > 1
            toUp = true