Direction at last square block
Last Updated :
27 Oct, 2022
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.
Examples :
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 column, then your current direction is “Down”.
- If you are traversing bottom row, then your current direction is “Left”.
- If you are traversing left column, 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.
C++
// C++ program to tell the Current direction in
// R x C grid
#include <iostream>
using namespace std;
typedef long long int ll;
// Function which tells the Current direction
void direction(ll R, ll C)
{
if (R != C && R % 2 == 0 && C % 2 != 0 && R < C) {
cout << "Left" << endl;
return;
}
if (R != C && R % 2 != 0 && C % 2 == 0 && R > C) {
cout << "Up" << endl;
return;
}
if (R == C && R % 2 != 0 && C % 2 != 0) {
cout << "Right" << endl;
return;
}
if (R == C && R % 2 == 0 && C % 2 == 0) {
cout << "Left" << endl;
return;
}
if (R != C && R % 2 != 0 && C % 2 != 0 && R < C) {
cout << "Right" << endl;
return;
}
if (R != C && R % 2 != 0 && C % 2 != 0 && R > C) {
cout << "Down" << endl;
return;
}
if (R != C && R % 2 == 0 && C % 2 == 0 && R < C) {
cout << "Left" << endl;
return;
}
if (R != C && R % 2 == 0 && C % 2 == 0 && R > C) {
cout << "Up" << endl;
return;
}
if (R != C && R % 2 == 0 && C % 2 != 0 && R > C) {
cout << "Down" << endl;
return;
}
if (R != C && R % 2 != 0 && C % 2 == 0 && R < C) {
cout << "Right" << endl;
return;
}
}
// Driver program to test the Cases
int main()
{
ll R = 3, C = 1;
direction(R, C);
return 0;
}
C
// C program to tell the Current direction in
// R x C grid
#include <stdio.h>
typedef long long int ll;
// Function which tells the Current direction
void direction(ll R, ll C)
{
if (R != C && R % 2 == 0 && C % 2 != 0 && R < C) {
printf("Left\n");
return;
}
if (R != C && R % 2 != 0 && C % 2 == 0 && R > C) {
printf("Up\n");
return;
}
if (R == C && R % 2 != 0 && C % 2 != 0) {
printf("Right\n");
return;
}
if (R == C && R % 2 == 0 && C % 2 == 0) {
printf("Left\n");
return;
}
if (R != C && R % 2 != 0 && C % 2 != 0 && R < C) {
printf("Right\n");
return;
}
if (R != C && R % 2 != 0 && C % 2 != 0 && R > C) {
printf("Down\n");
return;
}
if (R != C && R % 2 == 0 && C % 2 == 0 && R < C) {
printf("Left\n");
return;
}
if (R != C && R % 2 == 0 && C % 2 == 0 && R > C) {
printf("Up\n");;
return;
}
if (R != C && R % 2 == 0 && C % 2 != 0 && R > C) {
printf("Down\n");
return;
}
if (R != C && R % 2 != 0 && C % 2 == 0 && R < C) {
printf("Right\n");
return;
}
}
// Driver program to test the Cases
int main()
{
ll R = 3, C = 1;
direction(R, C);
return 0;
}
// This code is contributed by kothavvsaakash.
Java
// Java program to tell the Current direction in
// R x C grid
import java.io.*;
class GFG {
// Function which tells the Current direction
static void direction(int R, int C)
{
if (R != C && R % 2 == 0 && C % 2 != 0 && R < C) {
System.out.println("Left");
return;
}
if (R != C && R % 2 != 0 && C % 2 == 0 && R > C) {
System.out.println("Up");
return;
}
if (R == C && R % 2 != 0 && C % 2 != 0) {
System.out.println("Right");
return;
}
if (R == C && R % 2 == 0 && C % 2 == 0) {
System.out.println("Left");
return;
}
if (R != C && R % 2 != 0 && C % 2 != 0 && R < C) {
System.out.println("Right");
return;
}
if (R != C && R % 2 != 0 && C % 2 != 0 && R > C) {
System.out.println("Down");
return;
}
if (R != C && R % 2 == 0 && C % 2 == 0 && R < C) {
System.out.println("Left");
return;
}
if (R != C && R % 2 == 0 && C % 2 == 0 && R > C) {
System.out.println("Up");
return;
}
if (R != C && R % 2 == 0 && C % 2 != 0 && R > C) {
System.out.println("Down");
return;
}
if (R != C && R % 2 != 0 && C % 2 == 0 && R < C) {
System.out.println("Right");
return;
}
}
// Driver code
public static void main(String[] args)
{
int R = 3, C = 1;
direction(R, C);
}
}
// This code is contributed by KRV.
Python3
# Python3 program to tell the Current
# direction in R x C grid
# Function which tells the Current direction
def direction(R, C):
if (R != C and R % 2 == 0 and
C % 2 != 0 and R < C):
print("Left")
return
if (R != C and R % 2 == 0 and
C % 2 == 0 and R > C):
print("Up")
return
if R == C and R % 2 != 0 and C % 2 != 0:
print("Right")
return
if R == C and R % 2 == 0 and C % 2 == 0:
print("Left")
return
if (R != C and R % 2 != 0 and
C % 2 != 0 and R < C):
print("Right")
return
if (R != C and R % 2 != 0 and
C % 2 != 0 and R > C):
print("Down")
return
if (R != C and R % 2 == 0 and
C % 2 != 0 and R < C):
print("Left")
return
if (R != C and R % 2 == 0 and
C % 2 == 0 and R > C):
print("Up")
return
if (R != C and R % 2 != 0 and
C % 2 != 0 and R > C):
print("Down")
return
if (R != C and R % 2 != 0 and
C % 2 != 0 and R < C):
print("Right")
return
# Driver code
R = 3; C = 1
direction(R, C)
# This code is contributed by Shrikant13
C#
// C# program to tell the Current
// direction in R x C grid
using System;
class GFG
{
// Function which tells
// the Current direction
static void direction(int R, int C)
{
if (R != C && R % 2 == 0 &&
C % 2 != 0 && R < C)
{
Console.WriteLine("Left");
return;
}
if (R != C && R % 2 != 0 &&
C % 2 == 0 && R > C)
{
Console.WriteLine("Up");
return;
}
if (R == C && R % 2 != 0 &&
C % 2 != 0)
{
Console.WriteLine("Right");
return;
}
if (R == C && R % 2 == 0 &&
C % 2 == 0)
{
Console.WriteLine("Left");
return;
}
if (R != C && R % 2 != 0 &&
C % 2 != 0 && R < C)
{
Console.WriteLine("Right");
return;
}
if (R != C && R % 2 != 0 &&
C % 2 != 0 && R > C)
{
Console.WriteLine("Down");
return;
}
if (R != C && R % 2 == 0 &&
C % 2 == 0 && R < C)
{
Console.WriteLine("Left");
return;
}
if (R != C && R % 2 == 0 &&
C % 2 == 0 && R > C)
{
Console.WriteLine("Up");
return;
}
if (R != C && R % 2 == 0 &&
C % 2 != 0 && R > C)
{
Console.WriteLine("Down");
return;
}
if (R != C && R % 2 != 0 &&
C % 2 == 0 && R < C)
{
Console.WriteLine("Right");
return;
}
}
// Driver code
static public void Main ()
{
int R = 3, C = 1;
direction(R, C);
}
}
// This code is contributed by m_kit
PHP
<?php
// PHP program to tell the Current
// direction in R x C grid
// Function which tells
// the Current direction
function direction($R, $C)
{
if ($R != $C && $R % 2 == 0 &&
$C % 2 != 0 && $R < $C)
{
echo "Left" ,"\n";
return;
}
if ($R != $C && $R % 2 != 0 &&
$C % 2 == 0 && $R > $C)
{
echo "Up" ,"\n";
return;
}
if ($R == $C && $R % 2 != 0
&& $C % 2 != 0)
{
echo "Right" ,"\n";
return;
}
if ($R == $C && $R % 2 == 0
&& $C % 2 == 0)
{
echo "Left" ,"\n";
return;
}
if ($R != $C && $R % 2 != 0 &&
$C % 2 != 0 && $R < $C)
{
echo "Right" ,"\n";
return;
}
if ($R != $C && $R % 2 != 0 &&
$C % 2 != 0 && $R > $C)
{
echo "Down" ,"\n";
return;
}
if ($R != $C && $R % 2 == 0 &&
$C % 2 == 0 && $R < $C)
{
echo "Left" ,"\n";
return;
}
if ($R != $C && $R % 2 == 0 &&
$C % 2 == 0 && $R > $C)
{
echo "Up" ,"\n";
return;
}
if ($R != $C && $R % 2 == 0 &&
$C % 2 != 0 && $R > $C)
{
echo "Down" ,"\n";
return;
}
if ($R != $C && $R % 2 != 0 &&
$C % 2 == 0 && $R < $C)
{
echo "Right" ,"\n";
return;
}
}
// Driver Code
$R = 3; $C = 1;
direction($R, $C);
// This code is contributed by aj_36
?>
JavaScript
<script>
// Javascript program to tell the Current
// direction in R x C grid
// Function which tells
// the Current direction
function direction(R, C)
{
if (R != C && R % 2 == 0 &&
C % 2 != 0 && R < C)
{
document.write("Left");
return;
}
if (R != C && R % 2 != 0 &&
C % 2 == 0 && R > C)
{
document.write("Up");
return;
}
if (R == C && R % 2 != 0 &&
C % 2 != 0)
{
document.write("Right");
return;
}
if (R == C && R % 2 == 0 &&
C % 2 == 0)
{
document.write("Left");
return;
}
if (R != C && R % 2 != 0 &&
C % 2 != 0 && R < C)
{
document.write("Right");
return;
}
if (R != C && R % 2 != 0 &&
C % 2 != 0 && R > C)
{
document.write("Down");
return;
}
if (R != C && R % 2 == 0 &&
C % 2 == 0 && R < C)
{
document.write("Left");
return;
}
if (R != C && R % 2 == 0 &&
C % 2 == 0 && R > C)
{
document.write("Up");
return;
}
if (R != C && R % 2 == 0 &&
C % 2 != 0 && R > C)
{
document.write("Down");
return;
}
if (R != C && R % 2 != 0 &&
C % 2 == 0 && R < C)
{
document.write("Right");
return;
}
}
let R = 3, C = 1;
direction(R, C);
</script>
Time Complexity : O(1)
Auxiliary Space : O(1)
This article is reviewed by team GeeksforGeeks.
Similar Reads
Solid square inside a hollow square | Pattern Given the value of n, print a hollow square of side length n and inside it a solid square of side length (n - 4) using stars(*). Examples : Input : n = 6 Output : ****** * * * ** * * ** * * * ****** Input : n = 11 Output : *********** * * * ******* * * ******* * * ******* * * ******* * * ******* * *
5 min read
Minimum block jumps to reach destination Given N lines and one starting point and destination point in 2-dimensional space. These N lines divide the space into some blocks. We need to print the minimum number of jumps to reach destination point from starting point. We can jump from one block to other block only if they share a side. Exampl
9 min read
Rotate Linked List block wise Given a Linked List of length n and block length k rotate in a circular manner towards right/left each block by a number d. If d is positive rotate towards right else rotate towards left. Examples: Input: 1->2->3->4->5->6->7->8->9->NULL, k = 3 d = 1 Output: 3->1->2-
14 min read
Minimum cost to reach the last cell of a grid with directions Given a 2D matrix grid[][] of size M X N, where each cell of the grid has a character denoting the next cell we should visit we are at the current cell. The character at any cell can be: 'R' which means go to the cell to the right. (i.e from grid[i][j] to grid[i][j + 1]) 'L' which means go to the ce
12 min read
Square with Ascending and Descending Edges Given an integer n, create a square pattern of size n x n such that the edges of the pattern contain the numbers 1 to n in ascending order on the top and left edges, and n to 1 in descending order on the bottom and right edges. The remaining elements of the pattern should be filled with numbers that
5 min read
Largest square which can be formed using given rectangular blocks Given an array arr[] of positive integers where each element of the array represents the length of the rectangular blocks. The task is to find the largest length of the square which can be formed using the rectangular blocks. Examples: Input: arr[] = {3, 2, 1, 5, 2, 4} Output: 3 Explanation: Using r
10 min read