Created
November 22, 2016 18:47
-
-
Save nasko90/eb9d05df40e0c74753d255954ffad161 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Numerics; | |
class Program | |
{ | |
static void Main() | |
{ | |
findLargestArea(matrix); | |
} | |
static int [,] matrix = inputMatrix(); | |
static int [,] inputMatrix () | |
{ | |
string[] size = Console.ReadLine().Split(' '); | |
int rows = Int32.Parse(size[0]); | |
int cols = Int32.Parse(size[1]); | |
int[,] matrix = new int[rows, cols]; | |
for (int row = 0; row < rows; row++) | |
{ | |
string[] input = Console.ReadLine().Split(' '); | |
for (int col = 0; col < cols; col++) | |
{ | |
matrix[row, col] = Int32.Parse(input[col]); | |
} | |
} | |
return matrix; | |
} | |
static int count = 0; | |
static int currentElement = 0; | |
static int bestLength = 0;static int bestNumber; | |
static void findLargestArea(int [,] matrix) | |
{ | |
for (int row = 0; row < matrix.GetLength(0); row++) | |
{ | |
for (int col = 0; col < matrix.GetLength(1); col++) | |
{ | |
currentElement = matrix[row, col]; | |
count = 0; | |
GetCurrentAreaLength(row, col); | |
if (count > bestLength) | |
{ | |
bestLength = count; | |
bestNumber = currentElement; | |
} | |
} | |
} | |
Console.WriteLine(bestLength); | |
} | |
static void GetCurrentAreaLength(int row, int col) | |
{ | |
if (row < 0 || row >= matrix.GetLength(0) || | |
col < 0 || col >= matrix.GetLength(1) || | |
matrix[row, col] == int.MinValue) | |
{ | |
return; | |
} | |
if (matrix[row,col]==currentElement) | |
{ | |
matrix[row, col] = int.MinValue; | |
count++; | |
GetCurrentAreaLength(row - 1, col); | |
GetCurrentAreaLength(row +1, col); | |
GetCurrentAreaLength(row , col-1); | |
GetCurrentAreaLength(row , col+1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment