POJ1579:Function Run Fun(记忆化)

Description

We all love recursion! Don‘t we?

Consider a three-parameter recursive function w(a, b, c):

if a <= 0 or b <= 0 or c <= 0, then w(a, b, c) returns:

1

if a > 20 or b > 20 or c > 20, then w(a, b, c) returns:

w(20, 20, 20)

if a < b and b < c, then w(a, b, c) returns:

w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)

otherwise it returns:

w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)

This is an easy function to implement. The problem is, if implemented directly, for moderate values of a, b and c (for example, a = 15, b = 15, c = 15), the program takes hours to run because of the massive recursion.

Input

The input for your program will be a series of integer triples, one per line, until the end-of-file flag of -1 -1 -1. Using the above technique, you are to calculate w(a, b, c) efficiently and print the result.

Output

Print the value for w(a,b,c) for each triple.

Sample Input

1 1 1
2 2 2
10 4 6
50 50 50
-1 7 18
-1 -1 -1

Sample Output

w(1, 1, 1) = 2
w(2, 2, 2) = 4
w(10, 4, 6) = 523
w(50, 50, 50) = 1048576
w(-1, 7, 18) = 1

状态方程都给出来了,直接无脑敲代码就行了
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int dp[25][25][25];

int dfs(int a,int b,int c)
{
    if(a<=0 || b<=0 || c<=0)
        return 1;
    if(a>20 || b>20 || c>20)
        return dfs(20,20,20);
    if(dp[a][b][c])
        return dp[a][b][c];
    if(a<b && b<c)
        dp[a][b][c] = dfs(a,b,c-1)+dfs(a,b-1,c-1)-dfs(a,b-1,c);
    else
        dp[a][b][c] = dfs(a-1,b,c)+dfs(a-1,b-1,c)+dfs(a-1,b,c-1)-dfs(a-1,b-1,c-1);
    return dp[a][b][c];
}

int main()
{
    int a,b,c;
    memset(dp,0,sizeof(dp));
    while(~scanf("%d%d%d",&a,&b,&c))
    {
        if(a == -1 && b == -1 && c == -1)
            break;
        printf("w(%d, %d, %d) = %d\n",a,b,c,dfs(a,b,c));
    }

    return 0;
}


POJ1579:Function Run Fun(记忆化)

时间: 2024-10-09 17:56:07

POJ1579:Function Run Fun(记忆化)的相关文章

POJ 1579 Function Run Fun 记忆化搜索

Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a <= 0 or b <= 0 or c <= 0, then w(a, b, c) returns: 1 if a > 20 or b > 20 or c > 20, then w(a, b, c) returns: w(20, 20, 20) if a &

POJ 1579 Function Run Fun 记忆化递归

典型的记忆化递归问题. 这类问题的记忆主要是利用数组记忆.那么已经计算过的值就能够直接返回.不须要进一步递归了. 注意:下标越界.递归顺序不能错,及时推断是否已经计算过值了,不要多递归. 或者直接使用动态规划法填好表也是能够的. #include <stdio.h> #include <limits.h> const int MAX_N = 21; int W[MAX_N][MAX_N][MAX_N]; int getValue(int a, int b, int c) { if

POJ1579 Function Run Fun

Function Run Fun Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID: 157964-bit integer IO format: %lld      Java class name: Main We all love recursion! Don't we? Consider a three-parameter recursive function w(

HDU 1331 Function Run Fun(记忆化搜索)

Problem Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a <= 0 or b <= 0 or c <= 0, then w(a, b, c) returns:1 if a > 20 or b > 20 or c > 20, then w(a, b, c) returns:w(20, 20, 20)

hdu1579 Function Run Fun(深搜+记忆化)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1579 Problem Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a <= 0 or b <= 0 or c <

HDU 1331 Function Run Fun (基础记忆化搜索)

Function Run Fun Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2602    Accepted Submission(s): 1263 Problem Description We all love recursion! Don't we? Consider a three-parameter recursive f

POJ 1579 Function Run Fun 【记忆化搜索入门】

题目传送门:http://poj.org/problem?id=1579 Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20560   Accepted: 10325 Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a <=

uva10285 - Longest Run on a Snowboard(记忆化搜索)

题目:uva10285 - Longest Run on a Snowboard(记忆化搜索) 题目大意:给出N * N的矩阵,要求找到一条路径,路径上的值是递减的,求这样的路径的最长长度. 解题思路:记忆话搜索.因为要求最长的路径那么就需要将所有的这样的路径找出,但是直接dfs会超时的.对于同一个位置,从这个点出发的最长路径长度是固定的.所以在找的时候就要将这个位置的最长路径算出来并且保存下来.一开始还担心会走回头路就是一直兜圈子.但是后面发现能往右走就不可能在从右边的那个位置走回去,因为路径

uva10285 Longest Run on a Snowboard(dp之记忆化搜索 )

10285 Longest Run on a Snowboard Michael likes snowboarding. That's not very surprising, since snowboarding is really great. The bad thing is that in order to gain speed, the area must slide downwards. Another disadvantage is that when you've reached