Dynamic Plan for the HDU 2059 Tortoise and rabbit race

Source: Internet
Author: User

HDU 2059 Tortoise and rabbit race

Finally ~ Not easy.

The first method was wrong. Then I had to go online and read my thoughts. I vowed to turn off the webpage after seeing the four words of Dynamic Planning and then write it myself ..

The firstCode:

// Hdu2059 Tortoise and rabbit race // method error # include <stdio. h> int P [101] = {0}; int flag [101]; Main () {int I, j, L, N, C, T, VR, V1, v2; int X; // while (scanf ("% d", & L )! = EOF) {scanf ("% d", & N, & C, & T, & VR, & V1, & V2 ); for (I = 1; I <= N; I ++) scanf ("% d", & P [I]); P [0] = 0, P [I] = L; X = (T * V1 * V2)/(V1-V2); flag [0] = 0; For (j = I = 1; I <= N; I ++) {If (P [I]-flag [J-1] <= x) continue; if (P [I] + C> = P [I]-x) continue; flag [J ++] = P [I];} j --; if (flag [J] + x> = L) Flag [J] = L; else {J ++; flag [J] = L ;} printf ("x = % d \ n", x); for (I = 1; I <= J; I ++) printf ("% d ", flag [I]); printf ("\ n"); float TT = (J-1) * t; printf ("total fuel time % F \ n", TT ); for (I = 1; I <= J; I ++) {Tt + = x/V1; TT + = (flag [I]-flag [I-1]-x)/V2; printf ("% d ~ % D ~ % D ", flag [I-1], flag [I-1] + X, flag [I]); printf (" % F \ n ", (float) x/V1, (float) (flag [I]-flag [I-1]-x)/V2);} printf ("TT = % F \ n", TT ); if (TT <L/VR) printf ("what a pity rabbit! \ N "); elseprintf (" Good job, Rabbit! \ N ");}}

Then dynamic planning

# Include <stdio. h> # include <string. h> # include <stdlib. h> # define min (x, y) (x> Y )? Y: X) int main () {int L, N, C, T, VR, V1, V2, P [101]; int I, j; float time, Min, DP [101]; while (scanf ("% d", & L )! = EOF) {scanf ("% d", & N, & C, & T, & VR, & V1, & V2 ); for (I = 1; I <= N; I ++) scanf ("% d", & P [I]); P [0] = 0, P [I] = L; for (I = 1; I <= n + 1; I ++) DP [I] = 100000000.0; for (I = 1; I <= n + 1; I ++) {for (j = 0; j <= I-1; j ++) {If (P [I]-P [J] <= c) time = T + (P [I]-P [J])/V1; else time = T + C/V1 + (P [I]-P [J]-C)/V2; If (j = 0) Time-= T; min = DP [J] + min (time, (P [I]-P [J])/V2); // printf ("% d % D min = %. 2f \ n ", I, j, min); If (DP [I]> min) DP [I] = min;} printf (" DP [% d] = %. 2f \ n ", I, d P [I]);} // printf ("~ %. 2f %. 2f \ n ", DP [n + 1], L/VR * 1.0); If (DP [n + 1] <L/VR * 1.0) printf ("what a pity rabbit! \ N "); elseprintf (" Good job, Rabbit! \ N ") ;}return 0 ;}

This is still the error code.

 

Because the speed and distance should be floating-point variables written as integer,

 

# Include <stdio. h> # include <string. h> # include <stdlib. h> # define min (x, y) (x> Y )? Y: X) int main () {// int L, N, C, T, VR, V1, V2, P [101]; int I, j, N; float time, Min, DP [101], L, C, T, VR, V1, V2, P [101]; while (scanf ("% F", & L )! = EOF) {scanf ("% d % F", & N, & C, & T, & VR, & V1, & V2 ); for (I = 1; I <= N; I ++) scanf ("% F", & P [I]); P [0] = 0; P [I] = L; for (I = 1; I <= n + 1; I +++) DP [I] = 100000000.0; DP [0] = 0; for (I = 1; I <= n + 1; I ++) {for (j = 0; j <= I-1; j ++) {If (P [I]-P [J] <= c) time = T + (P [I]-P [J])/V1; else time = T + C/V1 + (P [I]-P [J]-C)/V2; // printf ("P [I] = %. 2f P [J] = %. 2f time = %. 2f \ n ", P [I], p [J], time); If (j = 0) Time-= T; min = DP [J] + min (time, (P [I]-P [J])/V2); // printf ("time = %. 2f DP [J] = % . 2f % d ~ % D min = %. 2f \ n ", time, DP [J], J, I, min); If (DP [I]> min) DP [I] = min ;} // printf ("DP [% d] = %. 2f \ n ", I, DP [I]);} // printf ("~ %. 2f %. 2f \ n ", DP [n + 1], L/VR * 1.0); If (DP [n + 1] <L/VR * 1.0) printf ("what a pity rabbit! \ N "); elseprintf (" Good job, Rabbit! \ N ") ;}return 0 ;}

AC ......

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.