编程以查找二维数组中的相邻单元格

时间:2013-06-16 12:41:50

标签: c logic

请分享一些逻辑来查找二维数组的所有相邻单元格。我有两个逻辑,但我仍在追求第三好。

1)。遍历所有单元格并检查它是否与所选单元格相邻。

2)。通过计算索引直接跳转到相邻单元格。我发现这种方法比第一种方法更好,但有一件事我不喜欢它有很多很多条件吗?

-Bhupesh

2 个答案:

答案 0 :(得分:2)

从max(0,x-1)和max(0,y-1)循环到min(x + 1,MAX_X)和min(y + 1,MAX_Y)并跳过(x,y)。

例如:

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

int max(int a, int b) {
    if (a > b) {
        return a;
    }
    else {
        return b;
    }
}

int min(int a, int b) {
    if (a < b) {
        return a;
    }
    else {
        return b;
    }
}

void print_adjacent(int x, int y, int cols, int rows) {
    int lowest_x = max(x-1, 0);
    int highest_x = min(x+1, cols-1);
    int lowest_y = max(y-1, 0);
    int highest_y = min(y+1, rows-1);
    int i, j;

    for ( i = lowest_x; i <= highest_x; i++ ) {
        for ( j = lowest_y; j <= highest_y; j++ ) {
            if ( !(i == x && j == y) ) {
                printf("(%d, %d) is adjacent to (%d, %d)\n", i, j, x, y);
            }
        }
    }
}

int main(void) {
    int num_cols = 10;
    int num_rows = 10;

    print_adjacent(0, 0, num_cols, num_rows);
    print_adjacent(0, 3, num_cols, num_rows);
    print_adjacent(4, 5, num_cols, num_rows);
    print_adjacent(9, 5, num_cols, num_rows);
    print_adjacent(9, 9, num_cols, num_rows);

    return EXIT_SUCCESS;
}

答案 1 :(得分:0)

int SnakyArray::GetAliveNeighbourCount(int p_pRowIndex, int p_pColumnIndex)
{
    auto l_bRowDecrement    = false;
    auto l_bRowIncrement    = false;
    auto l_bCoulmnDecrement = false;
    auto l_bColumnIncrement = false;
    auto l_iAliveNeighbourCount = 0;

    if(p_pRowIndex - 1 > -1) l_bRowDecrement = true;
    if(p_pRowIndex + 1 < m_ROW_SIZE) l_bRowIncrement = true;

    if(p_pColumnIndex - 1 > -1) l_bCoulmnDecrement = true;
    if(p_pColumnIndex + 1 < m_COLUMN_SIZE) l_bColumnIncrement = true;

    if(l_bRowDecrement)
        //This is the adjacent cell.
        //l_iAliveNeighbourCount += m_PatternArray[p_pRowIndex - 1][p_pColumnIndex];

    if(l_bRowIncrement)
        //This is the adjacent cell
        //l_iAliveNeighbourCount += m_PatternArray[p_pRowIndex + 1][p_pColumnIndex];

    //Checking previous row
    if(l_bCoulmnDecrement)
    {
        //This is the adjacent cell
        //l_iAliveNeighbourCount +=  m_PatternArray[p_pRowIndex][p_pColumnIndex - 1];

        if(l_bRowDecrement)
            //This is the adjacent cell
            //l_iAliveNeighbourCount +=  m_PatternArray[p_pRowIndex - 1][p_pColumnIndex - 1];

        if(l_bRowIncrement)
            ////This is the adjacent cell
            //l_iAliveNeighbourCount +=  m_PatternArray[p_pRowIndex + 1][p_pColumnIndex - 1];
    }

    //Checking next row
    if(l_bColumnIncrement)
    {
        //This is the adjacent cell
        //l_iAliveNeighbourCount +=  m_PatternArray[p_pRowIndex][p_pColumnIndex + 1];

        if(l_bRowDecrement)
            //This is the adjacent cell
            //l_iAliveNeighbourCount +=  m_PatternArray[p_pRowIndex - 1][p_pColumnIndex + 1];

        if(l_bRowIncrement)
            //This is the adjacent cell
            //l_iAliveNeighbourCount +=  m_PatternArray[p_pRowIndex + 1][p_pColumnIndex + 1];
    }
    return l_iAliveNeighbourCount;
}