POJ1579:Function Run Fun
Description
We all love recursion! Don't we?
Consider a three-parameter recursive function w(a, b, c):
if a <= 0 or b <= 0 or c <= 0, then w(a, b, c) returns:
1
if a > 20 or b > 20 or c > 20, then w(a, b, c) returns:
w(20, 20, 20)
if a < b and b < c, then w(a, b, c) returns:
w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)
otherwise it returns:
w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)
This is an easy function to implement. The problem is, if implemented directly, for moderate values of a, b and c (for example, a = 15, b = 15, c = 15), the program takes hours to run because of the massive recursion.
Consider a three-parameter recursive function w(a, b, c):
if a <= 0 or b <= 0 or c <= 0, then w(a, b, c) returns:
1
if a > 20 or b > 20 or c > 20, then w(a, b, c) returns:
w(20, 20, 20)
if a < b and b < c, then w(a, b, c) returns:
w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)
otherwise it returns:
w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)
This is an easy function to implement. The problem is, if implemented directly, for moderate values of a, b and c (for example, a = 15, b = 15, c = 15), the program takes hours to run because of the massive recursion.
Input
The input for your program will be a series of integer triples, one per line, until the end-of-file flag of -1 -1 -1. Using the above technique, you are to calculate w(a, b, c) efficiently and print the result.
Output
Print the value for w(a,b,c) for each triple.
Sample Input
1 1 1 2 2 2 10 4 6 50 50 50 -1 7 18 -1 -1 -1
Sample Output
w(1, 1, 1) = 2 w(2, 2, 2) = 4 w(10, 4, 6) = 523 w(50, 50, 50) = 1048576 w(-1, 7, 18) = 1
Source
方法一:
直接递归会超时,预处理即可。实现把所有结果存到三维数组中。
1 #include <iostream> 2 #include <cstdio> 3 4 using namespace std; 5 6 int main() 7 { 8 int w[21][21][21], ans, a, b, c; 9 //预处理 10 for(int i = 0; i < 21; i++) 11 for(int j = 0; j < 21; j++) 12 for(int k = 0; k < 21; k++) 13 { 14 if(!i || !j || !k) w[i][j][k] = 1; 15 else if(i < j && j < k) 16 w[i][j][k] = w[i][j][k-1] + w[i][j-1][k-1] - w[i][j-1][k]; 17 //w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c) 18 else 19 w[i][j][k] = w[i-1][j][k] + w[i-1][j-1][k] + w[i-1][j][k-1] - w[i-1][j-1][k-1]; 20 //w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1) 21 } 22 while(scanf("%d %d %d", &a, &b, &c)) 23 { 24 if(a == -1 && b == -1 && c == -1) break; 25 if(a <= 0 || b <= 0 || c <= 0) ans = 1; 26 else if(a > 20 || b > 20 || c > 20) ans = w[20][20][20]; 27 else ans = w[a][b][c]; 28 printf("w(%d, %d, %d) = %d\n", a, b, c, ans); 29 } 30 return 0; 31 }
方法二:
总之要先把之前运算出来的结果存到三维数组中,避免重复运算。
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 6 int dp[25][25][25]; 7 8 int dfs(int a,int b,int c) 9 { 10 if(a<=0 || b<=0 || c<=0) 11 return 1; 12 if(a>20 || b>20 || c>20) 13 return dfs(20,20,20); 14 if(dp[a][b][c]) 15 return dp[a][b][c]; 16 if(a<b && b<c) 17 dp[a][b][c] = dfs(a,b,c-1)+dfs(a,b-1,c-1)-dfs(a,b-1,c); 18 else 19 dp[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); 20 return dp[a][b][c]; 21 } 22 23 int main() 24 { 25 int a,b,c; 26 memset(dp,0,sizeof(dp)); 27 while(~scanf("%d%d%d",&a,&b,&c)) 28 { 29 if(a == -1 && b == -1 && c == -1) 30 break; 31 printf("w(%d, %d, %d) = %d\n",a,b,c,dfs(a,b,c)); 32 } 33 34 return 0; 35 }