1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| class Solution: def __init__(self): self.counter = {} def cut(self,pizza,i,j,k): if (i,j,k) in self.counter: return self.counter[(i,j,k)] if k==1: for r in range(i,len(pizza)): for c in range(j,len(pizza[0])): if pizza[r][c] == 'A': self.res += 1 return 1 return 0 if len(pizza)-i-1 + len(pizza[0])-j-1 < k-1: return 0 num = 0 flag = False for m in range(i,len(pizza)-1): if flag: num += self.cut(pizza,m+1,j,k-1) else: for c in range(j,len(pizza[0])): if pizza[m][c] == 'A': flag = True break if flag: num += self.cut(pizza,m+1,j,k-1) flag = False for m in range(j,len(pizza[0])-1): if flag: num += self.cut(pizza,i,m+1,k-1) else: for r in range(i,len(pizza)): if pizza[r][m] == 'A': flag = True break if flag: num += self.cut(pizza,i,m+1,k-1) self.counter[(i,j,k)] = num return num
def ways(self, pizza: List[str], k: int) -> int: return (self.cut(pizza,0,0,k) % int(1e9 + 7)) `
|