# Count possible moves in the given direction in a grid

Given a grid **N x M** size and initial position **(X, Y)** and a series of **K** moves.

Each move contains 2 integers **Dx** and **Dy**.

A single move consists of the following operation that can also be performed as many times as possible:

X = X + DxandY = Y + Dy.

The task is to count the number of moves performed.

**Note** that at every point of time, the current position must be inside the grid.

**Examples:**

Input:N = 4, M = 5, X = 1, Y = 1, k[] = {{1, 1}, {1, 1}, {0, -2}}

Output:4

The values of(X, Y)are as follows:

Steps in Move 1: (1, 1) -> (2, 2) -> (3, 3) -> (4, 4) i.e. 3 moves

Move 2 is not possible as we cannot move to (5, 5) which is out of the bounds of the grid

Steps in Move 3: (4, 4) -> (4, 2) i.e. a single move and (4, 0) will be out of the bound so no further moves possible.

Total moves = 3 + 1 = 4

Input:N = 10, M = 10, X = 3, Y = 3, k[] = {{1, 1}, {-1, -1}}

Output:16

**Approach:** It can be observed that we can handle **X** and **Y** independently, and merge them as number of steps in current move= minimum of (number of steps with **X**, number of steps with **Y**). To find number of moves with **X**, we can use the sign of **Dx** to know which end we are approaching and find the distance to that end, with speed being **Dx**, we can find out the number of steps it would take.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `#define int long long ` `using` `namespace` `std; ` ` ` `// Function to return the count of ` `// possible steps in a single direction ` `int` `steps(` `int` `cur, ` `int` `x, ` `int` `n) ` `{ ` ` ` ` ` `// It can cover infinte steps ` ` ` `if` `(x == 0) ` ` ` `return` `INT_MAX; ` ` ` ` ` `// We are approaching towards X = N ` ` ` `if` `(x > 0) ` ` ` `return` `abs` `((n - cur) / x); ` ` ` ` ` `// We are approaching towards X = 1 ` ` ` `else` ` ` `return` `abs` `((cur - 1) / x); ` `} ` ` ` `// Function to return the count of steps ` `int` `countSteps(` `int` `curx, ` `int` `cury, ` `int` `n, ` `int` `m, ` ` ` `vector<pair<` `int` `, ` `int` `> > moves) ` `{ ` ` ` `int` `count = 0; ` ` ` `int` `k = moves.size(); ` ` ` `for` `(` `int` `i = 0; i < k; i++) { ` ` ` `int` `x = moves[i].first; ` ` ` `int` `y = moves[i].second; ` ` ` ` ` `// Take the minimum of both moves independently ` ` ` `int` `stepct ` ` ` `= min(steps(curx, x, n), steps(cury, y, m)); ` ` ` ` ` `// Update count and current positions ` ` ` `count += stepct; ` ` ` `curx += stepct * x; ` ` ` `cury += stepct * y; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `main() ` `{ ` ` ` `int` `n = 4, m = 5, x = 1, y = 1; ` ` ` `vector<pair<` `int` `, ` `int` `> > moves = { { 1, 1 }, ` ` ` `{ 1, 1 }, ` ` ` `{ 0, -2 } }; ` ` ` `int` `k = moves.size(); ` ` ` `cout << countSteps(x, y, n, m, moves); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count of ` `# possible steps in a single direction ` `def` `steps(cur, x, n): ` ` ` ` ` `# It can cover infinte steps ` ` ` `if` `x ` `=` `=` `0` `: ` ` ` `return` `float` `(` `'inf'` `) ` ` ` ` ` `# We are approaching towards X = N ` ` ` `elif` `x > ` `0` `: ` ` ` `return` `abs` `((n ` `-` `cur) ` `/` `/` `x) ` ` ` ` ` `# We are approaching towards X = 1 ` ` ` `else` `: ` ` ` `return` `abs` `(` `int` `((cur ` `-` `1` `) ` `/` `x)) ` ` ` `# Function to return the count of steps ` `def` `countSteps(curx, cury, n, m, moves): ` ` ` ` ` `count ` `=` `0` ` ` `k ` `=` `len` `(moves) ` ` ` `for` `i ` `in` `range` `(` `0` `, k): ` ` ` `x ` `=` `moves[i][` `0` `] ` ` ` `y ` `=` `moves[i][` `1` `] ` ` ` ` ` `# Take the minimum of both moves ` ` ` `# independently ` ` ` `stepct ` `=` `min` `(steps(curx, x, n), ` ` ` `steps(cury, y, m)) ` ` ` ` ` `# Update count and current positions ` ` ` `count ` `+` `=` `stepct ` ` ` `curx ` `+` `=` `stepct ` `*` `x ` ` ` `cury ` `+` `=` `stepct ` `*` `y ` ` ` ` ` `return` `count ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n, m, x, y ` `=` `4` `, ` `5` `, ` `1` `, ` `1` ` ` `moves ` `=` `[[` `1` `, ` `1` `], [` `1` `, ` `1` `], [` `0` `, ` `-` `2` `]] ` ` ` ` ` `print` `(countSteps(x, y, n, m, moves)) ` ` ` `# This code is contributed ` `# by Rituraj Jain ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// Php implementation of the above approach ` ` ` `// Function to return the count of ` `// possible steps in a single direction ` `function` `steps(` `$cur` `, ` `$x` `, ` `$n` `) ` `{ ` ` ` ` ` `// It can cover infinte steps ` ` ` `if` `(` `$x` `== 0) ` ` ` `return` `PHP_INT_MAX; ` ` ` ` ` `// We are approaching towards X = N ` ` ` `if` `(` `$x` `> 0) ` ` ` `return` `floor` `(` `abs` `((` `$n` `- ` `$cur` `) / ` `$x` `)); ` ` ` ` ` `// We are approaching towards X = 1 ` ` ` `else` ` ` `return` `floor` `(` `abs` `((` `$cur` `- 1) / ` `$x` `)); ` `} ` ` ` `// Function to return the count of steps ` `function` `countSteps(` `$curx` `, ` `$cury` `, ` `$n` `, ` `$m` `, ` `$moves` `) ` `{ ` ` ` `$count` `= 0; ` ` ` `$k` `= sizeof(` `$moves` `); ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$k` `; ` `$i` `++) ` ` ` `{ ` ` ` `$x` `= ` `$moves` `[` `$i` `][0]; ` ` ` `$y` `= ` `$moves` `[` `$i` `][1]; ` ` ` ` ` `// Take the minimum of both moves independently ` ` ` `$stepct` `= min(steps(` `$curx` `, ` `$x` `, ` `$n` `), steps(` `$cury` `, ` `$y` `, ` `$m` `)); ` ` ` ` ` `// Update count and current positions ` ` ` `$count` `+= ` `$stepct` `; ` ` ` `$curx` `+= ` `$stepct` `* ` `$x` `; ` ` ` `$cury` `+= ` `$stepct` `* ` `$y` `; ` ` ` `} ` ` ` `return` `$count` `; ` `} ` ` ` ` ` `// Driver code ` ` ` `$n` `= 4 ; ` ` ` `$m` `= 5 ; ` ` ` `$x` `= 1 ; ` ` ` `$y` `= 1 ; ` ` ` `$moves` `= ` `array` `( ` `array` `( 1, 1 ), ` ` ` `array` `( 1, 1 ), ` ` ` `array` `( 0, -2 ) ); ` ` ` `$k` `= sizeof(` `$moves` `); ` ` ` ` ` `echo` `countSteps(` `$x` `, ` `$y` `, ` `$n` `, ` `$m` `, ` `$moves` `); ` ` ` `// This code is contributed by Ryuga ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(N)

## Recommended Posts:

- Print direction of moves such that you stay within the [-k, +k] boundary
- Check if the robot is within the bounds of the grid after given moves
- Count number of 1s in the array after N moves
- Count Magic squares in a grid
- Sum of the count of number of adjacent squares in an M X N grid
- Find the direction from given string
- Direction at last square block
- Rotate a matrix by 90 degree in clockwise direction without using any extra space
- Print a given matrix in spiral form using direction tracking method
- Time until distance gets equal to X between two objects moving in opposite direction
- Possible moves of knight
- Number of triangles after N moves
- Maximum prime moves to convert X to Y
- Number of moves required to guess a permutation.
- Array element moved by k using single moves

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.