Dados dos números enteros L y B que representan el largo y el ancho de un rectángulo y una coordenada (X, Y) que representa un punto en el plano cartesiano , la tarea es encontrar las coordenadas de todos los rectángulos que tienen un vértice como (X, Y) del dimensiones dadas.
Ejemplo:
Entrada: X=9, Y=9, L=5, B=3
Salida:
(9, 9), (14, 9), (9, 12), (14, 12)
(4, 9), (9 , 9), (4, 12), (9, 12)
(9, 6), (14, 6), (9, 9), (14, 9) (
4, 6), (9, 6), (4, 9), (9, 9)
(9, 9), (12, 9), (9, 14), (12, 14) (6, 9), (
9, 9), (6, 14) ), (9, 14)
(9, 4), (12, 4), (9, 9), (12, 9)
(6, 4), (9, 4), (6, 9), (9 , 9)
Explicación: Hay 8 rectángulos posibles tales que uno de sus vértices es (9, 9) y el largo y el ancho son 5 y 3 respectivamente como se mencionó anteriormente.Entrada: X=2, Y=3, L=4, B=1
Salida:
(2, 3), (6, 3), (2, 4), (6, 4)
(-2, 3), ( 2, 3), (-2, 4), (2, 4)
(2, 2), (6, 2), (2, 3), (6, 3) (
-2, 2), (2, 2), (-2, 3), (2, 3)
(2, 3), (3, 3), (2, 7), (3, 7)
(1, 3), (2, 3), (1, 7), (2, 7)
(2, -1), (3, -1), (2, 3), (3, 3)
(1, -1), (2, -1), (1, 3), (2, 3)
Planteamiento: Se puede observar que para un largo y ancho dado y un vértice (X,Y) son posibles ocho rectángulos como se muestra en las siguientes imágenes:
Si la longitud y el ancho dados de los rectángulos son iguales, tanto el rectángulo horizontal como el vertical representarán las mismas coordenadas. Por lo tanto, solo se pueden mostrar 4 cuadrados únicos en la imagen 1 o en la imagen 2.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ code for the above approach #include <bits/stdc++.h> using namespace std; void printHorizontal(int X, int Y, int L, int B) { cout << '(' << X << ", " << Y << "), "; cout << '(' << X + L << ", " << Y << "), "; cout << '(' << X << ", " << Y + B << "), "; cout << '(' << X + L << ", " << Y + B << ")" << endl; } void printVertical(int X, int Y, int L, int B) { cout << '(' << X << ", " << Y << "), "; cout << '(' << X + B << ", " << Y << "), "; cout << '(' << X << ", " << Y + L << "), "; cout << '(' << X + B << ", " << Y + L << ")" << endl; } // Function to find all possible rectangles void findAllRectangles(int L, int B, int X, int Y) { // First four Rectangles printHorizontal(X, Y, L, B); printHorizontal(X - L, Y, L, B); printHorizontal(X, Y - B, L, B); printHorizontal(X - L, Y - B, L, B); // If length and breadth are same // i.e, it is a square if (L == B) return; // Next four Rectangles printVertical(X, Y, L, B); printVertical(X - B, Y, L, B); printVertical(X, Y - L, L, B); printVertical(X - B, Y - L, L, B); } // Driver Code int main() { int L = 5, B = 3; int X = 9, Y = 9; findAllRectangles(L, B, X, Y); }
Java
// Java code for the above approach class GFG{ static void printHorizontal(int X, int Y, int L, int B) { System.out.print("("+ X+ ", " + Y+ "), "); System.out.print("("+ (X + L)+ ", " + Y+ "), "); System.out.print("("+ X+ ", " + (Y + B)+ "), "); System.out.print("("+ (X + L)+ ", " + (Y + B)+ ")" +"\n"); } static void printVertical(int X, int Y, int L, int B) { System.out.print("("+ X+ ", " + Y+ "), "); System.out.print("("+ (X + B)+ ", " + Y+ "), "); System.out.print("("+ X+ ", " + (Y + L)+ "), "); System.out.print("("+ (X + B)+ ", " + (Y + L)+ ")" +"\n"); } // Function to find all possible rectangles static void findAllRectangles(int L, int B, int X, int Y) { // First four Rectangles printHorizontal(X, Y, L, B); printHorizontal(X - L, Y, L, B); printHorizontal(X, Y - B, L, B); printHorizontal(X - L, Y - B, L, B); // If length and breadth are same // i.e, it is a square if (L == B) return; // Next four Rectangles printVertical(X, Y, L, B); printVertical(X - B, Y, L, B); printVertical(X, Y - L, L, B); printVertical(X - B, Y - L, L, B); } // Driver Code public static void main(String[] args) { int L = 5, B = 3; int X = 9, Y = 9; findAllRectangles(L, B, X, Y); } } // This code is contributed by shikhasingrajput
Python3
# python code for the above approach def printHorizontal(X, Y, L, B): print(f"({X}, {Y}), ", end="") print(f"({X + L}, {Y}), ", end="") print(f"('{X}, {Y + B}), ", end="") print(f"({X + L}, {Y + B})") def printVertical(X, Y, L, B): print(f"({X}, {Y}), ", end="") print(f"({X + B}, {Y}), ", end="") print(f"({X}, {Y + L}), ", end="") print(f"({X + B}, {Y + L})") # Function to find all possible rectangles def findAllRectangles(L, B, X, Y): # First four Rectangles printHorizontal(X, Y, L, B) printHorizontal(X - L, Y, L, B) printHorizontal(X, Y - B, L, B) printHorizontal(X - L, Y - B, L, B) # If length and breadth are same # i.e, it is a square if (L == B): return # Next four Rectangles printVertical(X, Y, L, B) printVertical(X - B, Y, L, B) printVertical(X, Y - L, L, B) printVertical(X - B, Y - L, L, B) # Driver Code if __name__ == "__main__": L = 5 B = 3 X = 9 Y = 9 findAllRectangles(L, B, X, Y) # This code is contributed by rakeshsahni
C#
// C# code for the above approach using System; class GFG{ static void printHorizontal(int X, int Y, int L, int B) { Console.Write("(" + X + ", " + Y + "), "); Console.Write("(" + (X + L) + ", " + Y + "), "); Console.Write("(" + X + ", " + (Y + B) + "), "); Console.Write("(" + (X + L) + ", " + (Y + B) + ")" + "\n"); } static void printVertical(int X, int Y, int L, int B) { Console.Write("(" + X + ", " + Y + "), "); Console.Write("(" + (X + B) + ", " + Y + "), "); Console.Write("(" + X + ", " + (Y + L) + "), "); Console.Write("(" + (X + B) + ", " + (Y + L) + ")" + "\n"); } // Function to find all possible rectangles static void findAllRectangles(int L, int B, int X, int Y) { // First four Rectangles printHorizontal(X, Y, L, B); printHorizontal(X - L, Y, L, B); printHorizontal(X, Y - B, L, B); printHorizontal(X - L, Y - B, L, B); // If length and breadth are same // i.e, it is a square if (L == B) return; // Next four Rectangles printVertical(X, Y, L, B); printVertical(X - B, Y, L, B); printVertical(X, Y - L, L, B); printVertical(X - B, Y - L, L, B); } // Driver Code public static void Main(String[] args) { int L = 5, B = 3; int X = 9, Y = 9; findAllRectangles(L, B, X, Y); } } // This code is contributed by shikhasingrajput
Javascript
<script> // JavaScript code for the above approach function printHorizontal(X, Y, L, B) { document.write('(' + X + ", " + Y + "), "); document.write('(' + (X + L) + ", " + Y + "), "); document.write('(' + X + ", " + (Y + B) + "), "); document.write('(' + (X + L) + ", " + (Y + B) + ")" + '<br>'); } function printVertical(X, Y, L, B) { document.write('(' + X + ", " + Y + "), "); document.write('(' + (X + B) + ", " + Y + "), "); document.write('(' + X + ", " + (Y + L) + "), "); document.write('(' + (X + B) + ", " + (Y + L) + ")" + '<br>'); } // Function to find all possible rectangles function findAllRectangles(L, B, X, Y) { // First four Rectangles printHorizontal(X, Y, L, B); printHorizontal(X - L, Y, L, B); printHorizontal(X, Y - B, L, B); printHorizontal(X - L, Y - B, L, B); // If length and breadth are same // i.e, it is a square if (L == B) return; // Next four Rectangles printVertical(X, Y, L, B); printVertical(X - B, Y, L, B); printVertical(X, Y - L, L, B); printVertical(X - B, Y - L, L, B); } // Driver Code let L = 5, B = 3; let X = 9, Y = 9; findAllRectangles(L, B, X, Y); // This code is contributed by Potta Lokesh </script>
(9, 9), (14, 9), (9, 12), (14, 12) (4, 9), (9, 9), (4, 12), (9, 12) (9, 6), (14, 6), (9, 9), (14, 9) (4, 6), (9, 6), (4, 9), (9, 9) (9, 9), (12, 9), (9, 14), (12, 14) (6, 9), (9, 9), (6, 14), (9, 14) (9, 4), (12, 4), (9, 9), (12, 9) (6, 4), (9, 4), (6, 9), (9, 9)
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por bhagyarajraj1432 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA