--- In [email protected], "David Hamill" <[EMAIL PROTECTED]> wrote:
>
> The situation seems to contain symmetries. You can simplify 
> things considerably if you make maximum use of this fact. 
> (This idea is commonly used in 
> noughts-and-crosses/tic-tac-toe programs.)
> 
> Try and write a mathematical expression (a set) defining the 
> neighbours of the ij-th cell. (Sometimes it's easier to 
> define non-neighbours than neighbours. The two sets are 
> complementary.) Once you've done this, the algorithm you 
> need should become obvious.

Hi David- you've got me worried. I thought I knew what the 'best' and
'obvious' solution(s) was/were, but I'm having trouble understanding
what you're getting at. Hence I don't know whether what I have in mind
is the same as your 'obvious' algorithm.

This would be my effort, assuming each cell has 8 neighbours and that
the edges of the cells don't wrap around; the main function is
isSmallest():

static int cells[COLS][ROWS];

/* Returns non-zero if cells[col][row] is a valid cell and >= val. */
static int isBigger(int val, int col, int row)
{
    return (0 <= col) && (col < COLS) /* col valid? */
        && (0 <= row) && (row < ROWS) /* row valid? */
        && (cells[col][row] >= val);
}   

/* Returns non-zero if cells[col][row] is smaller than any of its
neighbours. */
static int isSmallest(int col, int row)
{
    int val = cells[col][row];

    return !isBigger(val, col - 1, row - 1)  /* left top */
        && !isBigger(val, col    , row - 1)  /* middle top */
        && !isBigger(val, col + 1, row - 1)  /* right top */
        && !isBigger(val, col - 1, row    )  /* left middle */
        && !isBigger(val, col + 1, row    )  /* right middle */
        && !isBigger(val, col - 1, row + 1)  /* left bottom */
        && !isBigger(val, col    , row + 1)  /* middle bottom */
        && !isBigger(val, col + 1, row + 1); /* right bottom */
}

The other simple approach is to use nested for loops as suggested by
Steve. Is your algorithm significantly different to either of these?

John

Reply via email to