poj1579

记忆化搜索

View Code
#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <cstring>

using namespace std;



#define maxn 25



int a, b, c;

int f[maxn][maxn][maxn];



int 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 (f[a][b][c] != -1)

        return f[a][b][c];

    if (a < b && b < c)

        return f[a][b][c] = w(a, b, c - 1) + w(a, b - 1, c - 1)- w(a, b - 1, c);

    return f[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()

{

    memset(f, -1, sizeof(f));

    //freopen("t.txt", "r", stdin);

    while (scanf("%d%d%d", &a, &b, &c), ~a | ~b | ~c)

        printf("w(%d, %d, %d) = %d\n", a, b, c,  w(a, b, c));    

    return 0;

}

 

你可能感兴趣的:(poj)