1
var ways = function (pizza, k) {2
const height = pizza.length;3
const width = pizza[0].length;7
const dfs = (slices, startRow, startCol) => {8
// if we cut the entire pizza return 09
if (startRow >= height || startCol >= width) return 0;11
// see if the parameters of the DFS has already been seen12
// if i has been seen, return the value13
const key = [slices, startRow, startCol].join("-");14
if (cache[key]) return cache[key];16
// if there is only 1 slice left to be made17
// find out if there is an apple in the slice19
for (let row = startRow; row < height; row++) {20
for (let col = startCol; col < width; col++) {21
if (pizza[row][col] === APPLE) {22
return (cache[key] = 1);26
return (cache[key] = 0);29
// create a variable to total the number of ways31
// create a variable to see if we have found an apple33
// decrement the number of slices left to be made36
// try to cut horizontally checking if we have found an apple37
// if we have found an apple keep cutting every row after that row38
for (let row = startRow; row < height; row++) {39
// to find an apple we search the current row if it has an apple in it40
// this will only run as long as we have not found an apple and in a current column41
for (let col = startCol; col < width && !hasApple; col++) {42
if (pizza[row][col] === APPLE) {47
// if we have found an apple cut the pizza horizontally49
totalWays += dfs(slices, row + 1, startCol);53
// same as above except this time we cut vertically55
for (let col = startCol; col < width; col++) {56
for (let row = startRow; row < height && !hasApple; row++) {57
if (pizza[row][col] === APPLE) {62
totalWays += dfs(slices, startRow, col + 1);66
// return the number of ways67
return (cache[key] = totalWays % 1000000007);