标签:using span ons return nbsp ret auth one alt
考察:记忆化搜索
思路:
可用递推和记忆化搜索两种方式.
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <cstdio> 5 using namespace std; 6 const int N = 21; 7 int f[N][N][N]; 8 int dfs(int a,int b,int c) 9 { 10 if(a<=0||b<=0||c<=0) return 1; 11 else if(a>20||b>20||c>20) return dfs(20,20,20); 12 if(f[a][b][c]!=-1) return f[a][b][c]; 13 if(a<b&&b<c) 14 return f[a][b][c] = dfs(a, b, c - 1) + dfs(a, b - 1, c - 1) - dfs(a, b - 1, c); 15 else 16 return f[a][b][c] = dfs(a - 1, b, c) + dfs(a - 1, b - 1, c) + dfs(a - 1, b, c - 1) - dfs(a - 1, b - 1, c - 1); 17 } 18 int main() 19 { 20 int a,b,c; 21 memset(f,-1,sizeof f); 22 while(scanf("%d%d%d",&a,&b,&c)!=EOF&&(a!=-1||b!=-1||c!=-1)) 23 printf("w(%d, %d, %d) = %d\n",a,b,c,dfs(a,b,c)); 24 return 0; 25 }
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <cstdio> 5 using namespace std; 6 const int N = 21; 7 int f[N][N][N]; 8 int main() 9 { 10 int a,b,c; 11 for(int i=0;i<=20;i++) 12 for(int j=0;j<=20;j++) 13 for(int k=0;k<=20;k++) 14 f[i][j][k] = 1; 15 for(int i=1;i<=20;i++) 16 for(int j=1;j<=20;j++) 17 for(int k=1;k<=20;k++) 18 { 19 if(i<j&&j<k) f[i][j][k] = f[i][j][k-1]+f[i][j-1][k-1]-f[i][j-1][k]; 20 else f[i][j][k] = f[i-1][j][k]+f[i-1][j-1][k]+f[i-1][j][k-1]-f[i-1][j-1][k-1]; 21 } 22 while(scanf("%d%d%d",&a,&b,&c)!=EOF&&(a!=-1||b!=-1||c!=-1)) 23 { 24 int ta = a,tb = b,tc = c; 25 if(a<0||b<0||c<0) a = 0,b = 0,c = 0; 26 else if(a>20||b>20||c>20) a= 20,b = 20,c =20; 27 printf("w(%d, %d, %d) = %d\n",ta,tb,tc,f[a][b][c]); 28 } 29 return 0; 30 }
几个月前做过这道题,当时不会写来着,那时真的连算法门都没入.
P1464 Function 双倍经验
标签:using span ons return nbsp ret auth one alt
原文地址:https://www.cnblogs.com/newblg/p/14409527.html