Algorithm competition entry classic 6.3.1 Binary Tree-ball fall, java Ball fall

Source: Internet
Author: User

Algorithm competition entry classic 6.3.1 Binary Tree-ball fall, java Ball fall

There is a binary tree with the maximum depth of D and the depth of all leaves is the same. Numbers of all nodes from top to bottom from left to right are 1, 2, 3 ,..., 2eD-1. Place a ball at Node 1 and it will fall. Each node has a switch, which is initially closed. When a ball falls onto a switch, its status changes. When the ball reaches an internal node, if the switch is off, it goes up. Otherwise, it goes down until it reaches the leaf node, as shown in.
Some balls fall from node 1 in sequence. Where will the last ball fall? Enter the leaf depth D and the number of small balls I, and output the leaf number at the end of the I small ball. Assume that I cannot exceed the number of leaves of the entire tree; D <= 20. The output can contain up to 1000 groups of data. 1 # include <stdio. h> 2 # include <string. h> 3 # define MAXN 20 4 int s [1 <MAXN]; // shift 1 to 20 places left, that is, the maximum number of nodes is 2eMAXN-1 5 int main (void) 6 {7 int D, I; 8 while (scanf ("% d", & D, & I) = 2) 9 {10 memset (s, 0, sizeof (s); // switch (0 is disabled by default). The memset function contains the header file string. h11 int k, n = (1 <D)-1; // n is the maximum node number 12 for (int I = 0; I <I; I ++) // keep n small balls falling 13 {14 k = 1; 15 for (;) 16 {17 s [k] =! S [k]; 18 k = s [k]? K * 2: k * 2 + 1; // select the fall direction 19 Based on the switch status if (k> n) break; // It is already out of bounds, dropped times: D20} 21} 22 printf ("% d \ n", k/2); // leaf No. 23} 24 return 0; 25}View Code

Analysis:
1. For a node k, the numbers of its left son and right son are 2 k and 2 k + 1 respectively.
2. Although each ball is strictly falling D-1 times, but the above code using the "if (k> n) break" Method to Determine "out of the world" is more general.
3. this program is too computation: Since I can be as high as 2 (D-1), the total number of dropped layers of each test data may be as high as 219*19 (I .e. I * 19) = 9961472, A total of 1000 groups of data may exist.
Method 2:

1 # include <stdio. h> 2 # include <string. h> 3 # define MAXN 20 4 int s [1 <MAXN]; // shift 1 to 20 places left, that is, the maximum number of nodes is 2eMAXN-1 5 int main (void) 6 {7 int D, I; // defines the number of balls, that is, the last ball number is I 8 // directly simulate the route of the last ball 9 while (scanf ("% d", & D, & I) = 2) 10 {11 memset (s, 0, sizeof (s); // The switch (0 is disabled by default). The memset function contains the header file string. h12 int k; // n is the maximum node number 13 for (int I = 0; I <I; I ++) // keep n balls falling 14 {15 k = 1; 16 for (int I = 0; I <D-1; I ++) // continuous D-1 falling 17 if (I % 2) {k = k * 2; I = (I + 1)/2 ;}// when I is an odd number, it is the first (I + 1)/second ball to the left 18 else {k = k * 2 + 1; I/= 2 ;}// when I is an even number, it is the first I/2 ball to the right 19} 20 printf ("% d \ n", k ); // output the leaf number of the last ball I 21} 22 return 0; 23}View Code

Analysis:
1. (1) because each ball falls on the root node, the first two balls must be one on the left and the other on the right. In general, you only need to check the parity of the ball number, you will be able to know which subtree it is ultimately in.
(2) For Small balls that fall into the left/right subtree of the root node, you only need to know the first few of the balls fall into the left/right subtree of the root node, you can see whether it is going to the left or the right.
(3) so on until the ball falls on the leaves.
2. The program not only has nothing to do with the ball number, but also saves a huge array s.
3. Use tips I % 2 to judge and avoid discussing I parity.

Related Article

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.