poj1579 Function Run Fun
我是不是可以把这题理解为直接给出状态转移方程的DP问题
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<algorithm> using namespace std; int d[100][100][100]; bool check(int a,int b,int c) { if(a<=0||b<=0||c<=0) { return true; } return false; } void dp(int a,int b,int c) { int temp1,temp2,temp3,temp4; if(a<=0||b<=0||c<=0) { d[a][b][c]=1; return; } if(a>20||b>20||c>20) { if(d[20][20][20]==-1) { dp(20,20,20); } d[a][b][c]=d[20][20][20]; return; } if(a<b&&b<c) { if(!check(a,b,c-1)) { if(d[a][b][c-1]==-1) { dp(a,b,c-1); } temp1=d[a][b][c-1]; } else { temp1=1; } if(!check(a,b-1,c-1)) { if(d[a][b-1][c-1]==-1) { dp(a,b-1,c-1); } temp2=d[a][b-1][c-1]; } else { temp2=1; } if(!check(a,b-1,c)) { if(d[a][b-1][c]==-1) { dp(a,b-1,c); } temp3=d[a][b-1][c]; } else { temp3=1; } d[a][b][c]=temp1+temp2-temp3; return; } if(!check(a-1,b,c)) { if(d[a-1][b][c]==-1) { dp(a-1,b,c); } temp1=d[a-1][b][c]; } else { temp1=1; } if(!check(a-1,b-1,c)) { if(d[a-1][b-1][c]==-1) { dp(a-1,b-1,c); } temp2=d[a-1][b-1][c]; } else { temp2=1; } if(!check(a-1,b,c-1)) { if(d[a-1][b][c-1]==-1) { dp(a-1,b,c-1); } temp3=d[a-1][b][c-1]; } else { temp3=1; } if(!check(a-1,b-1,c-1)) { if(d[a-1][b-1][c-1]==-1) { dp(a-1,b-1,c-1); } temp4=d[a-1][b-1][c-1]; } else { temp4=1; } d[a][b][c]=temp1+temp2+temp3-temp4; } int main() { memset(d,-1,sizeof(d)); int a,b,c; while(scanf("%d %d %d",&a,&b,&c),a!=-1||b!=-1||c!=-1) { if(a<=0||b<=0||c<=0) { printf("w(%d, %d, %d) = 1\n",a,b,c,d[a][b][c]); continue; } if(d[a][b][c]==-1) { dp(a,b,c); } printf("w(%d, %d, %d) = %d\n",a,b,c,d[a][b][c]); } return 0; }
//4032K 0MS