コード例 #1
0
        private static int CalculateLargestArea(short[,] matrix)
        {
            int largestArea = 0;

            for (short row = 0; row < matrix.GetLength(0); row++)
            {
                for (short col = 0; col < matrix.GetLength(1); col++)
                {
                    var neighbour  = new Neighbour(row, col, matrix[row, col]);
                    var currentMax = 0;
                    var queue      = new Queue <Neighbour>();
                    queue.Enqueue(neighbour);

                    while (queue.Count > 0)
                    {
                        var current = queue.Dequeue();
                        matrix[current.Row, current.Col] = Visited;
                        var neighbours = GetNeighbours(matrix, current);
                        currentMax++;

                        foreach (var item in neighbours)
                        {
                            if (item.Content != Visited)
                            {
                                queue.Enqueue(item);
                            }
                        }
                    }

                    if (currentMax > largestArea)
                    {
                        largestArea = currentMax;
                    }
                }
            }

            return(largestArea);
        }
コード例 #2
0
ファイル: LargestArea.cs プロジェクト: Petko-Petkov/Telerik
        private static int CalculateLargestArea(short[,] matrix)
        {
            int largestArea = 0;

            for (short row = 0; row < matrix.GetLength(0); row++)
            {
                for (short col = 0; col < matrix.GetLength(1); col++)
                {
                    var neighbour = new Neighbour(row, col, matrix[row, col]);
                    var currentMax = 0;
                    var queue = new Queue<Neighbour>();
                    queue.Enqueue(neighbour);

                    while (queue.Count > 0)
                    {
                        var current = queue.Dequeue();
                        matrix[current.Row, current.Col] = Visited;
                        var neighbours = GetNeighbours(matrix, current);
                        currentMax++;

                        foreach (var item in neighbours)
                        {
                            if (item.Content != Visited)
                            {
                                queue.Enqueue(item);
                            }
                        }
                    }

                    if (currentMax > largestArea)
                    {
                        largestArea = currentMax;
                    }
                }
            }

            return largestArea;
        }
コード例 #3
0
ファイル: LargestArea.cs プロジェクト: Petko-Petkov/Telerik
        private static List<Neighbour> GetNeighbours(short[,] matrix, Neighbour neighbour)
        {
            var list = new List<Neighbour>();

            if (InBounds(neighbour.Row + 1, neighbour.Col, matrix) && matrix[neighbour.Row + 1, neighbour.Col] == neighbour.Content)
            {
                list.Add(new Neighbour((short)(neighbour.Row + 1), neighbour.Col, matrix[neighbour.Row + 1, neighbour.Col]));
                matrix[neighbour.Row + 1, neighbour.Col] = Visited;
            }
            if (InBounds(neighbour.Row - 1, neighbour.Col, matrix) && matrix[neighbour.Row - 1, neighbour.Col] == neighbour.Content)
            {
                list.Add(new Neighbour((short)(neighbour.Row - 1), neighbour.Col, matrix[neighbour.Row - 1, neighbour.Col]));
                matrix[neighbour.Row - 1, neighbour.Col] = Visited;
            }
            if (InBounds(neighbour.Row, neighbour.Col + 1, matrix) && matrix[neighbour.Row, neighbour.Col + 1] == neighbour.Content)
            {
                list.Add(new Neighbour(neighbour.Row, (short)(neighbour.Col + 1), matrix[neighbour.Row, neighbour.Col + 1]));
                matrix[neighbour.Row, neighbour.Col + 1] = Visited;
            }
            if (InBounds(neighbour.Row, neighbour.Col - 1, matrix) && matrix[neighbour.Row, neighbour.Col - 1] == neighbour.Content)
            {
                list.Add(new Neighbour(neighbour.Row, (short)(neighbour.Col - 1),  matrix[neighbour.Row, neighbour.Col - 1]));
                matrix[neighbour.Row, neighbour.Col - 1] = Visited;
            }

            return list;
        }