direction array is used to go from a index to other indexes in certain directions as told in the question. The most common directions are going in 4 directions Up, Down, Left, Right, 8 directions Up, Down, Left, Right and 4 diagonal directions and knight directions(If you’ve played chess, you’d know how knight moves)
Now, we use 2 dimensional arrays to make a grid. suppose X,Y is the co-ordinate of the position in the grid. So how will you move upward direction. X+= (-1) and Y = 0. Add this in the direction array.
dx = {-1}
dy = {0}
Now, how will you move downward direction? X+= 1 and Y = 0. Add this also.
dx = {-1,1}
dy = {0,0}
Similarly after adding left and right directions, you will get,
dx = {-1,1,0,0}
dy = {0,0,-1,1}
So, now if you iterate through them and add them to X and Y one by one, you can move to all the 4 directions.
Here are the direction array of 8 direction movement and knight movement:
//8 direction movement
int dx[] = { -1, -1, 0, 1, 1, 1, 0, -1 };
int dy[] = { 0, -1, -1, -1, 0, 1, 1, 1 };
//knight movement
int dxK[] = { -2, -2, -1, 1, 2, 2, 1, -1 };
int dyK[] = { -1, 1, 2, 2, 1, -1, -2, -2 };