NYOJ-63 Small Monkey whereabouts (binary tree and optimization algorithm detailed)

Source: Internet
Author: User

Small monkey Fall 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 23 40 0
Sample output
127

The first feeling is that the topic of thinking and clarity, at the beginning of the thought of directly calculate the answer output, considering the data structure is learning, so still choose to use the tree to solve the violence.

I had no idea that I had a good luck. Learn from the friends of the Learning tree together.

The optimization algorithm is also explained below:

The tree writes the following code:

1 #include <iostream> 2 #include <queue> 3 #include <cmath> 4 using namespaceStd 5 structNode 6{7 intData 8 intFlag 9 Node *lchild,*Rchild;10Node (); 11};12Node::node (13){Flag=-1; rchild=lchild=Null;16}17 void createtree (int d,node *&Root) 18{Queue<node *>Q;20 while (!Q.empty ()) 21Q.pop (); root=newnode;23 static int count=0; root->data=++Count;25Q.push (root); + node *t=Root;27 while (Count!=pow (2,d)-1) 28{t=Q.front (); 30Q.pop (); t->lchild=newNode;32 t->lchild->data=++Count;33 Q.push (t->Lchild); t->rchild=newNode;35 t->rchild->data=++Count;36 Q.push (t->Rchild); 37}38 t=null;39 count=0; 40}/*42 void Levelorder (node *root) 43 {//Queue implementation Queue<node *> q;45 node *t=root;46 if (t!=null) Q.push (t);//root non-empty , the queue of the!q.empty ()//team is not empty t=q.front (), Q.pop ();//The Team cout<<t->data<< ""; ) Q.push (T->lchild); Traverse left child, if (t->rchild) Q.push (t->rchild); Traverse Right Child}57}59 * /void Go (int &t,node *&root) (root->lchild&&root-> rchild) {if (root->flag==-1) + ( T, root->lchild); root->flag=1 }68 else69 (t,root-> rchild); root->flag=-1< c10>;72 }73 }74 else75 t=root->data;76 }77 Int. int Main () + int d,num;81 while ( cin>>d>>num,d&&num) {*root= nodenull;83 createtree (d,root); + int t;85 for (int i=0;i<num;i++) t,root Go, cout<<t<<endl;88 }89 return 0; /c25> 

But if the test data has n groups, the number of layers D has 19 layers (d<=20), then the tree will establish 2^19-1 nodes, time and space is very expensive. So what?

Let's talk about the optimization algorithm:

1
2 3
4 5 6 7
8 9 10 11 12 13 14 the

  

According to the test data of the right figure, there are n rows (3,4,5), x monkeys in every 2^n appear a cycle, the reason is that it is full of two fork tree.

According to figure four on the left we list the data to see:

The 1th Monkey 1 2 4 8
The 2nd Monkey 1 3 6 12
The 3rd Monkey 1 2 5 10
The 4th Monkey 1 3 7 14
The 5th Monkey 1 2 4 9
The 6th Monkey 1 3 6 13
The 7th Monkey 1 2 5 11
The 8th Monkey 1 3 7 15

Please look at the four-layer binary tree (upper left) and the above table is not difficult to find that the number of times to enter the nth node I is odd (that is, there have been n-1 over the monkey visited the node), then traverse its Zogen;

If it is an even number, it traverses the right child root.

Therefore, the control of the above table, the rule: I is odd, k=k*2;i= (i+1)/2;//First I enter the left sub-tree

I is an even number, K=K*2+1;I=I/2;//I enter right sub-tree

For example

1th Monkey: For the first node, I=1 is odd, then the next node to go k=1*2=2, then i= (/2=1) (first into the left subtree), continue to judge the parity of the left subtree I ...

3rd Monkey: For the first node, i=3 is odd, then the next node to go k=1*2=2, then i= (3+1)/2=2 (the second into the left subtree) ...

The 5th Monkey: for the first node, i=5 is odd, then the next node to go k=1*2=2, then i= (5+1)/2=3 (third into the left subtree) ...

......

1 for (int j=0;j<d-1;j++) 2        if (i%2) {k=k*2;i= (i+1)/2;} 3        else {k=k*2+1;i/=2;} 

OK then write the complete algorithm according to the input criteria as follows:

1   2 #include <iostream> 3 using namespace std; 4  5 int{7     int d,i,k; 8 while     (cin>& Gt;d>>i && (d+i)!=0 {ten k=1; one for (int j=0;j<d-1;j++) (i%2){k=k*2;i= (i+1)/2;} else {k=k*2+1;i/=2;} cout<<k<<         

Of course, you can shift the/2 to a bitwise operation to the left one, which is more efficient.

NYOJ-63 Small Monkey whereabouts (binary tree and optimization algorithm detailed)

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.