Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class LargestArea
- {
- static int[,] matrix =
- {
- {1, 3, 2, 2, 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}
- };
- static bool[,] usedNums;
- static string[,] path = new string[matrix.GetLength(0), matrix.GetLength(1)];
- static int maxLength = 1;
- static int currentLength = 0;
- static int bestNum;
- static int startNum;
- static void Main()
- {
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- currentLength = 0;
- startNum = matrix[row, col];
- usedNums = new bool[matrix.GetLength(0), matrix.GetLength(1)];
- FindLargestArea(row, col);
- if (currentLength > maxLength)
- {
- maxLength = currentLength;
- bestNum = matrix[row, col];
- FillPath();
- }
- }
- }
- Console.WriteLine("Best element: {0}\nBest length: {1}\n", bestNum, maxLength);
- PrintBestPath();
- }
- static bool OutsideTheMatrix(int row, int col)
- {
- bool isOutOfRange = row > matrix.GetLength(0) - 1 || row < 0 || col > matrix.GetLength(1) - 1 || col < 0;
- return isOutOfRange;
- }
- static void FindLargestArea(int row, int col)
- {
- if (OutsideTheMatrix(row, col))
- {
- return;
- }
- if (usedNums[row, col] == true)
- {
- return;
- }
- if (startNum != matrix[row, col])
- {
- return;
- }
- usedNums[row, col] = true;
- currentLength++;
- // up
- FindLargestArea(row - 1, col);
- // down
- FindLargestArea(row + 1, col);
- // left
- FindLargestArea(row, col - 1);
- // right
- FindLargestArea(row, col + 1);
- }
- static void FillPath()
- {
- for (int row = 0; row < usedNums.GetLength(0); row++)
- {
- for (int col = 0; col < usedNums.GetLength(1); col++)
- {
- if (usedNums[row, col] == true)
- {
- path[row, col] = matrix[row, col].ToString();
- }
- else
- {
- path[row, col] = "-";
- }
- }
- }
- }
- static void PrintBestPath()
- {
- Console.WriteLine("The largest area of equal neighbor elements:\n");
- for (int row = 0; row < path.GetLength(0); row++)
- {
- for (int col = 0; col < path.GetLength(1); col++)
- {
- Console.Write("{0} ", path[row, col]);
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement