Nanyang OJ-63 Small Monkey Whereabouts (data structure-two fork tree)

Source: Internet
Author: User

Topic 63
The whereabouts of the little monkey
Time limit: Ms | Memory Limit: 65535 KB
Difficulty: 3
Describe
There is a binary tree with a maximum depth of D, and all leaves have the same depth. All nodes are numbered 1,2,3,,2 from left to right from top to bottom minus 1 for D-Times. Put a little monkey at the junction 1 and it will run down. Each inner node has a switch, the initial all closed, when a small monkey ran to a switch, its state will change, when reaching an internal node, if the switch is closed, the small monkey go left, or go to the right, until the leaf node.

Some little monkeys began to run down from the Junction 1, where did the last little monkey go?

Input
Enter the depth of the binary tree leaves D, and the number of small monkeys I, assuming I do not exceed the number of leaves of the whole tree, d<=20. End With 0 0
Output
Output the number of the leaf where I have a small monkey.
Sample input
4 2
3 4
0 0
Sample output
12
7

Data structure Basic problem

#include <iostream>#include<cstring>using namespacestd;Const intN = -;intvis[1<<N];//tag Array, maximum number of nodes is 2^max-1.intMain () {intD, I;  while(Cin >> D >>I) {if(! D &&! I) Break;//d and I are zero endmemset (Vis,0,sizeof(VIS)); intn = (1&LT;&LT;D)-1, K;  for(inti =0; i < I; i++) {//continuous let I ball dropK =1;//Each operation starts at the root node       while(k <= N) {//Judging if it's out of boundsVIS[K] =!Vis[k]; K= Vis[k]?2*k:2*k+1;//Select path based on switch state}} cout<< k/2<< Endl;//leaf number before out of bounds  }} 

Nanyang OJ-63 Small Monkey Whereabouts (data structure-two fork tree)

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.