Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Matrix
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[][] matrix = new int[][]
- {
- new int[] { 1, 1, 1, 1, 1, 1 },
- new int[] { 1, 1, 1, 1, 1, 1 },
- new int[] { 1, 1, 1, 1, 1, 1 },
- new int[] { 1, 1, 1, 1, 1, 1 },
- new int[] { 1, 1, 1, 1, 1, 1 },
- new int[] { 1, 1, 1, 1, 1, 1 }
- };
- Rectangle largestRectangle = findLargestRectangle(matrix);
- Console.WriteLine("The largest rectangle is:");
- Console.WriteLine(" Area: " + largestRectangle.Size());
- Console.WriteLine(" Boundaries:");
- Console.WriteLine(" A: (" + largestRectangle.a.x + "; " + largestRectangle.a.y + ")");
- Console.WriteLine(" B: (" + largestRectangle.b.x + "; " + largestRectangle.b.y + ")");
- }
- static Rectangle findLargestRectangle(int[][] matrix)
- {
- Rectangle largestRectangle = null;
- for (int ya=0; ya<matrix.Length; ya++)
- {
- for (int xa = 0; xa < matrix[ya].Length; xa++)
- {
- for (int yb = 0; yb < matrix.Length; yb++)
- {
- for (int xb = 0; xb < matrix[yb].Length; xb++)
- {
- Rectangle rectangle = new Rectangle(xa, ya, xb, yb);
- if (IsFilled(matrix, rectangle))
- {
- if (largestRectangle == null || largestRectangle.Size() < rectangle.Size())
- {
- largestRectangle = rectangle;
- }
- }
- }
- }
- }
- }
- return largestRectangle;
- }
- static bool IsFilled(int[][] matrix, Rectangle rectangle)
- {
- int minX = Math.Min(rectangle.a.x, rectangle.b.x);
- int maxX = Math.Max(rectangle.a.x, rectangle.b.x);
- int minY = Math.Min(rectangle.a.y, rectangle.b.y);
- int maxY = Math.Max(rectangle.a.y, rectangle.b.y);
- for (int x=minX; x<=maxX; x++)
- {
- for (int y = minY; y <= maxY; y++)
- {
- if (matrix[y][x] == 0)
- {
- return false;
- }
- }
- }
- return true;
- }
- }
- class Rectangle
- {
- public Point a, b;
- public Rectangle(Point a, Point b)
- {
- this.a = a;
- this.b = b;
- }
- public Rectangle(int xa, int ya, int xb, int yb)
- {
- a = new Point(xa, ya);
- b = new Point(xb, yb);
- }
- public int Size()
- {
- int width = Math.Max(a.x, b.x) - Math.Min(a.x, b.x);
- int height = Math.Max(a.y, b.y) - Math.Min(a.y, b.y);
- return width * height;
- }
- }
- class Point
- {
- public int x, y;
- public Point(int x, int y)
- {
- this.x = x;
- this.y = y;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement