"Dynamic Programming" domino bone

Source: Internet
Author: User

Your constancy, why shift up just before dawn sleep, the most useless, MO a day exposure 10th cold. "Dynamic planning" domino bone time limit: 1 Sec memory limit: MB

The title describes a domino with up to 2 blocks, each with a 1~6 point. The existing rows of n dominoes are shown.
The sum of the points in the upper block is recorded as Σ1, and the sum of the points in the lower block is recorded as Σ2, and their difference is |σ1-σ2|. For example, σ1=6+1+1+1=9,σ2=1+5+3+2=11,|σ1-σ2| = 2. Each domino can rotate 180 °, making the upper and lower two blocks swap positions.

Programming with a minimum number of rotations makes it possible to minimize the difference between 2 line points on a domino.

For the example in Figure 8-1, if the last domino is rotated by 180 °, the difference between the top and bottom 2 line points will be 0.

The first line of input is a positive integer n (1≤n≤1000), which indicates the number of dominoes. The next n rows represent the points of N dominoes. Each line has two positive integers separated by a space, indicating the points A and B in the upper and lower squares of the dominoes, and 1≤a,b≤6. Outputs only one row, containing an integer. Represents the minimum number of rotations to be evaluated. Sample input
46 11 51) 31 2
Sample output
1

Analysis: The array should be opened more than once because there are negative values on the dominoes. DP[X] Represents the minimum number of exchanges for X
Dp[j+a]=min (Dp[j+a],dp[j]);
Dp[j+b]=min (dp[j+b],dp[j]+1);

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>using namespacestd;Const intMAXN =12010;intDP[MAXN], N;intMain () {inta,b,sum=0, I,j; CIN>>N;  for(intI=1; i<maxn;i++) Dp[i]=200000; dp[0]=0;  for(i=1; i<=n;i++) {cin>>a>>b; Sum+=a+b;  for(j=sum;j>=0; j--)        {            intt=Dp[j]; DP[J]=200000; Dp[j+a]=min (dp[j+a],t); Dp[j+b]=min (dp[j+b],t+1); }} sum=sum/2;  for(i=sum;i>=0; i--)        if(dp[i]<=N) Break;  for(j=sum;j<maxn;j++)        if(dp[j]<=N) Break; cout<<min (Dp[i],dp[j]) <<Endl; return 0;}
View Code

"Dynamic Programming" domino bone

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.