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<<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)