Shortest path on a Square
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 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 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)) 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 = 2; int x1 = 0, y1 = 0, x2 = 1, y2 = 0; cout << minPath(n, x1, y1, x2, y2); return 0; } |
Python 3
# 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)): 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 = 2; x1 = 0; y1 = 0x2 = 1; y2 = 0print(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))
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 = 2;
int x1 = 0, y1 = 0, x2 = 1, y2 = 0;
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)) 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 = 2; $x1 = 0 ; $y1 = 0 ; $x2 = 1 ; $y2 = 0 ; echo minPath($n, $x1, $y1, $x2, $y2); // This code is contributed by Ryuga ?> |
1
Recommended Posts:
- Shortest Path using Meet In The Middle
- Multistage Graph (Shortest Path)
- Multi Source Shortest Path in Unweighted Graph
- Check if a number is perfect square without finding square root
- Shortest distance between a Line and a Point in a 3-D plane
- Find the shortest distance between any pair of two different good nodes
- Path in a Rectangle with Circles
- Minimum Sum Path in a Triangle
- Source to destination in 2-D path with fixed sized jumps
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Nth non-Square number
- Sum of Area of all possible square inside a rectangle
- Smallest square formed with given rectangles
- Program for Area Of Square after N-th fold
- Largest hexagon that can be inscribed within a square
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




