[JSOI2007] Ancestral code Zuma

Source: Internet
Author: User

Title Description This is a popular game in Jsoi, named Zuma. The delicate backdrop, complemented by mysterious Inca music, seems to be in the midst of an ancient country, playing a mysterious game-the famous Zuma game. Zuma game protagonist is a stone frog, the stone frog will spit out all kinds of color beads, beads shape beautiful, and has a mysterious color, surrounded by the stone frog is carrying beads of the track, all kinds of color beads will move forward along the track, the stone frog must stop the beads rolled into the orbital end of the hole inside, how to reduce beads it? You have to rely on the stone Frog Spit beads and tracks on the combination of beads, the color of the same person can disappear score! Until the beads on the track are cleared. Maybe you don't know the Zuma game. Never mind. Here we introduce a simple version of the Zuma game rules. There are some glass beads in one channel, each with its own color, as shown in 1. What the player can do is choose a color of the beads (note: The color can be selected, which is different from the real game) into a certain location.    Figure 1 Figure 2, the player selects a blue bead, into the location of the diagram, and then get a picture of the situation of 3.   FIG. 2   Figure 3 When the player enters a bead, the beads will disappear if they are made up of more than three consecutive beads of the same color as the beads. For example, a white bead is injected into the position in Figure 4, resulting in three white beads of the same color. These three beads will disappear, and then get the situation in Figure 5.    Figure 4  Figure 5 It is important to note that the three consecutive yellow beads in Figure 4 do not disappear because there is no beads in them. The disappearance of beads will also have a ripple effect. When a string of beads of the same color disappears, if the beads around the vanishing position are the same color, and the length is greater than 2, you can continue to disappear. For example, in Figure 6, a red bead was injected, resulting in three consecutive red beads. When the red beads disappear, it is white beads around, and a total of four, so the white beads have disappeared. After that, the vanishing position of the left and right are blue beads, a total of three, so the blue beads also disappeared. Finally get the status of Figure 7. Note that the three yellow beads in Figure 7 do not disappear, because the blue beads disappear on one side of the purple beads, the other side is yellow beads, the color is different.    Figure 6   Figure 7 In addition to the above, there is no other way to eliminate the beads. Now, we have a row of beads that need you to eliminate. For each round, you are free to choose the beads of different colors and shoot them in any position. Your task is to shoot the fewest beads and eliminate all the beads. Enter the first line an integer n (n≤500), which represents the number of beads the second row n integers (32-bit integers in the range), separated by a space, each integer represents a color of the beads。 Outputs an integer that indicates the minimum number of beads that need to be shot. Sample input9 1 1 2 2 3 3 2 1 1Sample output1 interval DP F[i][j] represents the smallest solution to eliminate i~j Two types of cases: 1. Eliminate I~k,k+1~j. 2. Eliminate I+1~j-1 (i is the same color as J) The first point, the number of points called.
1#include <iostream>2#include <algorithm>3#include <cstring>4#include <cstdio>5 using namespacestd;6 intn,tot,s[1001],color[1001],f[1001][1001];7 intMain ()8{intlast,i,j,k,x;9 //freopen ("File.In", "R", stdin);Tenscanf"%d",&n); Onelast=-1; tot=0; A      for(i=1; i<=n;i++) -     { -scanf"%d",&x); the         if(x==Last ) -         { -s[tot]++; -         } +         Else -         { +tot++; As[tot]=1; atcolor[tot]=x; -last=x; -         } -     } -n=tot; -Memset (F,127/3,sizeof(f)); in       for(i=1; i<=n;i++) -      if(s[i]>=2) tof[i][i]=1; +      Elsef[i][i]=2; -      for(i=1; i<=n-1; i++) the     { *          for(j=1; j<=n-i;j++) $          {Panax Notoginseng              for(k=j;k<=i+j-1; k++) -             { theF[j][j+i]=min (f[j][j+i],f[j][k]+f[k+1][j+i]); +             } A             if(color[j]==color[j+i]) the             { +F[j][j+i]=min (f[j][j+i],f[j+1][j+i-1]+ (s[j]+s[j+i]>=3?0:1)); -             } $              $          } -     } -cout<<f[1][n]; the}

[JSOI2007] Ancestral code Zuma

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.