(algorithm) throw a pawn

Source: Internet
Author: User

Topic:

1, there is a 100-storey building, your hands have two of the same glass Weiqi. From one layer of this building and the higher layer, throw the Weiqi will be broken, with your hands of the two glass go, find an optimal strategy (throw the fewest number of times), to know the critical plane.

2, if the height of the building is N-level, you have a k pieces, I would like to throw at least a few times you can know the critical layer?

Ideas:

1. Derivation

Here does not tear down, directly to the conclusion, concrete derivation reference: http://blog.csdn.net/jiaomeng/article/details/1435226

First select layer x to throw the first piece Q1:

If the pieces are broken, then the Q2 from the bottom to the top of the 1~x-1 through another piece to test which layer is the critical level;

If the pieces are not broken, choose to continue to throw the pieces Q1 in the x+ (x-1) =2x-1 layer:

If the pieces are broken, then the Q2 from the bottom to the top of the x+1~2x through another piece to test which layer is the critical level;

If the pieces are not broken, then choose the x+ (x-1) + (x-2) =3x-3 continue to throw pieces Q1;

。。。。。。

If the pawn is not broken, then the pawn Q1 will be thrown to a layer greater than or equal to 100 stops: x+ (x-1) + (x-2) + (x-3) +...+2+1>=100, that is, x* (x+1)/2 of the minimum value, so that it satisfies the >=100, the solution gets x=14.

That

First throw from Layer 14 (break the test 1-13, need 1+13-1+1=14 times)
Then throw it from layer 27 (break the test 15-26, need 2+26-15+1=14 times)
Then throw it from Layer 39 (break the test 28-38, need 3+38-28+1=14 times)
Then throw it from layer 50 (break the test 40-49, need 4+49-40+1=14 times)
Then throw it from layer 60 (break the test 51-59, need 5+59-51+1=14 times)
Then throw it from Layer 69 (break the test 61-68, need 6+68-61+1=14 times)
Then throw it from layer 77 (break the test 70-76, need 7+76-70+1=14 times)
Then throw it from Layer 84 (break the test 78-83, need 8+83-78+1=14 times)
Then throw it from layer 90 (break the test 85-89, need 9+89-85+1=14 times)
Then throw it from Layer 95 (break the test 91-94, need 10+94-91+1=14 times)
Then throw it from Layer 99 (break the test 96-98, need 11+98-96+1=14 times)
Finally, from the 100-storey throw (according to the test instructions will be broken, you can not throw, need 11 times)

Throw 14 times in the worst case.

2. Dynamic planning

Suppose Dp[i][j] represents a layer I, J pieces when the critical layer is obtained, the minimum number of times required.

Initial state:

When I=1, that is, only 1 layers, dp[1][j]=1; (j>0)

When J=1, only 1 pieces, dp[i][1]=i; (i>0)

State transition equation:

Dp[i][j]=min (Max (dp[k-1][j-1],dp[i-k][j]) +1) (0<k<i)

Explanation: When choosing to throw a piece at level K, there are two cases, consider the worst case, and therefore choose the larger one.

Pieces broken, then the number of pieces minus 1, and in the 1~k-1 layer tempted, at this time the number of dp[k-1][j-1]+1;

Pieces are not broken, then the number of pieces unchanged, in the k+1~j layer of temptation, at this time the number of dp[j-k][j]+1;

Code:

ROUTE[I][J] means that when there is an I-layer j pieces of the next choice which layer to throw pieces, through the route can be recorded every time the floor of the pawn.

Reference: Http://blog.csdn.net/taylor_tao/article/details/7084467?reload

#include <iostream>#include<vector>#include<stdlib.h>using namespacestd;#defineLAYERS 101#defineCUPS 3intmain () {vector<vector<int> > DP (layers,vector<int>(CUPS)); Vector<vector<int> > Route (layers,vector<int>(CUPS));  for(intI=1; i<layers;i++){//dp[i][0]=0;dp[i][1]=i; }     for(intI=1; i<cups;i++){//dp[0][i]=0;dp[1][i]=1; route[1][i]=0; }     for(intj=2; j<cups;j++){         for(intI=2; i<layers;i++) {Dp[i][j]=i; ROUTE[I][J]=0;  for(intk=1; k<i;k++){                intMini=max (dp[k-1][j-1],DP[I-K][J]) +1; ROUTE[I][J]=MINI&LT;=DP[I][J]?K:route[i][j]; DP[I][J]=MINI&LT;DP[I][J]?Mini:dp[i][j]; } }} cout<< dp[layers-1][cups-1] <<Endl; return 0;}

(algorithm) throw a pawn

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.