There are up to 8 directions you can move in from any position on the grid. The dx
and dy
arrays represent the x and y axis offsets of the adjacent cells. Iterating over the elements pairwise gives you:
index dir x y
0 e 1 0
1 se 1 1
2 n 0 1
3 nw -1 1
4 w -1 0
5 sw -1 -1
6 s 0 -1
7 ne 1 -1
1
solved Why the values {1, 1, 0, -1, -1, -1, 0, 1} and {0, 1, 1, 1, 0, -1, -1, -1} were chosen for dx[dir] and dy[dir]? [closed]