| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
这题简单的记忆化递归,不过得注意记忆数组越界情况。#include"iostream"
using namespace std;
int vi[21][21][21];//a,b,c可能远大于21,所以得处理好越界
int f(int a,int b,int c)
{
if(a<=0||b<=0||c<=0)
return 1;
if(a>20||b>20||c>20)
return vi[20][20][20]=f(20,20,20);
if(vi[a][b][c]!=0)
return vi[a][b][c];
if(a<b&&b<c)
return vi[a][b][c]=f(a,b,c-1)+f(a,b-1,c-1)-f(a,b-1,c);
return vi[a][b][c]=f(a-1,b,c)+f(a-1,b-1,c)+f(a-1,b,c-1)-f(a-1,b-1,c-1);
}
int main()
{
int a,b,c;
while(cin>>a>>b>>c&&(a!=-1||b!=-1||c!=-1))
{
memset(vi,0,sizeof(vi));
printf("w(%d, %d, %d) = %d\n",a,b,c,f(a,b,c));
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator