Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class BiggestAreaOfSameNumbers
- {
- static void Main()
- {
- //define the matrix
- //original test
- int[,] myMatrix = { {1, 3, 2, 2, 1, 4},
- {3, 3, 3, 2, 4, 4},
- {4, 3, 1, 2, 3, 3},
- {4, 3, 1, 3, 3, 1},
- {4, 3, 3, 3, 1, 1}
- };
- //int[,] myMatrix = { {1, 3, 3, 3, 2, 4},
- // {3, 3, 3, 2, 4, 4},
- // {4, 3, 1, 2, 3, 3},
- // {4, 3, 1, 3, 3, 1},
- // {4, 3, 3, 3, 1, 1}
- // };
- //int[,] myMatrix = {
- // {1,2,3,4},
- // {1,2,2,2},
- // {1,2,2,2}
- // };
- //define a char matrix to store which position we have visited
- char[,] visitedMatrix = new char[myMatrix.GetLength(0), myMatrix.GetLength(1)];
- //fill the matrix with visited positions
- FillVisitedMatrix(visitedMatrix);
- //print the given matrix of numbers
- Console.WriteLine("The matrix is: ");
- PrintMatrix(myMatrix);
- //find the biggest area
- int currentBiggest = 0;
- int biggest = int.MinValue;
- int currentNumber = int.MinValue;
- int steps = 0;
- for (int row = 0; row < myMatrix.GetLength(0); row++)
- {
- for (int col = 0; col < myMatrix.GetLength(1); col++)
- {
- currentNumber = myMatrix[row, col];
- currentBiggest = FindBiggestArea(currentBiggest, currentNumber, steps, row, col, myMatrix, visitedMatrix);
- Console.WriteLine("outside biggest: {0}", currentBiggest);
- if (currentBiggest > biggest)
- {
- biggest = currentBiggest;
- }
- Console.WriteLine("BIGGEST : {0}", biggest);
- currentBiggest = 0;
- steps = 0;
- //reset the field in the visited matrix so we can check it again
- ResetVisitedMatrix(visitedMatrix);
- }
- }
- Console.WriteLine("The biggest area is: {0}", biggest);
- }
- //this method will reset the matrix with visited positions after checkinng every number is finished
- private static void ResetVisitedMatrix(char[,] visitedMatrix)
- {
- for (int row = 0; row < visitedMatrix.GetLength(0); row++)
- {
- for (int col = 0; col < visitedMatrix.GetLength(1); col++)
- {
- if (visitedMatrix[row, col] != 'u')
- {
- visitedMatrix[row, col] = 'u';
- }
- }
- }
- }
- //define a method to initially fill our visited matrix
- static void FillVisitedMatrix(char[,] visitedMatrix)
- {
- for (int row = 0; row < visitedMatrix.GetLength(0); row++)
- {
- for (int col = 0; col < visitedMatrix.GetLength(1); col++)
- {
- visitedMatrix[row, col] = 'u';
- }
- }
- }
- //define the recursive function that finds the biggest area
- static int FindBiggestArea(int currentBiggest, int currentNumber, int steps, int row, int col, int[,] myMatrix, char[,] visitedMatrix)
- {
- if (visitedMatrix[row, col] == 'v' || myMatrix[row, col] != currentNumber)
- {
- return currentBiggest;
- }
- else
- {
- visitedMatrix[row, col] = 'v';
- currentBiggest++;
- steps++;
- //define these variables for the four current biggest numbers returned from the recursive call of the function in the four directions
- int currentBiggest1 = 0;
- int currentBiggest2 = 0;
- int currentBiggest3 = 0;
- int currentBiggest4 = 0;
- if (col + 1 < visitedMatrix.GetLength(1))
- currentBiggest1 = FindBiggestArea(currentBiggest, currentNumber, steps, row, col + 1, myMatrix, visitedMatrix);
- if (row - 1 >= 0)
- currentBiggest2 = FindBiggestArea(currentBiggest, currentNumber, steps, row - 1, col, myMatrix, visitedMatrix);
- if (col - 1 >= 0)
- currentBiggest3 = FindBiggestArea(currentBiggest, currentNumber, steps, row, col - 1, myMatrix, visitedMatrix);
- if (row + 1 < visitedMatrix.GetLength(0))
- currentBiggest4 = FindBiggestArea(currentBiggest, currentNumber, steps, row + 1, col, myMatrix, visitedMatrix);
- //find the biggest value returned
- currentBiggest = FindMax(currentBiggest1, currentBiggest2, currentBiggest3, currentBiggest4);
- int[] biggestsArr = {currentBiggest1, currentBiggest2, currentBiggest3, currentBiggest4 };
- int newCurrentBiggest = currentBiggest;
- for (int i = 0; i < biggestsArr.Length; i++)
- {
- if (biggestsArr[i] != currentBiggest && biggestsArr[i] != 0)
- {
- newCurrentBiggest += biggestsArr[i] - steps;
- Console.WriteLine("The sum:{0}", currentBiggest);
- }
- }
- //
- Console.WriteLine("Number: {0} currentBiggest: {1} steps: {2}", currentNumber, currentBiggest, steps);
- Console.WriteLine("currentBiggest1: {0} currentBiggest2: {1} currentBiggest3 {2} currentBiggest4 {3}", biggestsArr[0], biggestsArr[1], biggestsArr[2], biggestsArr[3]);
- Console.WriteLine();
- return newCurrentBiggest;
- }
- }
- //
- static int FindMax(int currentBiggest1, int currentBiggest2, int currentBiggest3, int currentBiggest4)
- {
- int max = 0;
- int[] arr = {currentBiggest1, currentBiggest2, currentBiggest3, currentBiggest4 };
- for (int i = 0; i < arr.Length; i++)
- {
- if (arr[i] > max)
- {
- max = arr[i];
- }
- };
- return max;
- }
- //define a method to print the matrix
- static void PrintMatrix(int[,] matrix)
- {
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- Console.Write(matrix[row, col].ToString().PadLeft(4));
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement