public static List <ValidSlice> assemblePizza(List <ValidSlice> finalSortedValidSlices, ref List <ValidSlice> pizza, int startFromRow, int startFromColumn, ref Stack <int> indices, int pizzaRows, int pizzaColumns) { //base case if (pizza.Count != 0 && (pizza[pizza.Count - 1].endingRow + 1) == pizzaRows && (pizza[pizza.Count - 1].endingColumn + 1) == pizzaColumns) { return(pizza); } //recursive case else { ValidSlice nextSlice = findNextSlice(finalSortedValidSlices, startFromRow, startFromColumn, ref indices); if (nextSlice == null) { ValidSlice tmp = pizza [pizza.Count - 1]; pizza.RemoveAt(pizza.Count - 1); nextSlice = findNextSlice(finalSortedValidSlices, tmp.startingRow, tmp.startingColumn, ref indices); // try adding a variable named mistakeRouteDepth and make pops as much as mistakeRouteDepth indices.Pop(); indices.Pop(); pizza.Add(nextSlice); if (nextSlice.endingColumn + 1 < pizzaColumns) { return(assemblePizza(finalSortedValidSlices, ref pizza, nextSlice.startingRow, nextSlice.endingColumn + 1, ref indices, pizzaRows, pizzaColumns)); } else { return(assemblePizza(finalSortedValidSlices, ref pizza, nextSlice.endingRow + 1, 0, ref indices, pizzaRows, pizzaColumns)); } } else { pizza.Add(nextSlice); if (nextSlice.endingColumn + 1 < pizzaColumns) { return(assemblePizza(finalSortedValidSlices, ref pizza, nextSlice.startingRow, nextSlice.endingColumn + 1, ref indices, pizzaRows, pizzaColumns)); } else { return(assemblePizza(finalSortedValidSlices, ref pizza, nextSlice.endingRow + 1, 0, ref indices, pizzaRows, pizzaColumns)); } } } }
public static List <ValidSlice> createValidSlices(char[,] pizza, List <char[, ]> possibleSlicesToCut, int minimumIngredientsPerSlice) { List <ValidSlice> validSlices = new List <ValidSlice>(); int rowsOfPizza = pizza.GetLength(0); int columnsOfPizza = pizza.GetLength(1); foreach (char[,] slice in possibleSlicesToCut) { int rowsOfSlice = slice.GetLength(0); int columnsOfSlice = slice.GetLength(1); //the following two fors are moving the search window for (int i = 0; i <= (rowsOfPizza - rowsOfSlice); i++) { for (int j = 0; j <= (columnsOfPizza - columnsOfSlice); j++) { //check if it is a valid slice add it the ValidSlice list char[,] tmp = isValidSlice(pizza, i, j, rowsOfSlice - 1 + i, columnsOfSlice - 1 + j, minimumIngredientsPerSlice); if (tmp == null) { continue; } else { ValidSlice validSlice = new ValidSlice(); validSlice.startingRow = i; validSlice.startingColumn = j; validSlice.endingRow = rowsOfSlice - 1 + i; validSlice.endingColumn = columnsOfSlice - 1 + j; validSlice.slice = tmp; validSlices.Add(validSlice); } } } } return(validSlices); }