https://www.luogu.com.cn/problem/P1464
记忆化搜索就是记录之前程序走过的,
当再次走到的时候 直接取出来即可
这个题 由2可以确定下来要将20步以内的状态记录下来
#include <bits/stdc++.h> using namespace std; int r[22][22][22]; int w(int a,int b,int c){ if(a<=0||b<=0||c<=0) return 1; if(a>20||b>20||c>20) return w(20,20,20); if(r[a][b][c]) return r[a][b][c];//如果之前被计算过,直接返回 if(a<b&&b<c) r[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c); else r[a][b][c]=w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1); return r[a][b][c];//返回计算后的答案 } int main(){ int a,b,c; while(cin >> a >> b >> c){ if(a==-1&&b==-1&&c==-1) break; printf("w(%d, %d, %d) = ",a,b,c); printf("%d ",w(a,b,c)); } return 0; }