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++
#include <bits/stdc++.h>
using namespace std;
int minPath( int n, int x1, int y1, int x2, int y2)
{
if ((x1 <= x2 && y1 <= y2) || (x1 >= x2 && y1 >= y2))
{
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));
}
int main()
{
int n = 4;
int x1 = 2, y1 = 0, x2 = 3, y2 = 4;
cout << minPath(n, x1, y1, x2, y2);
return 0;
}
|
Java
class GFG{
static int minPath( int n, int x1, int y1,
int x2, int y2)
{
if ((x1 <= x2 && y1 <= y2) ||
(x1 >= x2 && y1 >= y2))
{
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));
}
public static void main(String[] args)
{
int n = 4 ;
int x1 = 2 , y1 = 0 , x2 = 3 , y2 = 4 ;
System.out.println(minPath(n, x1, y1, x2, y2));
}
}
|
Python3
def minPath(n, x1, y1, x2, y2):
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));
n = 4 ; x1 = 2 ; y1 = 0
x2 = 3 ; y2 = 4
print (minPath(n, x1, y1, x2, y2))
|
C#
using System;
class GFG
{
static int minPath( int n, int x1, int y1,
int x2, int y2)
{
if ((x1 <= x2 && y1 <= y2) || (x1 >= x2 && y1 >= y2))
{
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));
}
public static void Main()
{
int n = 4;
int x1 = 2, y1 = 0, x2 = 3, y2 = 4;
Console.Write(minPath(n, x1, y1, x2, y2));
}
}
|
PHP
<?php
function minPath( $n , $x1 , $y1 , $x2 , $y2 )
{
if (( $x1 <= $x2 && $y1 <= $y2 ) || ( $x1 >= $x2 && $y1 >= $y2 ))
{
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 ));
}
$n = 4;
$x1 = 2 ;
$y1 = 0 ;
$x2 = 3 ;
$y2 = 4 ;
echo minPath( $n , $x1 , $y1 , $x2 , $y2 );
?>
|
Javascript
<script>
function minPath(n, x1, y1, x2, y2)
{
if ((x1 <= x2 && y1 <= y2) || (x1 >= x2 && y1 >= y2))
{
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));
}
let n = 4;
let x1 = 2, y1 = 0, x2 = 3, y2 = 4;
document.write(minPath(n, x1, y1, x2, y2));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...