Hdu 1331 for Memory search and hdu1331 for Memory search

Source: Internet
Author: User

Hdu 1331 for Memory search and hdu1331 for Memory search
Function Run FunTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission (s): 2586 Accepted Submission (s): 1255

Problem DescriptionWe 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.
 
InputThe 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.
 
OutputPrint the value for w (a, B, c) for each triple.
Sample Input

1 1 12 2 210 4 650 50 50-1 7 18-1 -1 -1 
 
Sample Output
w(1, 1, 1) = 2w(2, 2, 2) = 4w(10, 4, 6) = 523w(50, 50, 50) = 1048576w(-1, 7, 18) = 1
 
SourcePacific Northwest 1999
Obviously, this question is simulated. Too many recursion times is a waste of time and cannot be passed. You can store the calculated value in the array by changing the space time, and then compute all possible calculated values. Output directly.
// Knowledge point: memory-based search uses array storage to reduce the number and time of recursive function calls. // It was made on the day of the examination. I did it again today and it turned out a little unfamiliar, B # include <stdio. h> int s [22] [22] [22]; void f () {int I, j, k; for (I = 1; I <22; ++ I) {for (j = 1; j <22; ++ j) {for (k = 1; k <22; ++ k) {if (I <j & j <k) {if (k-1 = 0) s [I] [j] [k-1] = s [I] [J-1] [k-1] = 1; if (J-1 = 0) s [I] [J-1] [k-1] = s [I] [J-1] [k] = 1; s [I] [j] [k] = s [I] [j] [k-1] + s [I] [J-1] [k-1]-s [I] [J-1] [k]; continue;} if (I = 1) s [I-1] [j] [k] = s [I-1] [J-1] [k] = s [I-1] [j] [k-1] = s [I-1] [J-1] k-1 = 1; if (j = 1) s [I-1] [J-1] [k] = s [I-1] [J-1] = 1; if (k = 1) s [I-1] [j] [k-1] = s [I-1] [J-1] [k-1] = 1; s [I] [j] [k] = s [I-1] [j] [k] + s [I-1] [J-1] [k] + s [I-1] [j] [k-1]-s [I-1] [J-1] [k-1] ;}}} int main () {int a, B, c; f (); while (~ Scanf ("% d", & a, & B, & c ),! (A =-1 & B =-1 & c =-1 )) {if (a <= 0 | B <= 0 | c <= 0) {printf ("w (% d, % d, % d) = 1 \ n ", a, B, c); continue;} if (a> 20 | B> 20 | c> 20) {printf ("w (% d, % d, % d) = % d \ n", a, B, c, s [20] [20] [20]); continue;} printf ("w (% d, % d, % d) = % d \ n", a, B, c, s [a] [B] [c]);} return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.