Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Neighborhood processing

โญโญโญ advancedโณ 10 min read๐Ÿ“ Lesson 186 of 256

Offset tables plus bounds guards; the two spec decisions (count self? divide by 8 or by actual?) that hide difficulty.

Neighborhood processing needs an offset table and a bounds guard โ€” the pattern behind Game-of-Life, image blurs, and hotspot problems:

int[] dr = {-1, -1, -1, 0, 0, 1, 1, 1};
int[] dc = {-1,  0,  1, -1, 1, -1, 0, 1};
for (int d = 0; d < 8; d++) {
    int nr = r + dr[d], nc = c + dc[d];
    if (nr >= 0 && nr < g.length && nc >= 0 && nc < g[0].length) {
        sum += g[nr][nc];
        count++;
    }
}

Two decisions hide the difficulty: does a cell count itself? (usually no โ€” read the spec twice), and do you average over 8 or over the actual in-bounds neighbor count? Corners have 3 neighbors; dividing by 8 everywhere is the subtle wrong answer the hidden tests catch.

Aggregation with signs: when a ledger holds debits and credits, summing columns then taking absolute values is NOT the same as summing absolute values. The spec's phrase "per-column balance" tells you which.