Given a 2D grid of size **N x M**. The task is to find the final direction after visiting every cell under given conditions.

- You can start only from the top left corner of the N*M grid and facing towards the right.
- You can walk one square at a time in the direction you are facing.
- If you reach the boundary of the grid or if the next square you are about to visit has already been visited, you turn right.

**Examples:**

Input:N = 3, M = 3

Output:Right

Explanation:Below is the final position after traversing the grid

Input:N = 3, M = 1

Output:Down

**Approach:** For the above problem statement we have to observe the following:

- The path formed will always be a Spiral path. So, we can say that any of the middle cells will be the final cell and we need to find the direction of that cell.
- If
**n > m**, then the final direction could only be Up or Down depending on the value of m because there always be some cell left in the last uncovered column when all other columns are covered. - If
**n <= m**, then the final direction could only be Left or Right depending on the value of n.

Therefore on the basis of above observations, there can be only 4 cases for 4 directions:

- If n > m and m is even, final direction will be Up.
- If n > m and m is odd, final direction will be Down.
- If n <= m and n is even, final direction will be Left.
- If n <= m and n is odd, final direction will be Right.

Below is the implementation of the above approach:

## C++

`// C++ program to find the direction ` `// when stopped moving ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the direction ` `// when stopped moving ` `void` `findDirection(` `int` `n, ` `int` `m) ` `{ ` ` ` `if` `(n > m) { ` ` ` `if` `(m % 2 == 0) ` ` ` `printf` `(` `"Up\n"` `); ` ` ` `else` ` ` `printf` `(` `"Down\n"` `); ` ` ` `} ` ` ` `else` `{ ` ` ` `if` `(n % 2 == 0) ` ` ` `printf` `(` `"Left\n"` `); ` ` ` `else` ` ` `printf` `(` `"Right\n"` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given size of NxM grid ` ` ` `int` `n = 3, m = 3; ` ` ` ` ` `// Function Call ` ` ` `findDirection(n, m); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the direction ` `// when stopped moving ` `class` `GFG{ ` ` ` `// Function to find the direction ` `// when stopped moving ` `static` `void` `findDirection(` `int` `n, ` `int` `m) ` `{ ` ` ` `if` `(n > m) ` ` ` `{ ` ` ` `if` `(m % ` `2` `== ` `0` `) ` ` ` `System.out.print(` `"Up\n"` `); ` ` ` `else` ` ` `System.out.print(` `"Down\n"` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `if` `(n % ` `2` `== ` `0` `) ` ` ` `System.out.print(` `"Left\n"` `); ` ` ` `else` ` ` `System.out.print(` `"Right\n"` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `// Given size of NxM grid ` ` ` `int` `n = ` `3` `, m = ` `3` `; ` ` ` ` ` `// Function Call ` ` ` `findDirection(n, m); ` `} ` `} ` ` ` `// This code is contributed by shubham ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the direction ` `# when stopped moving ` ` ` `# Function to find the direction ` `# when stopped moving ` `def` `findDirection(n, m): ` ` ` ` ` `if` `(n > m): ` ` ` `if` `(m ` `%` `2` `=` `=` `0` `): ` ` ` `print` `(` `"Up\n"` `); ` ` ` `else` `: ` ` ` `print` `(` `"Down\n"` `); ` ` ` ` ` `else` `: ` ` ` `if` `(n ` `%` `2` `=` `=` `0` `): ` ` ` `print` `(` `"Left\n"` `); ` ` ` `else` `: ` ` ` `print` `(` `"Right\n"` `); ` ` ` `# Driver Code ` ` ` `# Given size of NxM grid ` `n ` `=` `3` `; m ` `=` `3` `; ` ` ` `# Function Call ` `findDirection(n, m); ` ` ` `# This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the direction ` `// when stopped moving ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to find the direction ` `// when stopped moving ` `static` `void` `findDirection(` `int` `n, ` `int` `m) ` `{ ` ` ` `if` `(n > m) ` ` ` `{ ` ` ` `if` `(m % 2 == 0) ` ` ` `Console.Write(` `"Up\n"` `); ` ` ` `else` ` ` `Console.Write(` `"Down\n"` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `if` `(n % 2 == 0) ` ` ` `Console.Write(` `"Left\n"` `); ` ` ` `else` ` ` `Console.Write(` `"Right\n"` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` ` ` `// Given size of NxM grid ` ` ` `int` `n = 3, m = 3; ` ` ` ` ` `// Function Call ` ` ` `findDirection(n, m); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

Right

**Time Complexity:** *O(1)*

**Auxiliary Space:** *O(1)*

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:

- Number of ways to reach (M, N) in a matrix starting from the origin without visiting (X, Y)
- Final cell position in the matrix
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
- Minimum Manhattan distance covered by visiting every coordinates from a source to a final vertex
- Minimum moves taken to move coin of each cell to any one cell of Matrix
- Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix
- Minimum Distance from a given Cell to all other Cells of a Matrix
- Sum of cost of all paths to reach a given cell in a Matrix
- Distance of nearest cell having 1 in a binary matrix
- 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
- Find minimum moves to bring all elements in one cell of a matrix
- Number of ways of cutting a Matrix such that atleast one cell is filled in each part
- Print a matrix in a spiral form starting from a point
- Number of ways to reach (X, Y) in a matrix starting from the origin
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Move matrix elements in given direction and add elements with same value
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Element in a matrix starting from which anti-clockwise traversal ends at the last element
- Direction at last square block

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.