https://leetcode-cn.com/problems/number-of-ways-of-cutting-a-pizza/
最后留一块也要符合要求、要求披萨全部用完,以在倒数第二块的时候判断return。
class Solution {
public:
int n,m;
long long count=0;
int K;
vector<string> pizza;
int record[55][55][15];
int mod=1e9+7;
//不包含x2,y2
bool check(int x1,int y1,int x2,int y2){
for(int i=x1;i<x2;i++){
for(int j=y1;j<y2;j++){
if(pizza[i][j]=='A')
return true;
}
}
return false;
}
void dfs(int x,int y,int k){
if(record[x][y][k]!=-1){
count+=record[x][y][k];
count%=mod;
return;
}
if(k==K-1){
if(check(x,y,n,m)){
count++;
record[x][y][k]=1;
}
else
record[x][y][k]=0;
return;
}
long long ori_count=count;
//行
for(int i=x+1;i<n;i++){
if(check(x,y,i,m))
dfs(i,y,k+1);
}
//列
for(int j=y+1;j<m;j++){
if(check(x,y,n,j))
dfs(x,j,k+1);
}
record[x][y][k]=(count-ori_count+mod)%mod;
}
int ways(vector<string>& pizza_, int K_) {
pizza=pizza_;
K=K_;
memset(record,-1,sizeof(record));
n=pizza.size();
m=pizza[0].size();
dfs(0,0,0);
return count;
}
};