Advertisement
vonko1988

EqualArea

Jul 14th, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.47 KB | None | 0 0
  1. using System;
  2.  
  3. class BiggestAreaOfSameNumbers
  4. {
  5.     static void Main()
  6.     {
  7.         //define the matrix
  8.  
  9.         //original test
  10.         int[,] myMatrix = { {1, 3, 2, 2, 1, 4},
  11.                             {3, 3, 3, 2, 4, 4},
  12.                             {4, 3, 1, 2, 3, 3},
  13.                             {4, 3, 1, 3, 3, 1},
  14.                             {4, 3, 3, 3, 1, 1}
  15.                           };
  16.  
  17.         //int[,] myMatrix = { {1, 3, 3, 3, 2, 4},
  18.         //                    {3, 3, 3, 2, 4, 4},
  19.         //                    {4, 3, 1, 2, 3, 3},
  20.         //                    {4, 3, 1, 3, 3, 1},
  21.         //                    {4, 3, 3, 3, 1, 1}
  22.         //                  };
  23.  
  24.         //int[,] myMatrix = {
  25.         //                       {1,2,3,4},
  26.         //                       {1,2,2,2},
  27.         //                       {1,2,2,2}
  28.         //                   };
  29.  
  30.         //define a char matrix to store which position we have visited
  31.         char[,] visitedMatrix = new char[myMatrix.GetLength(0), myMatrix.GetLength(1)];
  32.  
  33.         //fill the matrix with visited positions
  34.         FillVisitedMatrix(visitedMatrix);
  35.  
  36.         //print the given matrix of numbers
  37.         Console.WriteLine("The matrix is: ");
  38.         PrintMatrix(myMatrix);
  39.  
  40.         //find the biggest area
  41.         int currentBiggest = 0;
  42.         int biggest = int.MinValue;
  43.         int currentNumber = int.MinValue;
  44.         int steps = 0;
  45.  
  46.         for (int row = 0; row < myMatrix.GetLength(0); row++)
  47.         {
  48.             for (int col = 0; col < myMatrix.GetLength(1); col++)
  49.             {
  50.                 currentNumber = myMatrix[row, col];
  51.                 currentBiggest = FindBiggestArea(currentBiggest, currentNumber, steps, row, col, myMatrix, visitedMatrix);
  52.                 Console.WriteLine("outside biggest: {0}", currentBiggest);
  53.  
  54.                 if (currentBiggest > biggest)
  55.                 {
  56.                     biggest = currentBiggest;
  57.                 }
  58.  
  59.                 Console.WriteLine("BIGGEST : {0}", biggest);
  60.  
  61.                 currentBiggest = 0;
  62.                 steps = 0;
  63.  
  64.                 //reset the field in the visited matrix so we can check it again
  65.                 ResetVisitedMatrix(visitedMatrix);
  66.             }
  67.         }
  68.  
  69.         Console.WriteLine("The biggest area is: {0}", biggest);
  70.     }
  71.  
  72.     //this method will reset the matrix with visited positions after checkinng every number is finished
  73.     private static void ResetVisitedMatrix(char[,] visitedMatrix)
  74.     {
  75.         for (int row = 0; row < visitedMatrix.GetLength(0); row++)
  76.         {
  77.             for (int col = 0; col < visitedMatrix.GetLength(1); col++)
  78.             {
  79.                 if (visitedMatrix[row, col] != 'u')
  80.                 {
  81.                     visitedMatrix[row, col] = 'u';
  82.                 }
  83.             }
  84.         }
  85.     }
  86.  
  87.     //define a method to initially fill our visited matrix
  88.     static void FillVisitedMatrix(char[,] visitedMatrix)
  89.     {
  90.         for (int row = 0; row < visitedMatrix.GetLength(0); row++)
  91.         {
  92.             for (int col = 0; col < visitedMatrix.GetLength(1); col++)
  93.             {
  94.                 visitedMatrix[row, col] = 'u';
  95.             }
  96.         }
  97.     }
  98.  
  99.     //define the recursive function that finds the biggest area
  100.     static int FindBiggestArea(int currentBiggest, int currentNumber, int steps, int row, int col, int[,] myMatrix, char[,] visitedMatrix)
  101.     {
  102.         if (visitedMatrix[row, col] == 'v' || myMatrix[row, col] != currentNumber)
  103.         {
  104.             return currentBiggest;
  105.         }
  106.         else
  107.         {
  108.             visitedMatrix[row, col] = 'v';
  109.             currentBiggest++;
  110.             steps++;
  111.  
  112.             //define these variables for the four current biggest numbers returned from the recursive call of the function in the four directions
  113.             int currentBiggest1 = 0;
  114.             int currentBiggest2 = 0;
  115.             int currentBiggest3 = 0;
  116.             int currentBiggest4 = 0;
  117.  
  118.             if (col + 1 < visitedMatrix.GetLength(1))
  119.                 currentBiggest1 = FindBiggestArea(currentBiggest, currentNumber, steps, row, col + 1, myMatrix, visitedMatrix);
  120.             if (row - 1 >= 0)
  121.                 currentBiggest2 = FindBiggestArea(currentBiggest, currentNumber, steps, row - 1, col, myMatrix, visitedMatrix);
  122.             if (col - 1 >= 0)
  123.                 currentBiggest3 = FindBiggestArea(currentBiggest, currentNumber, steps, row, col - 1, myMatrix, visitedMatrix);
  124.             if (row + 1 < visitedMatrix.GetLength(0))
  125.                 currentBiggest4 = FindBiggestArea(currentBiggest, currentNumber, steps, row + 1, col, myMatrix, visitedMatrix);
  126.  
  127.             //find the biggest value returned
  128.             currentBiggest = FindMax(currentBiggest1, currentBiggest2, currentBiggest3, currentBiggest4);
  129.  
  130.             int[] biggestsArr = {currentBiggest1, currentBiggest2, currentBiggest3, currentBiggest4 };
  131.             int newCurrentBiggest = currentBiggest;
  132.  
  133.             for (int i = 0; i < biggestsArr.Length; i++)
  134.             {
  135.                 if (biggestsArr[i] != currentBiggest && biggestsArr[i] != 0)
  136.                 {
  137.                     newCurrentBiggest += biggestsArr[i] - steps;
  138.                     Console.WriteLine("The sum:{0}", currentBiggest);
  139.                 }
  140.             }
  141.  
  142.             //
  143.             Console.WriteLine("Number: {0} currentBiggest: {1} steps: {2}", currentNumber, currentBiggest, steps);
  144.             Console.WriteLine("currentBiggest1: {0} currentBiggest2: {1} currentBiggest3 {2} currentBiggest4 {3}", biggestsArr[0], biggestsArr[1], biggestsArr[2], biggestsArr[3]);
  145.             Console.WriteLine();
  146.             return newCurrentBiggest;
  147.         }
  148.     }
  149.  
  150.     //
  151.     static int FindMax(int currentBiggest1, int currentBiggest2, int currentBiggest3, int currentBiggest4)
  152.     {
  153.         int max = 0;
  154.  
  155.         int[] arr = {currentBiggest1, currentBiggest2, currentBiggest3, currentBiggest4 };
  156.  
  157.         for (int i = 0; i < arr.Length; i++)
  158.         {
  159.             if (arr[i] > max)
  160.             {
  161.                 max = arr[i];    
  162.             }
  163.         };
  164.  
  165.         return max;
  166.     }
  167.  
  168.     //define a method to print the matrix
  169.     static void PrintMatrix(int[,] matrix)
  170.     {
  171.         for (int row = 0; row < matrix.GetLength(0); row++)
  172.         {
  173.             for (int col = 0; col < matrix.GetLength(1); col++)
  174.             {
  175.                 Console.Write(matrix[row, col].ToString().PadLeft(4));
  176.             }
  177.             Console.WriteLine();
  178.         }
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement