Hossain Muctadir
Hossain Muctadir

Reputation: 3626

Distance between two elements in an two dimensional array

I am trying to solve the 8 puzzle game with program. For this reason i need to find distance between two elements of an two dimensional array. for example

int[][] input = { { 8, 5, 1 }, { 7, 4, 3 }, { 2, 0, 6 } };

if i want to move the '8' into the position [2][2], i will need 4 moves(move 2 cells horizontally and 2 vertically) which is the distance from [0][0] to [2][2]. now how can i get this distance?

Upvotes: 1

Views: 1892

Answers (1)

Scott Hunter
Scott Hunter

Reputation: 49803

The distance from [a1][b1] to [a2][b2] is abs(a1-a2)+abs(b1-b2).

Upvotes: 3

Related Questions