Advertisement
valent1n

Homework Multidimensional Arrays - Task 7

Dec 24th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.90 KB | None | 0 0
  1. using System;
  2.  
  3. class LargestArea
  4. {
  5.     static int[,] matrix =
  6.     {
  7.         {1, 3, 2, 2, 2, 4},
  8.         {3, 3, 3, 2, 4, 4},
  9.         {4, 3, 1, 2, 3, 3},
  10.         {4, 3, 1, 3, 3, 1},
  11.         {4, 3, 3, 3, 1, 1}
  12.     };
  13.     static bool[,] usedNums;
  14.     static string[,] path = new string[matrix.GetLength(0), matrix.GetLength(1)];
  15.  
  16.     static int maxLength = 1;
  17.     static int currentLength = 0;
  18.  
  19.     static int bestNum;
  20.     static int startNum;
  21.  
  22.     static void Main()
  23.     {
  24.         for (int row = 0; row < matrix.GetLength(0); row++)
  25.         {
  26.             for (int col = 0; col < matrix.GetLength(1); col++)
  27.             {
  28.                 currentLength = 0;
  29.                 startNum = matrix[row, col];
  30.                 usedNums = new bool[matrix.GetLength(0), matrix.GetLength(1)];
  31.  
  32.                 FindLargestArea(row, col);
  33.  
  34.                 if (currentLength > maxLength)
  35.                 {
  36.                     maxLength = currentLength;
  37.                     bestNum = matrix[row, col];
  38.                     FillPath();
  39.                 }
  40.             }
  41.         }
  42.  
  43.         Console.WriteLine("Best element: {0}\nBest length: {1}\n", bestNum, maxLength);
  44.         PrintBestPath();
  45.     }
  46.  
  47.     static bool OutsideTheMatrix(int row, int col)
  48.     {
  49.         bool isOutOfRange = row > matrix.GetLength(0) - 1 || row < 0 || col > matrix.GetLength(1) - 1 || col < 0;
  50.         return isOutOfRange;
  51.     }
  52.  
  53.     static void FindLargestArea(int row, int col)
  54.     {
  55.         if (OutsideTheMatrix(row, col))
  56.         {
  57.             return;
  58.         }
  59.  
  60.         if (usedNums[row, col] == true)
  61.         {
  62.             return;
  63.         }
  64.  
  65.         if (startNum != matrix[row, col])
  66.         {
  67.             return;
  68.         }
  69.  
  70.         usedNums[row, col] = true;
  71.         currentLength++;
  72.         // up
  73.         FindLargestArea(row - 1, col);
  74.         // down
  75.         FindLargestArea(row + 1, col);
  76.         // left
  77.         FindLargestArea(row, col - 1);
  78.         // right
  79.         FindLargestArea(row, col + 1);
  80.     }
  81.  
  82.     static void FillPath()
  83.     {
  84.         for (int row = 0; row < usedNums.GetLength(0); row++)
  85.         {
  86.             for (int col = 0; col < usedNums.GetLength(1); col++)
  87.             {
  88.                 if (usedNums[row, col] == true)
  89.                 {
  90.                     path[row, col] = matrix[row, col].ToString();
  91.                 }
  92.                 else
  93.                 {
  94.                     path[row, col] = "-";
  95.                 }
  96.             }
  97.         }
  98.     }
  99.  
  100.     static void PrintBestPath()
  101.     {
  102.         Console.WriteLine("The largest area of equal neighbor elements:\n");      
  103.         for (int row = 0; row < path.GetLength(0); row++)
  104.         {
  105.             for (int col = 0; col < path.GetLength(1); col++)
  106.             {
  107.                 Console.Write("{0} ", path[row, col]);
  108.             }
  109.             Console.WriteLine();
  110.         }
  111.         Console.WriteLine();
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement