Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Minimum steps required to visit all corners of an N * M grid

  • Last Updated : 14 Sep, 2021

Given two integers N and M representing the dimensions of a 2D grid, and two integers R and C, representing the position of a block in that grid, the task is to find the minimum number of steps required to visit all the corners of the grid, starting from (R, C). In each step, it is allowed to move the side-adjacent block in the grid.

Examples:

Input: N = 2, M = 2, R = 1, C = 2 
Output: 3

Explanation: 
(1, 2) -> (1, 1) -> (2, 1) -> (2, 2) 
Therefore, the required output is 3.

Input: N = 2, M = 3, R = 2, C = 2 
Output:
Explanation: 
(2, 2) -> (2, 3) -> (1, 3) -> (1, 2) -> (1, 1) -> (2, 1) 
Therefore, the required output is 5.

Approach: The problem can be solved based on the following observations.

Minimum count of steps required to visit the block (i2, j2) starting from (i1, j1) is equal to abs(i2 – i1) + abs(j2 – j1)

Follow the steps given below to solve the problem:

  • First visit the corner which takes minimum count of steps using the above observations.
  • Visit the other corners of the grid by traversing the boundary of the grid either in clockwise or anticlockwise, depending on which will take the minimum count of steps to visit the corners.
  • Finally, print the minimum count of steps obtained.

Below is the implementation of the above approach:

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum count of steps
// required to visit all the corners of the grid
int min_steps_required(int n, int m, int r, int c)
{
 
    // Stores corner of the grid
    int i, j;
 
    // Stores minimum count of steps required
    // to visit the first corner of the grid
    int corner_steps_req = INT_MAX;
 
    // Checking for leftmost upper corner
    i = 1;
    j = 1;
    corner_steps_req = min(corner_steps_req,
                           abs(r - i) + abs(j - c));
 
    // Checking for leftmost down corner
    i = n;
    j = 1;
    corner_steps_req = min(corner_steps_req,
                           abs(r - i) + abs(j - c));
 
    // Checking for rightmost upper corner
    i = 1;
    j = m;
    corner_steps_req = min(corner_steps_req,
                           abs(r - i) + abs(j - c));
 
    // Checking for rightmost down corner
    i = n;
    j = m;
    corner_steps_req = min(corner_steps_req,
                           abs(r - i) + abs(j - c));
 
    // Stores minimum count of steps required
    // to visit remaining three corners of the grid
    int minimum_steps = min(2 * (n - 1) + m - 1,
                            2 * (m - 1) + n - 1);
 
    return minimum_steps + corner_steps_req;
}
 
// Driver Code
int main()
{
 
    int n = 3;
    int m = 2;
    int r = 1;
    int c = 1;
 
    cout << min_steps_required(n, m, r, c);
 
    return 0;
}

Java




// Java Program to implement the
// above approach
import java.util.*;
class GFG
{
  
// Function to find the minimum count of steps
// required to visit all the corners of the grid
static int min_steps_required(int n, int m, int r, int c)
{
 
    // Stores corner of the grid
    int i, j;
 
    // Stores minimum count of steps required
    // to visit the first corner of the grid
    int corner_steps_req = Integer.MAX_VALUE;
 
    // Checking for leftmost upper corner
    i = 1;
    j = 1;
    corner_steps_req = Math.min(corner_steps_req,
                           Math.abs(r - i) + Math.abs(j - c));
 
    // Checking for leftmost down corner
    i = n;
    j = 1;
    corner_steps_req = Math.min(corner_steps_req,
                           Math.abs(r - i) + Math.abs(j - c));
 
    // Checking for rightmost upper corner
    i = 1;
    j = m;
    corner_steps_req = Math.min(corner_steps_req,
                           Math.abs(r - i) + Math.abs(j - c));
 
    // Checking for rightmost down corner
    i = n;
    j = m;
    corner_steps_req = Math.min(corner_steps_req,
                           Math.abs(r - i) + Math.abs(j - c));
 
    // Stores minimum count of steps required
    // to visit remaining three corners of the grid
    int minimum_steps = Math.min(2 * (n - 1) + m - 1,
                            2 * (m - 1) + n - 1);
 
    return minimum_steps + corner_steps_req;
}
  
// Driver Code
public static void main(String[] args)
{
    int n = 3;
    int m = 2;
    int r = 1;
    int c = 1;
 
    System.out.print(min_steps_required(n, m, r, c));
}
}
 
// This code is contributed by code_hunt.

Python3




# Python3 program to implement
# the above approach
import sys
INT_MAX = sys.maxsize;
 
# Function to find the minimum count of steps
# required to visit all the corners of the grid
def min_steps_required(n, m, r, c) :
 
    # Stores corner of the grid
    i = 0; j = 0;
 
    # Stores minimum count of steps required
    # to visit the first corner of the grid
    corner_steps_req = INT_MAX;
 
    # Checking for leftmost upper corner
    i = 1;
    j = 1;
    corner_steps_req = min(corner_steps_req,
                           abs(r - i) + abs(j - c));
 
    # Checking for leftmost down corner
    i = n;
    j = 1;
    corner_steps_req = min(corner_steps_req,
                           abs(r - i) + abs(j - c));
 
    # Checking for rightmost upper corner
    i = 1;
    j = m;
    corner_steps_req = min(corner_steps_req,
                           abs(r - i) + abs(j - c));
 
    # Checking for rightmost down corner
    i = n;
    j = m;
    corner_steps_req = min(corner_steps_req,
                           abs(r - i) + abs(j - c));
 
    # Stores minimum count of steps required
    # to visit remaining three corners of the grid
    minimum_steps = min(2 * (n - 1) + m - 1,
                            2 * (m - 1) + n - 1);
 
    return minimum_steps + corner_steps_req;
 
# Driver Code
if __name__ == "__main__" :
    n = 3;
    m = 2;
    r = 1;
    c = 1;
    print(min_steps_required(n, m, r, c));
 
    # This code is contributed by AnkThon

C#




// C# program to implement the
// above approach
using System;
 
class GFG{
  
// Function to find the minimum count
// of steps required to visit all the
// corners of the grid
static int min_steps_required(int n, int m,
                              int r, int c)
{
     
    // Stores corner of the grid
    int i, j;
 
    // Stores minimum count of steps required
    // to visit the first corner of the grid
    int corner_steps_req = int.MaxValue;
 
    // Checking for leftmost upper corner
    i = 1;
    j = 1;
    corner_steps_req = Math.Min(corner_steps_req,
                                Math.Abs(r - i) +
                                Math.Abs(j - c));
 
    // Checking for leftmost down corner
    i = n;
    j = 1;
    corner_steps_req = Math.Min(corner_steps_req,
                                Math.Abs(r - i) +
                                Math.Abs(j - c));
 
    // Checking for rightmost upper corner
    i = 1;
    j = m;
    corner_steps_req = Math.Min(corner_steps_req,
                                Math.Abs(r - i) +
                                Math.Abs(j - c));
 
    // Checking for rightmost down corner
    i = n;
    j = m;
    corner_steps_req = Math.Min(corner_steps_req,
                                Math.Abs(r - i) +
                                Math.Abs(j - c));
 
    // Stores minimum count of steps required
    // to visit remaining three corners of the grid
    int minimum_steps = Math.Min(2 * (n - 1) + m - 1,
                                 2 * (m - 1) + n - 1);
 
    return minimum_steps + corner_steps_req;
}
  
// Driver Code
public static void Main(String[] args)
{
    int n = 3;
    int m = 2;
    int r = 1;
    int c = 1;
 
    Console.Write(min_steps_required(n, m, r, c));
}
}
 
// This code is contributed by shikhasingrajput

Javascript




<script>
 
// JavaScript program to implement the
// above approach
  
// Function to find the minimum count
// of steps required to visit all the
// corners of the grid
 
function  min_steps_required( n,  m,  r,  c)
{
      
    // Stores corner of the grid
    var i, j;
  
    // Stores minimum count of steps required
    // to visit the first corner of the grid
     
    var corner_steps_req = Number.MAX_VALUE;
  
    // Checking for leftmost upper corner
     
    i = 1;
    j = 1;
    corner_steps_req = Math.min(corner_steps_req,
                                Math.abs(r - i) +
                                Math.abs(j - c));
  
    // Checking for leftmost down corner
    i = n;
    j = 1;
    corner_steps_req = Math.min(corner_steps_req,
                                Math.abs(r - i) +
                                Math.abs(j - c));
  
    // Checking for rightmost upper corner
    i = 1;
    j = m;
    corner_steps_req = Math.min(corner_steps_req,
                                Math.abs(r - i) +
                                Math.abs(j - c));
  
    // Checking for rightmost down corner
    i = n;
    j = m;
    corner_steps_req = Math.min(corner_steps_req,
                                Math.abs(r - i) +
                                Math.abs(j - c));
  
    // Stores minimum count of steps required
    // to visit remaining three corners of the grid
    var minimum_steps = Math.min(2 * (n - 1) + m - 1,
                                 2 * (m - 1) + n - 1);
  
    return minimum_steps + corner_steps_req;
}
   
// Driver Code
 
    var n = 3;
    var m = 2;
    var r = 1;
    var c = 1;
  
    document.write(min_steps_required(n, m, r, c));
     
</script>
Output: 
4

 

Time complexity: O(1) 
Auxiliary space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!