关闭

POJ 1579

标签: poj
161人阅读 评论(0) 收藏 举报
分类:

水题!

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

题意:
题意什么的,相信你能看懂,已经把函数都写好了,只是要注意的是为了避免重复的调用 ,所以采用记忆化搜索*


import java.util.Scanner;

public class Main{
    private static int x,y,z;
    private static int[][][] dp = new int[100][100][100];
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    while (scanner.hasNext()) {
        x = scanner.nextInt();
        y = scanner.nextInt();
        z = scanner.nextInt();
        if (x==-1&&y==-1&&z==-1) 
        break;
        int current = dfs(x, y, z);
        System.out.printf("w(%d, %d, %d) = %d\n",x,y,z,current);
    }
    scanner.close();
    }
    private static 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]!=0)
        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);

    return 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);
    }
}
0
0
查看评论

POJ-1579

//给了你动态转移方程,直接敲就是了。不过,要用开一个数组DP【】,储存每步累加值,节约时间。// AC代码: #include #include #define max 50 int dp[max][max][max]; int w(int a,int b,int c) { if(...
  • zyx520ytt
  • zyx520ytt
  • 2015-07-14 08:35
  • 321

POJ1579简单dp

Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19108   Accepted: 9666 ...
  • qq_36258516
  • qq_36258516
  • 2017-05-13 23:34
  • 134

POJ1579:Function Run Fun(记忆化)

Description We all love recursion! Don't we?  Consider a three-parameter recursive function w(a, b, c):  if a 1  if a...
  • libin56842
  • libin56842
  • 2014-07-28 17:18
  • 2662

poj 1579

刚开始看到这道题递归,一直在想可以不以剪枝 但是发现没用 然后
  • u010840444
  • u010840444
  • 2014-04-13 16:43
  • 403

poj 1579

起先还以为照着题目写下递归就好了,可是运行到50 50 50时,怎么也出不了答案,寒,原来要用记忆化搜索,这样可以省去重复操作。#includelong num[21][21][21]={0};long w(int a,int b,int c){ if(a0 || b0 || c0) return...
  • designer_
  • designer_
  • 2010-03-24 20:39
  • 321

POJ 1579

递归变动态规划,这个就直接发代码了。 #include using namespace std; int getw(int a,int b, int c, int*** arr) { if(a 0 || b 0 || c 0 ) return 1; else ret...
  • lxy54101
  • lxy54101
  • 2014-11-15 18:27
  • 243

POJ 1579 解题报告

简单题 thestoryofsnow 1579 Accepted 184K 0MS C++ 1247B /* ID: thestor1 LANG: C++ TASK: poj1579 */ #include #include #include ...
  • thestoryofsnow
  • thestoryofsnow
  • 2015-05-16 05:11
  • 270

poj 1579 递归-记忆化搜索

直接递归会重复计算一些值,如题所述会花费很长时间。最好的方法就是用记忆化搜索,用数组将值记录下来,当搜到已经计算过的值时直接使用就行了,避免再一次递归计算,这样会节省很多时间。 代码如下:http://blog.csdn.net/non_cease/article/details/6817914 #...
  • u011567017
  • u011567017
  • 2016-04-18 11:13
  • 207

POJ 1579(记忆化搜索)

Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17625   Accepted: 9021 ...
  • qq_24489717
  • qq_24489717
  • 2016-03-03 13:47
  • 346

poj 1579(记忆化搜索)

思路:用数组w[a][b][c]保存,减少重复搜索#include #include #define MAX 25 int x[MAX][MAX][MAX]; int w(int a,int b,int c){ if(a<=0||b<=0||c<=0)return 1; ...
  • dpy95
  • dpy95
  • 2015-11-02 00:41
  • 154
    个人资料
    • 访问:112171次
    • 积分:1387
    • 等级:
    • 排名:千里之外
    • 原创:41篇
    • 转载:3篇
    • 译文:0篇
    • 评论:32条
    博客专栏
    最新评论