Ejemplo n.º 1
0
        private bool SlicePizzaAtPosition(int[,] plate, int r, int c, Dictionary <int, PSlice> sliceHash, int nextSliceId)
        {
            if (plate[r, c] < 0)
            {
                return(false);
            }

            PSlice maxSlice = GetMaxSliceExtentionAt(plate, sliceHash, r, c, nextSliceId);

            if (maxSlice != null)
            {
                // Shrink existing slices
                Dictionary <int, int> sliceContent = maxSlice.GetSliceContent(plate);
                foreach (int overlapSliceId in sliceContent.Keys)
                {
                    if (overlapSliceId > 0)
                    {
                        continue;
                    }

                    PSlice existingSlice        = sliceHash[overlapSliceId];
                    PSlice existingAfterOverlap = existingSlice.BuildShirnkedSliceWithOverlapping(maxSlice);
                    sliceHash[existingSlice.ID] = existingAfterOverlap;
                }

                maxSlice.RemoveSliceFromPlate(plate);
                sliceHash.Add(maxSlice.ID, maxSlice);

                return(true);
            }

            return(false);
        }
Ejemplo n.º 2
0
        private PSlice GetMaxSliceExtentionAt(int[,] plate, Dictionary <int, PSlice> sliceHash, int row, int column, int nextSliceId)
        {
            PSlice maxSlice            = null;
            int    maxSliceIngredients = 0;

            for (int minRow = row; minRow >= Math.Max(0, row - this.mMaxSliceSize); minRow--)
            {
                for (int maxRow = row; maxRow < Math.Min(row + this.mMaxSliceSize + 1, mRows); maxRow++)
                {
                    for (int minCol = column; minCol >= Math.Max(0, column - this.mMaxSliceSize); minCol--)
                    {
                        for (int maxCol = column; maxCol < Math.Min(column + this.mMaxSliceSize + 1, mColumns); maxCol++)
                        {
                            int isValidSlice = IsValidSlice(this.mPlate, minRow, maxRow, minCol, maxCol);
                            if ((isValidSlice == CHECK_SLICE_TOO_BIG) || (isValidSlice == CHECK_SLICE_INVALID_SLICE))
                            {
                                break;
                            }

                            if (isValidSlice != CHECK_SLICE_VALID)
                            {
                                continue;
                            }

                            PSlice newSlice = new PSlice(nextSliceId, minRow, maxRow, minCol, maxCol);

                            // The new slice contains positions previously not in any slice
                            int newSliceIngredients = newSlice.CountIngredients(plate);
                            if (newSliceIngredients == 0)
                            {
                                continue;
                            }

                            // Check overlapping slices are still valid slices
                            Dictionary <int, int> sliceContent = newSlice.GetSliceContent(plate);
                            bool isValidOverlap = true;
                            foreach (int overlapSliceId in sliceContent.Keys)
                            {
                                if (overlapSliceId > 0)
                                {
                                    continue;
                                }

                                PSlice existingSlice        = sliceHash[overlapSliceId];
                                PSlice existingAfterOverlap = existingSlice.BuildShirnkedSliceWithOverlapping(newSlice);
                                if (existingAfterOverlap == null)
                                {
                                    isValidOverlap = false;
                                    break;
                                }

                                if (this.IsValidSlice(this.mPlate,
                                                      existingAfterOverlap.RowMin, existingAfterOverlap.RowMax,
                                                      existingAfterOverlap.ColumnMin, existingAfterOverlap.ColumnMax) != CHECK_SLICE_VALID)
                                {
                                    isValidOverlap = false;
                                    break;
                                }
                            }
                            if (isValidOverlap == false)
                            {
                                continue;
                            }

                            // Check if the new slice is bettter than existing max
                            if (maxSlice == null)
                            {
                                maxSlice            = newSlice;
                                maxSliceIngredients = newSliceIngredients;
                            }
                            else if (maxSliceIngredients < newSliceIngredients)
                            {
                                maxSlice            = newSlice;
                                maxSliceIngredients = newSliceIngredients;
                            }
                        }
                    }
                }
            }

            return(maxSlice);
        }