vonko1988

EqaulArea_ver2

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