Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- public class RectangleIntersection
- {
- static void Main()
- {
- var input = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse)
- .ToArray();
- int numberOfRectangles = input[0];
- int intersectionChecks = input[1];
- var rectangles = new List<Rectangles>();
- //Adding all Rectangles in a List
- for (int i = 0; i < numberOfRectangles; i++)
- {
- var rectangleInfo = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- rectangles.Add(new Rectangles(
- rectangleInfo[0],
- int.Parse(rectangleInfo[1]),
- int.Parse(rectangleInfo[2]),
- int.Parse(rectangleInfo[3]),
- int.Parse(rectangleInfo[4])
- ));
- }
- // Taking all intersection checks
- for (int i = 0; i < intersectionChecks; i++)
- {
- var rectangleIds = Console.ReadLine().Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
- // checking the List of rectangles, and taking only these which ID is contained in the Array of "IDs" we want to check for each iteration
- bool isIntersect = CheckIfRectanglesIntersect(rectangles.Where(r => rectangleIds.Contains(r.Id)).ToArray());
- Console.WriteLine(isIntersect.ToString().ToLower());
- }
- }
- private static bool CheckIfRectanglesIntersect(params Rectangles[] rectangles)
- {
- bool intersect = true;
- //checking if we have more then to rectangles
- if (rectangles.Length > 1)
- {
- // starting from 1 so I can take every time the 1 behind
- for (int i = 1; i < rectangles.Length; i++)
- {
- Rectangle firstRec = new Rectangle(rectangles[i].CorX, rectangles[i].CorY, rectangles[i].Width, rectangles[i].Height);
- Rectangle secRec = new Rectangle(rectangles[i - 1].CorX, rectangles[i - 1].CorY, rectangles[i - 1].Width, rectangles[i - 1].Height);
- var checkIfIntersect = Rectangle.Intersect(firstRec, secRec);
- // Integrated Method which checks the Intersect condition
- if (!firstRec.IntersectsWith(secRec))
- {
- intersect = false;
- break;
- }
- // I saw this check in StackOverFlow, in certain condition it returns empty rectangle which means False
- if (checkIfIntersect == Rectangle.Empty)
- {
- intersect = false;
- break;
- }
- }
- }
- // if we have just one rectangle to check is "intersected with itself". I tried to put throw new Exception here, but in Judge nothing changed
- if (rectangles.Length == 1)
- {
- return true;
- }
- return intersect;
- }
- }
- public class Rectangles
- {
- private string id;
- private int width;
- private int height;
- private int corX;
- private int corY;
- public Rectangles(string id, int width, int height, int corX, int corY)
- {
- this.id = id;
- this.width = width;
- this.height = height;
- this.corX = corX;
- this.corY = corY;
- }
- public string Id
- {
- get { return id; }
- set { id = value; }
- }
- public int Width
- {
- get { return width; }
- set { width = value; }
- }
- public int Height
- {
- get { return height; }
- set { height = value; }
- }
- public int CorX
- {
- get { return corX; }
- set { corX = value; }
- }
- public int CorY
- {
- get { return corY; }
- set { corY = value; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement