Exemplo n.º 1
0
        public int CompareTo(object obj)
        {
            if (obj == null)
            {
                return(-1);
            }

            ConnectedArea otherConnectedArea = obj as ConnectedArea;

            if (otherConnectedArea != null)
            {
                int bySize = -1 * this.Size.CompareTo(otherConnectedArea.Size);
                int byRow  = this.Row.CompareTo(otherConnectedArea.Row);
                int byCol  = this.Col.CompareTo(otherConnectedArea.Col);

                if (bySize == 0)
                {
                    if (byRow == 0)
                    {
                        return(byCol);
                    }

                    return(byRow);
                }

                return(bySize);
            }
            else
            {
                throw new ArgumentException("Object is not a ConnectedArea.");
            }
        }
        public static void Main(string[] args)
        {
            char[,] matrix1 = new char[, ]
            {
                { ' ', ' ', ' ', '*', ' ', ' ', ' ', '*', ' ' },
                { ' ', ' ', ' ', '*', ' ', ' ', ' ', '*', ' ' },
                { ' ', ' ', ' ', '*', ' ', ' ', ' ', '*', ' ' },
                { ' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' ' }
            };
            char[,] matrix2 = new char[, ]
            {
                { '*', ' ', ' ', '*', ' ', ' ', ' ', '*', ' ', ' ' },
                { '*', ' ', ' ', '*', ' ', ' ', ' ', '*', ' ', ' ' },
                { '*', ' ', ' ', '*', '*', '*', '*', '*', ' ', ' ' },
                { '*', ' ', ' ', '*', ' ', ' ', ' ', '*', ' ', ' ' },
                { '*', ' ', ' ', '*', ' ', ' ', ' ', '*', ' ', ' ' }
            };

            matrix = matrix1;
            PrintArray(matrix);

            TraverseAllAreas();
            PrintResult();

            Console.WriteLine();

            matrix = matrix2;
            PrintArray(matrix);
            currentConnectedArea = null;
            connectedAreas.Clear();

            TraverseAllAreas();
            PrintResult();
        }
        private static void TraverseAllAreas()
        {
            if (!FindFirstTraversableCell(matrix))
            {
                return;
            }

            FindFirstTraversableCell(matrix);
            TraverseArea(currentConnectedArea.Row, currentConnectedArea.Col);
            connectedAreas.Add(currentConnectedArea);
            currentConnectedArea = null;

            TraverseAllAreas();
        }
        private static bool FindFirstTraversableCell(char[,] matrix)
        {
            for (int row = 0; row < matrix.GetLength(0); row++)
            {
                for (int col = 0; col < matrix.GetLength(1); col++)
                {
                    if (matrix[row, col] == ' ')
                    {
                        currentConnectedArea = new ConnectedArea();

                        currentConnectedArea.Row = row;
                        currentConnectedArea.Col = col;

                        return(true);
                    }
                }
            }

            return(false);
        }