水题,把迭代转化成递推就OK了。
#include <stdio.h>
#define N 21
int w[N][N][N];
int main()
{
int a, b, c;
w[0][0][0] = 1;
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
for(int k = 0; k < N; k++)
{
if(i < j && j < k)
{
if(i < 1 || j < 1 || k < 2) a = 1;
else a = w[i][j][k - 1];
if(i < 1 || j < 2 || k < 2) b = 1;
else b = w[i][j - 1][k - 1];
if(i < 1 || j < 2 || k < 1) c = 1;
else c = w[i][j - 1][k];
w[i][j][k] = a + b - c;
}
else
{
int a, b, c, d;
if(i < 2 || j < 1 || k < 1) a = 1;
else a = w[i - 1][j][k];
if(i < 2 || j < 2 || k < 1) b = 1;
else b = w[i - 1][j - 1][k];
if(i < 2 || j < 1 || k < 2) c = 1;
else c = w[i - 1][j][k - 1];
if(i < 2 || j < 2 || k < 2) d = 1;
else d = w[i - 1][j - 1][k - 1];
w[i][j][k] = a + b + c - d;
}
}
while(scanf("%d %d %d", &a, &b, &c), a != -1 || b != -1 || c != -1)
{
if(a < 1 || b < 1 || c < 1)
printf("w(%d, %d, %d) = 1\n", a, b, c);
else if(a > 20 ||b > 20 || c > 20)
printf("w(%d, %d, %d) = %d\n", a, b, c, w[20][20][20]);
else printf("w(%d, %d, %d) = %d\n", a, b, c, w[a][b][c]);
}
return 0;
}