Given an infinite grid, initial cell position (x, y) and a sequence of other cell position which needs to be covered in the given order. The task is to find the minimum number of steps needed to travel to all those cells.

**Note:** Movement can be done in any of the eight possible directions from a given cell i.e from cell (x, y) you can move to any of the following eight positions:(x-1, y+1), (x-1, y), (x-1, y-1), (x, y-1), (x+1, y-1), (x+1, y), (x+1, y+1), (x, y+1) is possible.

**Examples:**

Input:points[] = [(0, 0), (1, 1), (1, 2)]

Output:2

Move from (0, 0) to (1, 1) in 1 step(diagonal) and

then from (1, 1) to (1, 2) in 1 step (rightwards)

Input:points[] = [{4, 6}, {1, 2}, {4, 5}, {10, 12}]

Output:14

Move from(4, 6)-> (3, 5) -> (2, 4) -> (1, 3) ->

(1, 2)-> (2, 3) -> (3, 4) ->

(4, 5)-> (5, 6) -> (6, 7) ->

(7, 8) -> (8, 9) -> (9, 10) -> (10, 11) ->(10, 12)

**Approach:** Since all the given points are to be covered in the specified order. Find the minimum number of steps required to reach from a starting point to next point, then the sum of all such minimum steps for covering all the points would be the answer. One way to reach from a point (x1, y1) to (x2, y2) is to move abs(x2-x1) steps in the horizontal direction and abs(y2-y1) steps in the vertical direction, but this is not the shortest path to reach (x2, y2). The best way would be to cover the maximum possible distance in a diagonal direction and remaining in horizontal or vertical direction.

If we look closely this just reduces to the maximum of **abs(x2-x1)** and **abs(y2-y1)**. Traverse for all points and summation of all diagonal distance will be the answer.

Below is the implementation of the above approach:

## C++

`// C++ program to cover a sequence of points ` `// in minimum steps in a given order. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// cell structure denoted as point ` `struct` `point { ` ` ` `int` `x, y; ` `}; ` ` ` `// function to give minimum steps to ` `// move from point p1 to p2 ` `int` `shortestPath(point p1, point p2) ` `{ ` ` ` `// dx is total horizontal ` ` ` `// distance to be covered ` ` ` `int` `dx = ` `abs` `(p1.x - p2.x); ` ` ` ` ` `// dy is total vertical ` ` ` `// distance to be covered ` ` ` `int` `dy = ` `abs` `(p1.y - p2.y); ` ` ` ` ` `// required answer is ` ` ` `// maximum of these two ` ` ` `return` `max(dx, dy); ` `} ` ` ` `// Function to return the minimum steps ` `int` `coverPoints(point sequence[], ` `int` `size) ` `{ ` ` ` `int` `stepCount = 0; ` ` ` ` ` `// finding steps for each ` ` ` `// consecutive point in the sequence ` ` ` `for` `(` `int` `i = 0; i < size - 1; i++) { ` ` ` `stepCount += shortestPath(sequence[i], ` ` ` `sequence[i + 1]); ` ` ` `} ` ` ` ` ` `return` `stepCount; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// arr stores sequence of points ` ` ` `// that are to be visited ` ` ` `point arr[] = { { 4, 6 }, { 1, 2 }, { 4, 5 }, { 10, 12 } }; ` ` ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `cout << coverPoints(arr, n); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to cover a ` `// sequence of points in ` `// minimum steps in a given order. ` `import` `java.io.*; ` `import` `java.util.*; ` `import` `java.lang.*; ` ` ` `// class denoted as point ` `class` `point ` `{ ` ` ` `int` `x, y; ` ` ` `point(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `x = a; ` ` ` `y = b; ` ` ` ` ` `} ` `} ` ` ` `class` `GFG ` `{ ` `// function to give minimum ` `// steps to move from point ` `// p1 to p2 ` `static` `int` `shortestPath(point p1, ` ` ` `point p2) ` `{ ` ` ` `// dx is total horizontal ` ` ` `// distance to be covered ` ` ` `int` `dx = Math.abs(p1.x - p2.x); ` ` ` ` ` `// dy is total vertical ` ` ` `// distance to be covered ` ` ` `int` `dy = Math.abs(p1.y - p2.y); ` ` ` ` ` `// required answer is ` ` ` `// maximum of these two ` ` ` `return` `Math.max(dx, dy); ` `} ` ` ` `// Function to return ` `// the minimum steps ` `static` `int` `coverPoints(point sequence[], ` ` ` `int` `size) ` `{ ` ` ` `int` `stepCount = ` `0` `; ` ` ` ` ` `// finding steps for ` ` ` `// each consecutive ` ` ` `// point in the sequence ` ` ` `for` `(` `int` `i = ` `0` `; i < size - ` `1` `; i++) ` ` ` `{ ` ` ` `stepCount += shortestPath(sequence[i], ` ` ` `sequence[i + ` `1` `]); ` ` ` `} ` ` ` ` ` `return` `stepCount; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `// arr stores sequence of points ` ` ` `// that are to be visited ` ` ` `point arr[] = ` `new` `point[` `4` `]; ` ` ` `arr[` `0` `] = ` `new` `point(` `4` `, ` `6` `); ` ` ` `arr[` `1` `] = ` `new` `point(` `1` `, ` `2` `); ` ` ` `arr[` `2` `] = ` `new` `point(` `4` `, ` `5` `); ` ` ` `arr[` `3` `] = ` `new` `point(` `10` `, ` `12` `); ` ` ` ` ` `int` `n = arr.length; ` ` ` `System.out.print(coverPoints(arr, n)); ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to cover a sequence of points ` `# in minimum steps in a given order. ` ` ` `# function to give minimum steps to ` `# move from pop1 to p2 ` `def` `shortestPath(p1, p2): ` ` ` ` ` `# dx is total horizontal ` ` ` `# distance to be covered ` ` ` `dx ` `=` `abs` `(p1[` `0` `] ` `-` `p2[` `0` `]) ` ` ` ` ` `# dy is total vertical ` ` ` `# distance to be covered ` ` ` `dy ` `=` `abs` `(p1[` `1` `] ` `-` `p2[` `1` `]) ` ` ` ` ` `# required answer is ` ` ` `# maximum of these two ` ` ` `return` `max` `(dx, dy) ` ` ` `# Function to return the minimum steps ` `def` `coverPoints(sequence, size): ` ` ` ` ` `stepCount ` `=` `0` ` ` ` ` `# finding steps for each ` ` ` `# consecutive poin the sequence ` ` ` `for` `i ` `in` `range` `(size` `-` `1` `): ` ` ` `stepCount ` `+` `=` `shortestPath(sequence[i],sequence[i ` `+` `1` `]) ` ` ` ` ` `return` `stepCount ` ` ` `# Driver code ` `# arr stores sequence of points ` `# that are to be visited ` `arr ` `=` `[[` `4` `, ` `6` `] ,[ ` `1` `, ` `2` `], [ ` `4` `, ` `5` `] , [ ` `10` `, ` `12` `]] ` ` ` `n ` `=` `len` `(arr) ` `print` `(coverPoints(arr, n)) ` ` ` `# This code is contributed by shivanisinghss2110. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to cover a ` `// sequence of points in ` `// minimum steps in a given order. ` ` ` `using` `System; ` `// class denoted as point ` `public` `class` `point ` `{ ` ` ` `public` `int` `x, y; ` ` ` `public` `point(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `x = a; ` ` ` `y = b; ` ` ` ` ` `} ` `} ` ` ` `public` `class` `GFG ` `{ ` ` ` `// function to give minimum ` ` ` `// steps to move from point ` ` ` `// p1 to p2[] ` ` ` `static` `int` `shortestPath(point p1, ` ` ` `point p2) ` ` ` `{ ` ` ` `// dx is total horizontal ` ` ` `// distance to be covered ` ` ` `int` `dx = Math.Abs(p1.x - p2.x); ` ` ` ` ` `// dy is total vertical ` ` ` `// distance to be covered ` ` ` `int` `dy = Math.Abs(p1.y - p2.y); ` ` ` ` ` `// required answer is ` ` ` `// maximum of these two ` ` ` `return` `Math.Max(dx, dy); ` ` ` `} ` ` ` ` ` `// Function to return ` ` ` `// the minimum steps ` ` ` `static` `int` `coverPoints(point []sequence, ` ` ` `int` `size) ` ` ` `{ ` ` ` `int` `stepCount = 0; ` ` ` ` ` `// finding steps for ` ` ` `// each consecutive ` ` ` `// point in the sequence ` ` ` `for` `(` `int` `i = 0; i < size - 1; i++) ` ` ` `{ ` ` ` `stepCount += shortestPath(sequence[i], ` ` ` `sequence[i + 1]); ` ` ` `} ` ` ` ` ` `return` `stepCount; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `// arr stores sequence of points ` ` ` `// that are to be visited ` ` ` `point []arr = ` `new` `point[4]; ` ` ` `arr[0] = ` `new` `point(4, 6); ` ` ` `arr[1] = ` `new` `point(1, 2); ` ` ` `arr[2] = ` `new` `point(4, 5); ` ` ` `arr[3] = ` `new` `point(10, 12); ` ` ` ` ` `int` `n = arr.Length; ` ` ` `Console.WriteLine(coverPoints(arr, n)); ` ` ` `} ` `} ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

`# Traversing from one point to another point ` `# storing the minimum number of steps ` `def` `traversal_steps(points): ` ` ` `minSteps ` `=` `0` ` ` `for` `p ` `in` `range` `(` `len` `(points)` `-` `1` `): ` ` ` ` ` `# taking the manhattan distance between ` ` ` `# x and y-coordinates ` ` ` `d1 ` `=` `abs` `(points[p][` `0` `] ` `-` `points[p` `+` `1` `][` `0` `]) ` ` ` `d2 ` `=` `abs` `(points[p][` `1` `] ` `-` `points[p` `+` `1` `][` `1` `]) ` ` ` ` ` `# adding the maximum among the two to the ` ` ` `# running steps parameter ` ` ` `minSteps ` `+` `=` `max` `(d1,d2) ` ` ` `return` `(minSteps) ` ` ` `# Main Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `points ` `=` `[(` `0` `,` `0` `),(` `1` `,` `1` `),(` `1` `,` `2` `)] ` ` ` `print` `(traversal_steps(points)) ` ` ` `points ` `=` `[(` `4` `,` `6` `),(` `1` `,` `2` `),(` `4` `,` `5` `),(` `10` `,` `12` `)] ` ` ` `print` `(traversal_steps(points)) ` |

*chevron_right*

*filter_none*

**Output:**

14

**Time Complexity:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum number of points required to cover all blocks of a 2-D grid
- Minimum cost to cover the given positions in a N*M grid
- Steps required to visit M points in order on a circular ring of N points
- Find if the given number is present in the infinite sequence or not
- Find minimum moves to reach target on an infinite line
- Minimum moves to reach target on a infinite line | Set 2
- Minimum steps required to rearrange given array to a power sequence of 2
- Minimum squares to cover a rectangle
- Minimum time required to cover a Binary Array
- Count minimum number of fountains to be activated to cover the entire garden
- Number of Integral Points between Two Points
- Prime points (Points that split a number into two primes)
- Find the point on X-axis from given N points having least Sum of Distances from all other points
- Measure one litre using two vessels and infinite water supply
- Sum of series M/1 + (M+P)/2 + (M+2*P)/4 + (M+3*P)/8......up to infinite
- Find the sum of infinite series 1^2.x^0 + 2^2.x^1 + 3^2.x^2 + 4^2.x^3 +.......
- Find the count of unvisited indices in an infinite array
- Number of squares of side length required to cover an N*M rectangle
- Count number of ways to cover a distance | Set 2
- Maximum number of tiles required to cover the floor of given size using 2x1 size tiles

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.