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); }
private List <PSlice> PerformSlice_PhaseTwo(int[,] plate) { int nextSliceId = -1; Dictionary <int, PSlice> sliceHash = new Dictionary <int, PSlice>(); // Slice Pizza for (int r = 0; r < mRows; r++) { for (int c = 0; c < mColumns; c++) { if (SlicePizzaAtPosition(plate, r, c, sliceHash, nextSliceId) == true) { nextSliceId--; } } } // Try re-slicing List <PSlice> slices = new List <PSlice>(sliceHash.Values); foreach (PSlice slice in slices) { PSlice currentSlice = sliceHash[slice.ID]; sliceHash.Remove(currentSlice.ID); currentSlice.RestoreSliceToPlate(plate, mPlate); SlicePizzaAtPosition(plate, currentSlice.RowMin, currentSlice.ColumnMin, sliceHash, currentSlice.ID); } return(new List <PSlice>(sliceHash.Values)); }
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); }
public PSlice BuildShirnkedSliceWithOverlapping(PSlice newSlice) { // Verify overlap is rect if ( // Partial column overlap ((newSlice.ColumnMin > this.ColumnMin) || (newSlice.ColumnMax < this.ColumnMax)) && // Partial row overlap ((newSlice.RowMin > this.RowMin) || (newSlice.RowMax < this.RowMax)) ) { return(null); } // Calc new overlapping slice int existingNewRowMin = this.RowMin; int existingNewRowMax = this.RowMax; int existingNewColumnMin = this.ColumnMin; int existingNewColumnMax = this.ColumnMax; // Full column overlap if ((newSlice.ColumnMin <= this.ColumnMin) && (newSlice.ColumnMax >= this.ColumnMax)) { // New slice in the middle of old slice if ((newSlice.RowMin > this.RowMin) && (newSlice.RowMax < this.RowMax)) { return(null); } // Above if (newSlice.RowMin <= this.RowMin) { existingNewRowMin = newSlice.RowMax + 1; } // Below if (newSlice.RowMax >= this.RowMax) { existingNewRowMax = newSlice.RowMin - 1; } } // Full row overlap if ((newSlice.RowMin <= this.RowMin) && (newSlice.RowMax >= this.RowMax)) { // New slice in the middle of old slice if ((newSlice.ColumnMin > this.ColumnMin) && (newSlice.ColumnMax < this.ColumnMax)) { return(null); } // Left if (newSlice.ColumnMin <= this.ColumnMin) { existingNewColumnMin = newSlice.ColumnMax + 1; } // Right if (newSlice.ColumnMax >= this.ColumnMax) { existingNewColumnMax = newSlice.ColumnMin - 1; } } if (existingNewColumnMin > existingNewColumnMax) { return(null); } if (existingNewRowMin > existingNewRowMax) { return(null); } return(new PSlice(this.ID, existingNewRowMin, existingNewRowMax, existingNewColumnMin, existingNewColumnMax)); }