Microsoft's Throwing Cup problem-dynamic planning

Source: Internet
Author: User
Microsoft's Throwing Cup problem-dynamic planningCategory: Algorithm 2011-12-22 10:05 2754 people reading reviews (3) Collection report Microsoft Layer Sina Weibo test interview

Directory (?) [+]

Saw a Microsoft face test on Sina Weibo


Title DescriptionA cup, if the nth layer is broken, then in any higher than N of the floor will break, if not broken in the first layer, then in any lower than m floor will not break, give you two such cups, let you in the 100-storey high floor test, ask for a minimum number of tests to find the cup that happens to break the glass floor.
Solution of inequalitiesAll discussions are based on the case of 2 cups: The red lines in all the illustrations below indicate the drop-off position when the two cups are left, and when the cup is broken, that is, when only one Cup is left, only the bottom of the known range is required to test the top layer of the known range. When there is only one layer, you need to try a drop. When there are two layers, you need to drop two times. When three layers are required, first of all to discuss whether it can be done by two times, you can find the method as shown. When four layers are required, the first thing to discuss is whether it can be done by two Tests, and you can see that it is not working, so you need at least three trial drops. When five levels are required, first discuss whether it is possible to complete the test break by less than or equal to three times, and if not, to discuss the situation of up to four trial drops. And so on, the following figure:

Therefore, when there are 100 layers, it takes up to N times to try to fall, making n+ (n-1) + (n-2) +...+1 >= 100, and then rounding on N, so n=14. The worst-case scenario requires 14 tries to drop. The method is: the first time in the 14-layer throw, if not broken, in 27 layers (14+13) continue to throw, not broken in the 39 layer (27+12) continue to throw, and so on. If it is broken, one Cup is left, and the top layer of the known range is tested and dropped from the bottom of the known range.Dynamic Programming SolutionSuppose to use dp[i][j] to represent the remaining layer I, J Cup when the minimum number of times to try to fall. (1) dp[i][1] = I, that is, when there is only one Cup left, you can only start from the first layer to try to fall up. (2) Dp[0][j] = 0, when only 0 layers are left, only 0 test drops are required. (3) Dp[1][j] = 1, when only 1 layers are left, only 1 test drops are required, if j>0 (4) dp[i][j] = min (1=<k<j) (Max (dp[k-1][i-1], dp[j-k][i]) + 1), That is, when the cup broken and not broken when divided into two sub-problems, respectively, and then to find the maximum number of sub-problem test and as a test of this decomposition method of the number of times, and then take these all the sub-problem of the method, the sub-problem and the smallest method. ROUTE[I][J], which represents the next floor when I'm left with a J-cup. When J=1, the bottom of the range from the lowest level in turn to the top of the test drop.[CPP] View plain copy//dp.c   #include  <stdio.h>   #define  LAYER 101    #define  CUP 3   Int main () {       int dp[layer ][cup], route[layer][cup], i, j, k;       for (i=1; i< layer; i++) {           dp[i][1] = i;             dp[i][0] = 0;        }       for (i=1; i<cup; i++) {             dp[1][i] = 1;            route[1][i] = 0;           dp[0][ i] = 0;       }        for (i=2;  i<cup; i++)            for (j=2; j<layer; j++) {                dp[j][i] = j;                route[j][i] = 0;                for (k=1; k<j;  k++) {                  &NBSP;&NBSP;INT&NBSP;MIN&NBSP;=&NBSP;DP[K-1][I-1]&NBSP;&GT;&NBSP;DP[J-K][I]?&NBSP;DP[K-1][I-1]+1:&NBSP;DP[J-K] [i]+1;                    route[j][i] = min <= dp[j][i]? k: route[j][i];                    dp[j][i] = min  < dp[j][i]? min: dp[j][i];                }           }       printf ("min step is %d\n",  dp[layer-1][cup-1]);       //for (i=1;  i<layer; i++) {for (j=2; j<cup; j++) {printf ("%d\t",  route[i][j]);}  printf ("\ n");}        return 0;  }  
The output is as follows: MIN STEP is 14. That is, there is a way to try to fall 14 times. The route array also prints out the method. For example, the result of the route array:

ROUTE[I][2]
1 0 2 1 3 2 4 3 5 3
6 3 7 4 8 4 9 4 10 4
11 5 12 5 13 5 14 5 15 5
16 6 17 6 18 6 19 6 20 6
21st 6 22 7 23 7 24 7 25 7
26 7 27 7 28 7 29 8 30 8
31 8 32 8 33 8 34 8 35 8
36 8 37 9 38 9 39 9 40 9
41 9 42 9 43 9 44 9 45 9
46 10 47 10 48 10 49 10 50 10
51 10 52 10 53 10 54 10 55 10
56 11 57 11 58 11 59 11 60 11
61 11 62 11 63 11 64 11 65 11
66 11 67 12 68 12 69 12 70 12
71 12 72 12 73 12 74 12 75 12
76 12 77 12 78 12 79 13 80 13
81 13 82 13 83 13 84 13 85 13
86 13 87 13 88 13 89 13 90 13
91 13 92 14 93 14 94 14 95 14
96 14 97 14 98 14 99 14 100 14

Therefore, ROUTE[100][2]=14, the first Test fall is on the 14 floor, if broken, then from the first layer to the 14 floor to try to fall, if not broken, the second Test fall in the route[100-14][2]=route[86][2]=13, that is, in 14+13= 27 Layer for the second Test fall, if this break, then from the first 14+1=15 layer up to try to fall, if this is not broken, the third time to try to fall in the route[86-13][2]=route[73][2]=12, that is, in the 27+12=39 layer began to try to fall, and so on.

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.