Given a R x C (1 <= R, C <= 1000000000) grid and initial position as top left corner and direction as east. Now we start running in forward direction and cross each square blocks of matrix. Whenever we find dead end or reach a cell that is already visited, we take right because we can not cross the visited square blocks again. Tell the direction when we will be at last square block.
For example : Consider the case with R = 3, C = 3. The path followed will be (0, 0) — (0, 1) — (0, 2) — (1, 2) — (2, 2) — (2, 1) — (2, 0) — (1, 0) — (1, 1). At this point, all squares have been visited, and is facing right.
Input : R = 1, C = 1 Output : Right Input : R = 2, C = 2 Output : Left Input : R = 3, C = 1 Output : Down Input : R = 3, C = 3 Output : Right
Simple Solution: One simple solution for this problem is to make it R x C matrix initialized with zero and traverse it in spiral form and take a variable ‘Dir’ that tells the current direction. Whenever we are at the end of any row and column take “Right” and change the value of ‘Dir’ according to your current direction. Now follow the given conditions :
- If you are traversing top row, then your current direction is “Right”.
- If you are right coloumn, then your current direction is “Down”.
- If you are traversing bottom row, then your current direction is “Left”.
- If you are traversing left coloumn, then your current direction is “Up”.
When we reach at the last square, just print current direction i.e; value of ‘Dir’ variable.
Time and space complexity for this problem is O(R x C) and this will work only for small values of R, C but here R and C are too large so creating R x C matrix is not possible for too large values of R and C.
Efficient Approach : This approach requires little observation and some pen paper work. Here we have to consider all the possible cases for R and C, then we just need to put IF condition for all the possible cases. Here we are with all the possible conditions :
- R != C and R is even and C is odd and R<C, direction will be “Left”.
- R != C and R is odd and C is even and R<C, direction will be “Right”.
- R != C and R is even and C is even and R<C, direction will be “Left”.
- R != C and R is odd and C is odd and R<C, direction will be “Right”.
- R != C and R is even and C is odd and R>C, direction will be “Down”.
- R != C and R is odd and C is even and R>C, direction will be “Up”.
- R != C and R is even and C is even and R>C, direction will be “Up”.
- R != C and R is odd and C is odd and R>C, direction will be “Down”.
- R == C and R is even and C is even, direction will be “Left”.
- R == C and R is odd and C is odd, direction will be “Right”.
Below is implementation of above idea.
Time Complexity : O(1)
Auxiliary Space : O(1)
This article is contributed by Shashank Mishra (Gullu).This article is reviewd by team GeeksforGeeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Maximum size square sub-matrix with all 1s
- Magic Square
- Given an n x n square matrix, find sum of all sub-squares of size k x k
- Print maximum sum square sub-matrix of given size
- Inplace rotate square matrix by 90 degrees | Set 1
- Latin Square
- Sum of both diagonals of a spiral odd-order square matrix
- Finding the maximum square sub-matrix with all equal elements
- Maximum and Minimum in a square matrix.
- Magic Square | Even Order
- Minimum cost to convert 3 X 3 matrix into magic square
- Row-wise common elements in two diagonals of a square matrix
- A square matrix as sum of symmetric and skew-symmetric matrices
- Swap major and minor diagonals of a square matrix
- Check given matrix is magic square or not
- Move matrix elements in given direction and add elements with same value
- Minimum changes needed to make a 3*3 matrix magic square
- Sum of non-diagonal parts of a square Matrix
- Rotate a matrix by 90 degree in clockwise direction without using any extra space
- Sum of Area of all possible square inside a rectangle