poj 1579

起先还以为照着题目写下递归就好了,可是运行到50 50 50时,怎么也出不了答案,寒,原来要用记忆化搜索,这样可以省去重复操作。
#include<stdio.h>
long num[21][21][21]={0};
long 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(num[a][b][c])	return num[a][b][c];
	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));
	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));
}
int main()
{
	int x,y,z;
	while(scanf("%d%d%d",&x,&y,&z)!=EOF)
	{
		if(x==-1 && y==-1 && z==-1)	   return 0;
		printf("w(%d, %d, %d) = %ld\n",x,y,z,w(x,y,z));
	}
	return 0;
}
原文地址:https://www.cnblogs.com/submarinex/p/1941280.html