Advertisement
Guest User

Matrix.cs

a guest
Dec 5th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.33 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Matrix
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int[][] matrix = new int[][]
  10.             {
  11.                 new int[] { 1, 1, 1, 1, 1, 1 },
  12.                 new int[] { 1, 1, 1, 1, 1, 1 },
  13.                 new int[] { 1, 1, 1, 1, 1, 1 },
  14.                 new int[] { 1, 1, 1, 1, 1, 1 },
  15.                 new int[] { 1, 1, 1, 1, 1, 1 },
  16.                 new int[] { 1, 1, 1, 1, 1, 1 }
  17.             };
  18.  
  19.             Rectangle largestRectangle = findLargestRectangle(matrix);
  20.  
  21.             Console.WriteLine("The largest rectangle is:");
  22.             Console.WriteLine("  Area: " + largestRectangle.Size());
  23.             Console.WriteLine("  Boundaries:");
  24.             Console.WriteLine("    A: (" + largestRectangle.a.x + "; " + largestRectangle.a.y + ")");
  25.             Console.WriteLine("    B: (" + largestRectangle.b.x + "; " + largestRectangle.b.y + ")");
  26.         }
  27.  
  28.         static Rectangle findLargestRectangle(int[][] matrix)
  29.         {
  30.             Rectangle largestRectangle = null;
  31.  
  32.             for (int ya=0; ya<matrix.Length; ya++)
  33.             {
  34.                 for (int xa = 0; xa < matrix[ya].Length; xa++)
  35.                 {
  36.                     for (int yb = 0; yb < matrix.Length; yb++)
  37.                     {
  38.                         for (int xb = 0; xb < matrix[yb].Length; xb++)
  39.                         {
  40.                             Rectangle rectangle = new Rectangle(xa, ya, xb, yb);
  41.  
  42.                             if (IsFilled(matrix, rectangle))
  43.                             {
  44.                                 if (largestRectangle == null || largestRectangle.Size() < rectangle.Size())
  45.                                 {
  46.                                     largestRectangle = rectangle;
  47.                                 }
  48.                             }
  49.                         }
  50.                     }
  51.                 }
  52.             }
  53.  
  54.             return largestRectangle;
  55.         }
  56.  
  57.         static bool IsFilled(int[][] matrix, Rectangle rectangle)
  58.         {
  59.             int minX = Math.Min(rectangle.a.x, rectangle.b.x);
  60.             int maxX = Math.Max(rectangle.a.x, rectangle.b.x);
  61.             int minY = Math.Min(rectangle.a.y, rectangle.b.y);
  62.             int maxY = Math.Max(rectangle.a.y, rectangle.b.y);
  63.  
  64.             for (int x=minX; x<=maxX; x++)
  65.             {
  66.                 for (int y = minY; y <= maxY; y++)
  67.                 {
  68.                     if (matrix[y][x] == 0)
  69.                     {
  70.                         return false;
  71.                     }
  72.                 }
  73.             }
  74.  
  75.             return true;
  76.         }
  77.     }
  78.  
  79.     class Rectangle
  80.     {
  81.         public Point a, b;
  82.  
  83.         public Rectangle(Point a, Point b)
  84.         {
  85.             this.a = a;
  86.             this.b = b;
  87.         }
  88.  
  89.         public Rectangle(int xa, int ya, int xb, int yb)
  90.         {
  91.             a = new Point(xa, ya);
  92.             b = new Point(xb, yb);
  93.         }
  94.  
  95.         public int Size()
  96.         {
  97.             int width = Math.Max(a.x, b.x) - Math.Min(a.x, b.x);
  98.             int height = Math.Max(a.y, b.y) - Math.Min(a.y, b.y);
  99.  
  100.             return width * height;
  101.         }
  102.     }
  103.  
  104.     class Point
  105.     {
  106.         public int x, y;
  107.  
  108.         public Point(int x, int y)
  109.         {
  110.             this.x = x;
  111.             this.y = y;
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement