POJ 1579 解法二用动态规划给递归剪枝,减少重复计算。此题一开始没想到用此法耗费了不少时间。

 1 #include<stdio.h>
2
3 int num[21][21][21] = {0};
4
5 int w(int a,int b,int c)
6 {
7 if (a<=0 || b<=0 || c<=0) return 1;
8 if(a>20 || b>20 || c>20) return w(20,20,20);
9 if(num[a][b][c]) return num[a][b][c];
10
11 if(a<b && b<c) return num[a][b][c] = w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
12
13 return num[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);
14 }
15
16 int main()
17 {
18 int x, y,z ;
19
20 while(scanf("%d%d%d",&x,&y,&z) != EOF)
21 {
22 if(x==-1 && y==-1 && z==-1)
23 break;
24 printf("w(%d, %d, %d) = %d\n",x, y, z, w(x,y,z));
25 }
26 return 0;
27 }
28
29

  

posted @ 2011-08-21 21:43  zhongya  阅读(257)  评论(0编辑  收藏  举报