Ultraviolet A, uvaacm

Source: Internet
Author: User

Ultraviolet A, uvaacm

Question: Ultraviolet A-10118 Free Candies (memory-based search)


Here are four piles of candy, each of which has a color. Each time, you can only take any pile of top candy and put it in your basket. If there are two sweets in the same color, they can be put into their own pockets. You can only hold up to five sweets in your basket. If the basket is full, the game will end. Ask the maximum number of sweets you can get.


Solution: After thinking about this question for a long time, it was hard to get the right status. As a result, my mind became hot and I deviated from the direction. Dp [a] [B] [c] [d]: Which of the four heaps of candy is at the top. If the following candy is determined, then no matter how you take it, the best one is definitely only one. Therefore, you can add the maximum number of remaining Sweets to the number of sweets you can obtain, that is, the maximum number of sweets required.

Dp [a] [B] [c] [d] = Max (dp [a + 1] [B] [c] [d] + 0 | 1, dp [a] [B + 1] [c] [d] + 0 | 1, dp [a] [B] [c + 1] [d] + 0 | 1, dp [a] [B] [c] [d + 1] + 0 | 1 ). 0 | 1 depends on whether the color of the candy in your basket is the same as the one I picked. The number of sweets in the corresponding basket needs to change. If the number is equal to 5, it indicates that it cannot be put. 0 is returned, and each heap of candy has a maximum quantity. The result is also to be completed. Note these boundary conditions. A new knowledge is found here: when using memcpy, if not all data in it is required, specify the length. Otherwise, an error may occur.


Code:

# Include <cstdio> # include <cstring> const int N = 42; const int M = 5; const int maxn = 1000005; int candy [N] [M]; int top [M]; // The top serial number of each heap of candy. int f [N] [N] [N] [N]; int n; int Max (const int, const int B) {return a> B? A: B;} void init () {memset (f,-1, sizeof (f); f [n] [n] [n] [n] = 0; // end status no matter the basket is full or not} bool handle (int r, int c, int k, int * B) {// check whether the same pond fruit int * B is the basket, and k + 1 is the number of sweets in the basket. Int I; for (I = 0; I <k; I ++) {if (candy [r] [c] = B [I]) break;} if (! K | I = k) {B [k] = candy [r] [c]; return false;} else {for (int j = I; j <k-1; j ++) B [j] = B [j + 1]; return true ;}} int dfs (int k, int * bket, int, int B, int c, int d) {int bket1 [M * 2]; int & ans = f [a] [B] [c] [d]; if (k> = M) // return 0 when the basket is full; // note that ans is not necessarily equal to 0 because the order of obtaining candy is different, the condition of this basket may be different if (ans! =-1) return ans; top [1] = a; top [2] = B; top [3] = c; top [4] = d; for (int I = 1; I <M; I ++) {/* for (int j = 0; j <k; j ++) bket1 [j] = bket [j]; */memcpy (bket1, bket, k * sizeof (int); // note/* for (int j = 0; j <k; j ++) printf ("% d", bket1 [j]); printf ("\ n"); */if (handle (top [I], i, k, bket1) {top [I] ++; if (top [I] <= n) ans = Max (ans, dfs (k-1, bket1, top [1], top [2], top [3], top [4]) + 1);} else {top [I] ++; if (top [I] <= n) ans = Max (ans, dfs (k + 1, bket1, top [1], top [2], top [3], top [4]);} top [I] --;} return ans;} int main () {while (scanf ("% d", & n), n) {for (int I = 0; I <n; I ++) for (int j = 1; j <M; j ++) scanf ("% d ", & candy [I] [j]); int B [M * 2]; init (); printf ("% d \ n", dfs (0, B, 0, 0, 0, 0); // printf ("% d \ n", f [n] [n-1] [0] [0]); /* for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) {for (int k = 0; k <n; k ++) {for (int l = 0; l <n; l ++) printf ("% d ", f [I] [j] [k] [l]); printf ("\ n");} printf ("\ n ");} printf ("\ n");} printf ("\ n"); */} return 0 ;}



What is the difference between ultraviolet B and ultraviolet?

Invisible light is collectively referred to as UV
UV can be further divided into ultraviolet B and UVC Based on the wavelength.
Among them, crawlers need two types: ultraviolet B and ultraviolet.
In addition to the function of heating, ultraviolet A has the effect of sterilization and Growth Promotion (but the effect is very small ).
Ultraviolet B is a rumor that it promotes growth, but it only helps calcium deposition.
I personally think that the two types of light are of great use, but they are available in the market ....... In vain.
Because many people have studied this, the overall quality of the lamp is better. But... There are also many so-called major brands (such as climbing) that do not have UV at all.

If no lamp is needed... Use food for supplements.

What is the difference between ultraviolet B and ultraviolet?

Invisible light is collectively referred to as UV
UV can be further divided into ultraviolet B and UVC Based on the wavelength.
Among them, crawlers need two types: ultraviolet B and ultraviolet.
In addition to the function of heating, ultraviolet A has the effect of sterilization and Growth Promotion (but the effect is very small ).
Ultraviolet B is a rumor that it promotes growth, but it only helps calcium deposition.
I personally think that the two types of light are of great use, but they are available in the market ....... In vain.
Because many people have studied this, the overall quality of the lamp is better. But... There are also many so-called major brands (such as climbing) that do not have UV at all.

If no lamp is needed... Use food for supplements.

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.