Solution1
#include<stdio.h>
#include<iostream>
#include<string.h>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int book[25][25][25];
int w(int a,int b,int c)
{
if(a<=0||b<=0||c<=0)
return 1;
else if(a>20||b>20||c>20)
return w(20,20,20);
else if(book[a][b][c]!=-1)
return book[a][b][c];
else if(a<b&&b<c)
return book[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
else
return book[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 book[a][b][c];
}
int main()
{
int a,b,c;
while(~scanf("%d %d %d",&a,&b,&c))
{
if(a==-1&&b==-1&&c==-1)
break;
memset(book,-1,sizeof(book));
printf("w(%d, %d, %d) = %d\n",a,b,c,w(a,b,c));
}
return 0;
}
Solution2
int w(int a,int b,int c)
{
if(a>=0&&a<=20&&b>=0&&b<=20&&c>=0&&c<=20&&book[a][b][c]!=-1)//防止越界
return book[a][b][c];
if(a<=0||b<=0||c<=0)
return 1;
else if(a>20||b>20||c>20)
return w(20,20,20);
else if(a<b&&b<c)
return book[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
else
return book[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);
}