Exemple #1
0
        static void Main(string[] args)
        {
            string case1 =
                "0000000000\n"
                + "0011100000\n"
                + "0011111000\n"
                + "0010001000\n"
                + "0011111000\n"
                + "0000101000\n"
                + "0000101000\n"
                + "0000111000\n"
                + "0000000000\n"
                + "0000000000\n";

            string empty =
                "0000000000\n"
                + "0000000000\n"
                + "0000000000\n"
                + "0000000000\n"
                + "0000000000\n"
                + "0000000000\n"
                + "0000000000\n"
                + "0000000000\n"
                + "0000000000\n"
                + "0000000000\n";

            string full =
                "1111111111\n"
                + "1111111111\n"
                + "1111111111\n"
                + "1111111111\n"
                + "1111111111\n"
                + "1111111111\n"
                + "1111111111\n"
                + "1111111111\n"
                + "1111111111\n"
                + "1111111111\n";

            List <string> cases = new List <string> {
                case1, empty, full
            };

            foreach (string input in cases)
            {
                Console.Write(input);

                bool[,] blob = Util.InputToMatrix(input);

                BoundaryFinder finder     = new BoundaryFinder();
                Boundaries     boundaries = finder.FindBoundaries(blob, 10);

                Console.WriteLine($"Top: {boundaries.Top}");
                Console.WriteLine($"Left: {boundaries.Left}");
                Console.WriteLine($"Bottom: {boundaries.Bottom}");
                Console.WriteLine($"Right: {boundaries.Right}");
                Console.WriteLine($"Reads: {boundaries.CellReads}");
            }

            Console.ReadLine();
        }
        public Boundaries FindBoundaries(bool[,] blob, int size)
        {
            Grid matrix = new Grid(blob, size);

            Queue <GridCell> q     = new Queue <GridCell>();
            GridCell         first = FindOne(matrix);

            if (first == null)
            {
                return(new Boundaries
                {
                    Top = -1,
                    Bottom = -1,
                    Left = -1,
                    Right = -1,
                    CellReads = matrix.GetAccessCount()
                });
            }

            q.Enqueue(first);

            Boundaries boundaries = new Boundaries
            {
                Top    = first.I,
                Bottom = first.I,
                Left   = first.J,
                Right  = first.J
            };

            // While Queue is not empty, go through cells
            while (q.Count > 0)
            {
                GridCell current = q.Dequeue();

                if (current.I < boundaries.Top)
                {
                    boundaries.Top = current.I;
                }
                if (current.I > boundaries.Bottom)
                {
                    boundaries.Bottom = current.I;
                }

                if (current.J < boundaries.Left)
                {
                    boundaries.Left = current.J;
                }
                if (current.J > boundaries.Right)
                {
                    boundaries.Right = current.J;
                }

                List <GridCell> toVisit = GetSurroundingCells(matrix, current);

                foreach (GridCell cell in toVisit)
                {
                    if (!matrix.HasBeenVisited(cell.I, cell.J) && matrix.Visit(cell.I, cell.J))
                    {
                        q.Enqueue(cell);
                    }
                }
            }

            boundaries.CellReads = matrix.GetAccessCount();
            return(boundaries);
        }