动态规划练习 2

题目:Function Run Fun (POJ 1579)

链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=1579

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <memory.h>
 
using namespace std;
 
int main(int argc, char **argv)
{
    int value[21][21][21];
    int i, j, k;
 
    while (true)
    {
        cin >> i >> j >> k;
 
        if (i == -1 && j == -1 && k == -1)
        {
            break;
        }
 
        if (i <= 0 || j <= 0 || k <= 0)
        {
            printf("w(%d, %d, %d) = %d\n", i, j, k, 1);
            continue;
        }
 
        memset(value, 0, sizeof(value));
 
        value[0][0][0] = 1;
 
        for (int a = 0; a <= 20; ++a)
            for (int b = 0; b <= 20; ++b)
                for (int c = 0; c <= 20; ++c)
                {
                    if (a <= 0 || b <= 0 || c <= 0)
                    {
                        value[a][b][c] = 1;
                    }
                }
 
        for (int c = 1; c <= min(k, 20); ++c)
        {
            for (int b = 1; b <= min(j, 20); ++b)
            {
                for (int a = 1; a <= min(i, 20); ++a)
                {
                    if (a < b && b < c)
                    {
                        value[a][b][c] = 
                            value[a][b][c - 1] + 
                            value[a][b - 1][c - 1] - 
                            value[a][b - 1][c];
                    }
                    else
                    {
                        value[a][b][c] = 
                            value[a - 1][b][c] + 
                            value[a - 1][b - 1][c] + 
                            value[a - 1][b][c - 1] - 
                            value[a - 1][b - 1][c - 1];
                    }
                }
            }
        }
 
        printf("w(%d, %d, %d) = %d\n", i, j, k, value[min(i, 20)][min(j, 20)][min(k, 20)]);
    }
 
    return 0;
}
posted @ 2012-08-04 17:45  紫红的泪  阅读(275)  评论(1编辑  收藏  举报