So just use the positive distance between two points. By using this website you agree to our Cookie Policy. Find the horizontal and vertical distance.
By using this website you agree to our Cookie Policy.
At the time of BFS maintain an array of distance n and initialize it to zero for all vertices. To find the distance between two points x 1 y 1 and x 2 y 2 all that you need to do is use the coordinates of these ordered pairs and apply the formula pictured below. The distance between two points is the length of the path connecting them. The distance is the same in either direction going from point 1 to point 2 or vice versa.