Shortest path on a Square
Last Updated :
09 Jan, 2023
Given side of a square n and two points (x1, y1) and (x2, y2) on the boundaries of the given square. The task is to find the shortest path through the square sides between these two points where the corner coordinates of the square are (0, 0), (n, 0), (0, n), and (n, n).
Examples:
Input: n = 2, x1 = 0, y1 = 0, x2 = 1, y2 = 0
Output: 1

Input: n = 26, x1 = 21, y1 = 0, x2 = 26, y2 = 14
Output: 19
Approach:
- If both the x and y coordinates of a point is greater than the other and the points are not on opposite sides of square then the shortest distance will be abs(x2 - x1) + abs(y2 - y1).
- Else, the shortest distance will be equal to min((x1 + y1 + x2 + y2), (4 * n) - (x1 + y1 + x2 + y2))
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the length
// of the minimum path between
// two points on a square of given side
int minPath(int n, int x1, int y1, int x2, int y2)
{
// If both of the x and y coordinates
// of one point is greater than the other
if ((x1 <= x2 && y1 <= y2) || (x1 >= x2 && y1 >= y2))
{
// If the points are not on opposite sides
if (!(abs(y2 - y1) == n || abs(x2 - x1) == n))
return (abs(x1 - x2) + abs(y1 - y2));
}
return min(x1 + x2 + y1 + y2, (4 * n) - (x1 + x2 + y1 + y2));
}
// Driver code
int main()
{
// Side of the square
int n = 4;
int x1 = 2, y1 = 0, x2 = 3, y2 = 4;
cout << minPath(n, x1, y1, x2, y2);
return 0;
}
// improved by Sonal Agrawal
Java
// Java implementation of the approach
class GFG{
// Function to return the length
// of the minimum path between
// two points on a square of given side
static int minPath(int n, int x1, int y1,
int x2, int y2)
{
// If both of the x and y coordinates
// of one point is greater than the other
if ((x1 <= x2 && y1 <= y2) ||
(x1 >= x2 && y1 >= y2))
{
// If the points are not on opposite sides
if (!(Math.abs(y2 - y1) == n ||
Math.abs(x2 - x1) == n))
return (Math.abs(x1 - x2) +
Math.abs(y1 - y2));
}
return Math.min(x1 + x2 + y1 + y2, (4 * n) -
(x1 + x2 + y1 + y2));
}
// Driver code
public static void main(String[] args)
{
// Side of the square
int n = 4;
int x1 = 2, y1 = 0, x2 = 3, y2 = 4;
System.out.println(minPath(n, x1, y1, x2, y2));
}
}
// This code is contributed by sanjeev2552
Python3
# Python3 implementation of above approach
# Function to return the length of the
# minimum path between two points
# on a square of given side
def minPath(n, x1, y1, x2, y2):
# If both of the x and y coordinates
# of one point is greater than the other
if (((x1 <= x2 and y1 <= y2) or
(x1 >= x2 and y1 >= y2)) and
not (abs(y2 - y1) == n or
abs(x2 - x1) == n)):
return (abs(x1 - x2) + abs(y1 - y2));
return min(x1 + x2 + y1 + y2, (4 * n) -
(x1 + x2 + y1 + y2));
# Driver code
# side of the square
n = 4; x1 = 2; y1 = 0
x2 = 3; y2 = 4
print(minPath(n, x1, y1, x2, y2))
# This code is contributed
# by Shashank_Sharma
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the length
// of the minimum path between
// two points on a square of given side
static int minPath(int n, int x1, int y1,
int x2, int y2)
{
// If both of the x and y coordinates
// of one point is greater than the other
if ((x1 <= x2 && y1 <= y2) || (x1 >= x2 && y1 >= y2))
{
// If the points are not on opposite sides
if (!(Math.Abs(y2 - y1) == n || Math.Abs(x2 - x1) == n))
return (Math.Abs(x1 - x2) + Math.Abs(y1 - y2));
}
return Math.Min(x1 + x2 + y1 + y2, (4 * n) - (x1 + x2 + y1 + y2));
}
// Driver code
public static void Main()
{
// Side of the square
int n = 4;
int x1 = 2, y1 = 0, x2 = 3, y2 = 4;
Console.Write(minPath(n, x1, y1, x2, y2));
}
}
// This code is contributed
// by Akanksha Rai
PHP
<?php
// Php implementation of the approach
// Function to return the length
// of the minimum path between
// two points on a square of given side
function minPath($n, $x1, $y1, $x2, $y2)
{
// If both of the x and y coordinates
// of one point is greater than the other
if (($x1 <= $x2 && $y1 <= $y2) || ($x1 >= $x2 && $y1 >= $y2))
{
// If the points are not on opposite sides
if (!(abs($y2 - $y1) == $n || abs($x2 - $x1) == $n))
return (abs($x1 - $x2) + abs($y1 - $y2));
}
return min($x1 + $x2 + $y1 + $y2, (4 * $n) - ($x1 + $x2 + $y1 + $y2));
}
// Driver code
// Side of the square
$n = 4;
$x1 = 2 ;
$y1 = 0 ;
$x2 = 3 ;
$y2 = 4 ;
echo minPath($n, $x1, $y1, $x2, $y2);
// This code is contributed by Ryuga
?>
JavaScript
<script>
// Javascript implementation of the approach
// Function to return the length
// of the minimum path between
// two points on a square of given side
function minPath(n, x1, y1, x2, y2)
{
// If both of the x and y coordinates
// of one point is greater than the other
if ((x1 <= x2 && y1 <= y2) || (x1 >= x2 && y1 >= y2))
{
// If the points are not on opposite sides
if (!(Math.abs(y2 - y1) == n || Math.abs(x2 - x1) == n))
return (Math.abs(x1 - x2) + Math.abs(y1 - y2));
}
return Math.min(x1 + x2 + y1 + y2, (4 * n) - (x1 + x2 + y1 + y2));
}
// Side of the square
let n = 4;
let x1 = 2, y1 = 0, x2 = 3, y2 = 4;
document.write(minPath(n, x1, y1, x2, y2));
// This code is contributed by decode2207.
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Shortest path by removing K walls Given a 2D binary matrix of size N * M, where 0 represents an empty space while 1 represents a wall you cannot walk through. You are also given an integer K. You can walk up, down, left, or right. Given that you can remove up to K walls, return the minimum number of steps to walk from the top left c
10 min read
Number of squares in a rectangle Given a m x n matrix, count the number of squares in the matrix.Examples : Input: m = 2, n = 2Output: 5Explanation: There are 4 squares of size 1x1 + 1 square of size 2x2.Input: m = 4, n = 3Output: 20Explanation: There are 12 squares of size 1x1 + 6 squares of size 2x2 + 2 squares of size 3x3.[Naive
7 min read
Shortest path in grid with obstacles Given a grid of n rows and m columns. Currently, you are at point (0, 0) and must reach point (n-1, m-1). If any cell contains 0, it is a free cell, if it contains 1, there is an obstacle and you can't pass through the cell, and if it contains 2, that means it is free and if you step into this cell,
15 min read
Direction at last square block 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 cros
14 min read
Minimum length of the shortest path of a triangle Given N points on the plane, ( X1, Y1 ), ( X2, Y2 ), ( X3, Y3 ), ......, ( XN, YN ). The task is to calculate the minimum length of the shorter side of the triangle. and the path or points to place an isosceles triangle with any two sides of the triangle on the coordinate axis ( X axis and Y axis )
7 min read
Puzzle | Dividing a Square into N smaller squares Puzzle: Find all values of N for which one can dissect a square into N smaller squares, and outline an algorithm for doing such a dissection. Solution: The basic point to observe is a square has 4 right-angles. So, to divide it into smaller squares each of its right-angle must fall into another squa
2 min read