Esempio n. 1
0
        /// <summary>
        /// Find the contiguous areas of fertile ground in the provided array
        /// </summary>
        /// <param name="landMass">an array representing a parcel of land</param>
        /// <param name="barrenIndicator">The integer used to represent a barren parcel of land.</param>
        /// <param name="fertileIndicator">The integer used to represent a fertile parcel of land.</param>
        /// <returns>A list of the contiguous areas of fertile land, sorted from smallest to largest.</returns>
        public List <long> FindFertileGround(int[,] landMass, int barrenIndicator, int fertileIndicator)
        {
            List <long>   fertileAreas  = new List <long>();
            Queue <Point> visitedPoints = new Queue <Point>();
            Point?        startLocation = ArrayHelpers.FindNextOccurranceIn2DArray(landMass, fertileIndicator, new Point(0, 0));

            while (startLocation != null)
            {
                landMass[startLocation.Value.X, startLocation.Value.Y] = NewFertileIndicator;
                FertileLandArea++;

                visitedPoints.Enqueue(startLocation.Value);
                while (visitedPoints.Count > 0)
                {
                    Point currentPoint = visitedPoints.Dequeue();

                    Point west = new Point(currentPoint.X - 1, currentPoint.Y);
                    if (ShouldMoveToParcel(landMass, west.X, west.Y, fertileIndicator))
                    {
                        landMass[west.X, west.Y] = NewFertileIndicator;
                        visitedPoints.Enqueue(west);
                        FertileLandArea++;
                    }

                    Point north = new Point(currentPoint.X, currentPoint.Y + 1);
                    if (ShouldMoveToParcel(landMass, north.X, north.Y, fertileIndicator))
                    {
                        landMass[north.X, north.Y] = NewFertileIndicator;
                        visitedPoints.Enqueue(north);
                        FertileLandArea++;
                    }

                    Point east = new Point(currentPoint.X + 1, currentPoint.Y);
                    if (ShouldMoveToParcel(landMass, east.X, east.Y, fertileIndicator))
                    {
                        landMass[east.X, east.Y] = NewFertileIndicator;
                        visitedPoints.Enqueue(east);
                        FertileLandArea++;
                    }

                    Point south = new Point(currentPoint.X, currentPoint.Y - 1);
                    if (ShouldMoveToParcel(landMass, south.X, south.Y, fertileIndicator))
                    {
                        landMass[south.X, south.Y] = NewFertileIndicator;
                        visitedPoints.Enqueue(south);
                        FertileLandArea++;
                    }
                }
                fertileAreas.Add(FertileLandArea);
                // Update our counters
                FertileLandArea = 0;
                NewFertileIndicator++;

                startLocation = ArrayHelpers.FindNextOccurranceIn2DArray(landMass, fertileIndicator, startLocation.Value);
            }

            fertileAreas.Sort();
            return(fertileAreas);
        }
Esempio n. 2
0
        /// <summary>
        /// Find the contiguous areas of fertile ground in the provided array
        /// </summary>
        /// <param name="landMass">an array representing a parcel of land</param>
        /// <param name="barrenIndicator">The integer used to represent a barren parcel of land.</param>
        /// <param name="fertileIndicator">The integer used to represent a fertile parcel of land.</param>
        /// <returns>A list of the contiguous areas of fertile land, sorted from smallest to largest.</returns>
        public List <long> FindFertileGround(int[,] landMass, int barrenIndicator, int fertileIndicator)
        {
            List <long> fertileLandAreas = new List <long>();
            Point?      startLocation    = ArrayHelpers.FindNextOccurranceIn2DArray(landMass, fertileIndicator, new Point(0, 0));

            while (startLocation != null)
            {
                // Now that we have our start location, we can begin "filling" in the fertile area that is not closed off by barren land
                FillFertileLand(landMass, startLocation.Value.X, startLocation.Value.Y, fertileIndicator, NewFertileIndicator);
                fertileLandAreas.Add(FertileLandArea);
                FertileLandArea = 0;
                NewFertileIndicator++;
                startLocation = ArrayHelpers.FindNextOccurranceIn2DArray(landMass, fertileIndicator, new Point(0, 0));
            }

            fertileLandAreas.Sort();
            return(fertileLandAreas);
        }