Exemple #1
0
        public bool DoesOverlap(PizzaSlice other)
        {
            var overlapX = BottomRight.X >= other.TopLeft.X && TopLeft.X <= other.BottomRight.X;
            var overlapY = BottomRight.Y >= other.TopLeft.Y && TopLeft.Y <= other.BottomRight.Y;

            return(overlapX && overlapY);
        }
Exemple #2
0
        private PizzaSlice EnlargeSlice(bool[,] pizzaCells, PizzaSlice slice)
        {
            bool enlarged = true;

            while (enlarged)
            {
                PizzaSlice newSlice;
                if (TryEnlargedSliceByWidth(pizzaCells, slice, out newSlice))
                {
                    enlarged = true;
                    slice    = newSlice;
                    MarkSlice(pizzaCells, slice);
                }
                else if (TryEnlargedSliceByHeight(pizzaCells, slice, out newSlice))
                {
                    enlarged = true;
                    slice    = newSlice;
                    MarkSlice(pizzaCells, slice);
                }
                else
                {
                    enlarged = false;
                }
            }

            return(slice);
        }
        protected override IEnumerable <PizzaSlice> Solve(int xStart, int yStart, int maxX, int maxY)
        {
            if (xStart > maxX || yStart > maxY)
            {
                return new PizzaSlice[] {}
            }
            ;

            var results = new List <PizzaSlice>();
            int xEnd    = xStart;
            int yEnd    = yStart;

            while (true)
            {
                var currentSlice = new PizzaSlice(xStart, yStart, xEnd, yEnd);

                if (IsSliceTooLarge(currentSlice))
                {
                    results.AddRange(Solve(xEnd + 1, yStart, maxX, yEnd));
                    results.AddRange(Solve(xStart, yEnd + 1, maxX, maxY));
                    return(results);
                }

                if (IsEnoughIngredients(currentSlice))
                {
                    results.Add(currentSlice);
                    results.AddRange(Solve(xEnd + 1, yStart, maxX, yEnd));
                    results.AddRange(Solve(xStart, yEnd + 1, maxX, maxY));
                    return(results);
                }

                if (yEnd == maxY && xEnd == maxX)
                {
                    return new PizzaSlice[] {}
                }
                ;

                if (yEnd == maxY)
                {
                    xEnd++;
                }
                else if (xEnd == maxX)
                {
                    yEnd++;
                }
                else if (currentSlice.Height >= currentSlice.Width)
                {
                    xEnd++;
                }
                else
                {
                    yEnd++;
                }
            }
        }
    }
}
Exemple #4
0
        private static int GetCountInSlice(int[,] table, PizzaSlice slice)
        {
            var totalSum   = table[slice.BottomRight.X, slice.BottomRight.Y];
            var topSum     = slice.TopLeft.Y == 0 ? 0 : table[slice.BottomRight.X, slice.TopLeft.Y - 1];
            var leftSum    = slice.TopLeft.X == 0 ? 0 : table[slice.TopLeft.X - 1, slice.BottomRight.Y];
            var topLeftSum = slice.TopLeft.X == 0 || slice.TopLeft.Y == 0 ? 0 : table[slice.TopLeft.X - 1, slice.TopLeft.Y - 1];

            return(totalSum - leftSum - topSum + topLeftSum);
        }
Exemple #5
0
 private static void MarkSlice(bool[,] pizzaCells, PizzaSlice slice)
 {
     for (int y = slice.TopLeft.Y; y <= slice.BottomRight.Y; y++)
     {
         for (int x = slice.TopLeft.X; x <= slice.BottomRight.X; x++)
         {
             pizzaCells[x, y] = true;
         }
     }
 }
Exemple #6
0
        private bool TryEnlargedSliceByHeight(bool[,] pizzaCells, PizzaSlice slice, out PizzaSlice newSlice)
        {
            if (slice.Width * (slice.Height + 1) > Pizza.MaxSliceSize)
            {
                newSlice = slice;
                return(false);
            }

            if (slice.TopLeft.Y != 0)
            {
                bool succeed = true;
                for (int x = slice.TopLeft.X; x <= slice.BottomRight.X; x++)
                {
                    if (pizzaCells[x, slice.TopLeft.Y - 1])
                    {
                        succeed = false;
                        break;
                    }
                }

                if (succeed)
                {
                    Coordinate newTopLeft = new Coordinate(slice.TopLeft.X, slice.TopLeft.Y - 1);
                    newSlice = new PizzaSlice(newTopLeft, slice.BottomRight);
                    return(true);
                }
            }

            if (slice.BottomRight.Y != Pizza.YLength - 1)
            {
                bool succeed = true;
                for (int x = slice.TopLeft.X; x <= slice.BottomRight.X; x++)
                {
                    if (pizzaCells[x, slice.BottomRight.Y + 1])
                    {
                        succeed = false;
                        break;
                    }
                }

                if (succeed)
                {
                    Coordinate newBottomRight = new Coordinate(slice.BottomRight.X, slice.BottomRight.Y + 1);
                    newSlice = new PizzaSlice(slice.TopLeft, newBottomRight);
                    return(true);
                }
            }

            newSlice = slice;
            return(false);
        }
Exemple #7
0
        private bool TryEnlargedSliceByWidth(bool[,] pizzaCells, PizzaSlice slice, out PizzaSlice newSlice)
        {
            if (slice.Height * (slice.Width + 1) > Pizza.MaxSliceSize)
            {
                newSlice = slice;
                return(false);
            }

            if (slice.TopLeft.X != 0)
            {
                bool succeed = true;
                for (int y = slice.TopLeft.Y; y <= slice.BottomRight.Y; y++)
                {
                    if (pizzaCells[slice.TopLeft.X - 1, y])
                    {
                        succeed = false;
                        break;
                    }
                }

                if (succeed)
                {
                    Coordinate newTopLeft = new Coordinate(slice.TopLeft.X - 1, slice.TopLeft.Y);
                    newSlice = new PizzaSlice(newTopLeft, slice.BottomRight);
                    return(true);
                }
            }

            if (slice.BottomRight.X != Pizza.XLength - 1)
            {
                bool succeed = true;
                for (int y = slice.TopLeft.Y; y <= slice.BottomRight.Y; y++)
                {
                    if (pizzaCells[slice.BottomRight.X + 1, y])
                    {
                        succeed = false;
                        break;
                    }
                }

                if (succeed)
                {
                    Coordinate newBottomRight = new Coordinate(slice.BottomRight.X + 1, slice.BottomRight.Y);
                    newSlice = new PizzaSlice(slice.TopLeft, newBottomRight);
                    return(true);
                }
            }

            newSlice = slice;
            return(false);
        }
Exemple #8
0
        public IEnumerable <PizzaSlice> EnlargeSlices(List <PizzaSlice> slices, Random rand)
        {
            bool[,] pizzaCells = GetUsedCells(slices);
            List <PizzaSlice> newSlices = new List <PizzaSlice>();

            while (slices.Any())
            {
                int        index    = rand.Next() % slices.Count;
                PizzaSlice newSlice = EnlargeSlice(pizzaCells, slices[index]);
                newSlices.Add(newSlice);
                slices.RemoveAt(index);
            }

            return(newSlices);
        }
Exemple #9
0
        public override bool Equals(object obj)
        {
            PizzaSlice other = obj as PizzaSlice;

            return(other != null && Equals(this.TopLeft, other.TopLeft) && Equals(this.BottomRight, other.BottomRight));
        }
Exemple #10
0
        protected override IEnumerable <PizzaSlice> Solve(int xStart, int yStart, int maxX, int maxY)
        {
            if (xStart > maxX || yStart > maxY)
            {
                return new PizzaSlice[] { }
            }
            ;

            var results = new List <PizzaSlice>();
            int xEnd    = xStart;
            int yEnd    = yStart;

            int xIncrement = 0;
            int yIncrement = 0;

            while (true)
            {
                var currentSlice = new PizzaSlice(xStart, yStart, xEnd, yEnd);

                if (IsSliceTooLarge(currentSlice))
                {
                    var x1 = Solve(xEnd + 1, yStart, maxX, yEnd);
                    var x2 = Solve(xStart, yEnd + 1, maxX, maxY);

                    if (x1.Count() + x2.Count() == 0)
                    {
                        if (yStart == maxY)
                        {
                            xStart++;
                        }
                        else
                        {
                            yStart++;
                        }
                        continue;
                    }

                    results.AddRange(x1);
                    results.AddRange(x2);
                    return(results);
                }

                if (IsEnoughIngredients(currentSlice))
                {
                    results.Add(currentSlice);
                    results.AddRange(Solve(xEnd + 1, yStart, maxX, yEnd));
                    results.AddRange(Solve(xStart, yEnd + 1, maxX, maxY));
                    return(results);
                }

                if (yEnd == maxY && xEnd == maxX)
                {
                    return new PizzaSlice[] { }
                }
                ;

                if (yEnd == maxY)
                {
                    xEnd++;

                    continue;
                }
                if (xEnd == maxX)
                {
                    yEnd++;
                    continue;
                }

                if (xIncrement == 0 && yIncrement == 0)
                {
                    xIncrement = 1;
                    xEnd++;
                    yIncrement = 0;
                }
                else if (xIncrement == 1 && yIncrement == 0)
                {
                    xIncrement = 0;
                    xEnd--;
                    yIncrement = 1;
                    yEnd++;
                }
                else if (xIncrement == 0 && yIncrement == 1)
                {
                    xIncrement = 1;
                    xEnd++;
                    yIncrement = 1;
                }
                else if (xIncrement == 1 && yIncrement == 1)
                {
                    xIncrement = 1;
                    xEnd++;
                    yIncrement = 0;
                }
            }
        }
Exemple #11
0
 private bool CheckSlice(PizzaSlice slice)
 {
     return(IsEnoughIngredients(slice) && !IsSliceTooLarge(slice));
 }
Exemple #12
0
 public bool IsSliceTooLarge(PizzaSlice slice)
 {
     return(slice.Size > Pizza.MaxSliceSize);
 }
Exemple #13
0
 public bool IsEnoughIngredients(PizzaSlice slice)
 {
     return(GetMushroomsInSlice(slice) >= Pizza.MinIngredientNum && GetTomatoInSlice(slice) >= Pizza.MinIngredientNum);
 }
Exemple #14
0
 public bool IsSliceValid(PizzaSlice slice)
 {
     return(!IsSliceTooLarge(slice) && IsEnoughIngredients(slice));
 }
Exemple #15
0
 public int GetTomatoInSlice(PizzaSlice slice)
 {
     return(GetCountInSlice(tomatoTable, slice));
 }
Exemple #16
0
 public int GetMushroomsInSlice(PizzaSlice slice)
 {
     return(GetCountInSlice(mushroomTable, slice));
 }