C检查二维数组中元素的所有邻居 [英] C check all neighbors of a element in a 2d array

查看:120
本文介绍了C检查二维数组中元素的所有邻居的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been thinking if there is any smarter solution if I want to check all eight neighbors of an arbitrary element in a binary number only 2D array in C

我要做的是:

伪代码:

//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..

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

this is pretty dull, is there any smarter solution?

推荐答案

我使用类似的方法为 Minesweeper Game 中的特定单元格获取 Adjacent Mines.我所做的是我使用了这样的数组 (MAX_NUMBER_OF_CELLS = 8) :

I used a similar approach to get Adjacent Mines for a particular cell in the Minesweeper Game. What I did, is I used an Array like this (MAX_NUMBER_OF_CELLS = 8) :

int offset[MAX_NUMBER_OF_CELLS][2] = {
                                            {-1, -1},
                                            {-1, 0},
                                            {-1, 1},
                                            {0, -1},
                                            {0, 1},
                                            {1, -1},
                                            {1, 0},
                                            {1, 1}
                                         };

考虑到我们正在讨论矩阵中 location 0, 0 处的 CELL.我们将简单地将这些偏移值添加到 CELL 以检查相邻的 CELL 是否是有效的 CELL(即它位于矩阵内).如果它是VALID,那么我们将查看它是否包含1,如果是,则将sum 增加1 否则不是.

Considering we are talking about the CELL at location 0, 0 in a matrix. We will simply add these offset values, to the CELL to check, if the adjacent CELL is a valid CELL (i.e. it falls within the matrix). If it is VALID, then we will see if it contains 1, if yes than increment sum by 1 else not.

//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;
for (k = 0; k < MAX_NUMBER_OF_CELLS; k++)
{
    indexX = i + offset[k][0];
    indexY = j + offset[k][1];
    if (isValidCell(indexX, indexY, model)) // Here check if new CELL is VALID
                                            // whether indexX >= 0 && indexX < rows
                                            // and indexY >= 0 && indexY < columns
    {
        flag = 1;
        if (arr[indexX][indexY] == 1))
            sum += 1;
    }
}

编辑 1:

这是一个可行的例子(C 不是我的语言,但我仍然尝试着用它来给你一个想法:-)):

EDIT 1 :

Here is one working example (C is not my language, though still tried my hands on it to give you one idea :-)) :

#include <stdio.h>
#include <stdlib.h>

int findAdjacent(int [4][4], int, int, int, int);

int main(void) 
{
    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("Adjacent cells from (%d, %d) with value 1 : %d\n", i, j, sum);
    return EXIT_SUCCESS;
}

int findAdjacent(int arr[4][4], int i, int j, int rows, int columns)
{
    int sum = 0, k = 0;
    int x = -1, y = -1; // Location of the new CELL, which
                        // we will find after adding offsets
                        // to the present value of i and j
    int offset[8][2] = {
        {-1, -1},
        {-1, 0},
        {-1, 1},
        {0, -1},
        {0, 1},
        {1, -1},
        {1, 0},
        {1, 1}
    };
    for (k = 0; k < 8; k++)
    {
        x = i + offset[k][0];
        y = j + offset[k][1];
        if (isValidCell(x, y, rows, columns))
        {
            if (arr[x][y] == 1)
                sum += 1;
        }
    }

    return sum;
}

int isValidCell(int x, int y, int rows, int columns)
{
    if ((x >= 0 && x < rows) && (y >= 0 && y < columns))
        return 1;
    return 0;
}

这篇关于C检查二维数组中元素的所有邻居的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆