State compression DP----HDU2809

Source: Internet
Author: User

Status compression DP is a good entry-level question. The second attempt to solve this kind of problem is difficult, so I recorded it.

The question refers to Lu buzhan qunxiong. First, we will give you six numbers, namely Lu Bu's attack value, defense value, and life value. After the upgrade, these three values will be incremental, then there is the opponent's number num, followed by the opponent's name, attack value, defense value, life value, and experience value. It indicates that when Lu Bu's experience value increases by 100, the attack value will be displayed, defense value, lifecycle value plus increment. Finally, if the life value is greater than 0, output the life value. Otherwise, output a sentence "poor lvbu, his period was gone .".

At the beginning, I saw that this question was actually compressed DP, and thought it was a common DP. However, when I think about it, there are too many statuses and there is a correlation with the numbers. The reason is: assume there are three people playing against Lu Bu: Guan Yu, Zhang Fei, Xu Yu, now the problem arises. In what order does Lu Bu fight against the three men? Lu Bu --> Guan Yu --> Zhang Fei --> XU Yu, Lu Bu --> Zhang Fei --> Guan Yu --> XU Yu, etc, and whether the life value is zero after one person is defeated. What about the two .......? In this way, we can find that it is messy, in many States, and it is difficult to express it (this is not binary. 1 is good at State compression ). Therefore, the State expression lvbu [I] is defined. In the Binary Expression corresponding to I, 1 indicates that the user has already played against the user. For example, 5 = 101 indicates that the user has played against Guan Yu and Xu xiaofight respectively. But the trouble is coming again. 101 is the first fight with Guan Yu, or the first fight with XU Yu. Here we should note that the order of the first fight may lead to different results, for example, if I try again to get less experience and cannot upgrade, then the experience of upgrading is enough. Obviously, the two results will not have the same life value. How can this problem be solved? This only requires updates during the for loop.

Next we will explain the code in detail,

1 # include <iostream> 2 # include <cstdio> 3 4 using namespace STD; 5 6 struct person {7 int Attck; 8 int defense; 9 int HP; 10 int expirence; 11} lvbu [1 <21], enemy [21], update; // define the struct and allocate the corresponding array. The opponent can have up to 20 12 INT results, num; 13 14 inline int max (int A, int B) // maximum function 15 {16 if (A> B) return a; 17 return B; 18} 19 20 void fight () 21 {22 int Attck, defense; 23 int mytime, time; 24 int RHP, expirence, rattack, rdef; 25 for (in T I = 0; I <= result; I ++) // status 26 for (Int J = 0; j <num; j ++) // opponent number from 0 to num-127 {28 If (! (I & (1 <j) & lvbu [I]. HP> 0) // If Lu Bu does not fight against the number J opponent and the life value is not zero, it is necessary to fight against 29 {30 Attck = max (1, lvbu [I]. attck-enemy [J]. defense); // subject to the question, lub attacks the other party once and drops the Attck Point Blood 31 defense = max (1, enemy [J]. attck-lvbu [I]. defense); // the other party launches an attack, lubu drops the defense Point Blood, 32 mytime = (lvbu [I]. HP + defence-1)/defense; // total number of times lub can attack 33 time = (enemy [J]. HP + attck-1)/Attck; // total number of attacks the opponent can attack 34 // If Guan Yu's life value is 8, Lu Bu attacks cause one damage to-3, then, lub can attack three times in total. How can it be expressed with a line of code? You can obtain 1 unit of blood first, and divide the rest directly by the damage value 35 if (mytime <time) continue; // Lu Bu hangs up first, otherwise, continue executing 36 RHP = (lvbu [I]. HP-(time-1) * defense); // The original residual life value minus the value of generation. The reason for the time reduction is that the opponent first fell down. You beat me, I hit you into an even number, and you never hit me again. 37 expirence = enemy [J]. expirence + lvbu [I]. expirence; // obtain the experience value 38 rattack = lvbu [I]. attck; rdef = lvbu [I]. defense; // The reason for this is to facilitate the subsequent writing of 39 40 if (expirence> = 100) // you can upgrade 41 {42 expirence-= 100; // pay 100 points of experience. The price is 43 rattack + = Update. attck; 44 rdef + = Update. defense; 45 RHP + = Update. HP; 46} 47 48 if (RHP> = lvbu [I | (1 <j)]. HP) // This is the update of 49 {50 lvbu [I | (1 <j)]. attck = rattack; 51 lvbu [I | (1 <j)]. defense = rdef; 52 lvbu [I | (1 <j)]. HP = RHP; 53 lvbu [I | (1 <j)]. expirence = expirence; 54} 55} 56} 57 if (lvbu [Result]. HP) printf ("% d \ n", lvbu [Result]. HP); 58 else printf ("poor lvbu, his period was gone. \ n "); 59} 60 int main () 61 {62 while (scanf (" % d ", & lvbu [0]. attck, & lvbu [0]. Defense, & lvbu [0]. HP, & Update. Attck, & Update. Defense, & Update. HP )! = EOF) 63 {// enter 64 lvbu [0]. expirence = 0; // lub's initial experience is 065 char name [21]; 66 scanf ("% d", & num); 67 for (INT I = 0; I <num; I ++) 68 scanf ("% S % d", name, & enemy [I]. attck, & enemy [I]. defense, & enemy [I]. HP, & enemy [I]. expirence); 69 result = (1 <num)-1; // Total number of States 70 71 for (INT I = 1; I <= result; I ++) lvbu [I]. HP = 0; // initialize 72 fight (); // battle 73} 74 return 0; 75}

Looking at the review will save you a lot of trouble.

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.