Patterns of Getting Neighbors

Given a 2D array, what are all the ways to get the neighbors?

D: Down
U: Up
L: Left
R: Right

A DULR will translate into:

for x, y in [(1, 0), (-1, 0), (0, -1), (0, 1)]:
    nx, ny = mat[i + x][j + y]

Traversal path if pattern starts at [0][0].

Traversal path if pattern starts at [i / 2][j / 2].

Motivation

That DFS would not yield correct result on some algorithms, i.e. shortest path, was not intuitive to me: if we traverse all the graph we should get the same results; albeit less optimized. But no, doing DFS on some algorithms will NOT yield the correct solution at all.

Leave a comment