Recursive calculation of the number of possible problems in a fighter's target s-time dozen n Rings

Source: Internet
Author: User

Problem description

A warrior played 10 times and played 90 rings, asking how many possibilities there were and outputting these possible combinations.

Ideas

First, nesting 10-layer loops is not advisable, one is because the speed is too slow, and the second is if you change to hit 20 times the target will be finished.

In fact, this is a tree search problem.
1. For the first time hit 0 ring, then the second may play 0 ~ 10 Ring these may
2. Take the first hit of the 0 ring as root, the second time all the possible rings as the root of the child node
3. Repeat 1, 2 steps

This makes up a tree that represents all the possibilities when the first 0 rings are played. All we have to do is to traverse the tree from top to bottom, and when the sum of the nodes that pass is equal to 90 o'clock, that is the hit. Then change the root node value to 1 until 10.

So the question is, how many combinations does a tree need to traverse? Set the target number to T, then all the combined number = 1+ (one) t?1=1+ (11) 9 species. The result is already over 400 million, and it is obvious that all the time is not tolerated. We can prune the idea to remove some unnecessary traversal, that is, even if the next full dozen 10 ring can not meet the requirements of the 90 ring, if not the need to continue to recursion.

One more question, do we really have to manually create a tree-shaped data structure to perform the above procedure? If this is not a problem in theory, it will consume a lot of memory. In fact, we can use recursion to simulate tree traversal.

Realize

Defining methods

int shoot(intintleftint totalScores, Dequeue<Integer> path)

Indicates that the ring has been played score , but also to hit the left gun, the total number of rings for totalScores all the results. Here path is a stack data structure that records the path of a recursive call, thus recording the number of individual rings that may be combined at a time.

The complete code is as follows:

 Public  class Main {     Public Static intShoot_times =Ten; Public Static void Main(string[] args) {System.out.println (Shoot (0, Shoot_times, -,NewLinkedlist<> ())); }/** * Returns all results of score ring and only left gun with total number of rings Total_scores * @param score * @param left * @ param Path * @return * /     Public Static int Shoot(intScore,intLeftintTotalscores, deque<integer> Path) {inttot =0;if(1= = left) {//Pruning            //Remove the apparently impossible results            //In the case of the last shot, calculate the number of rings left in the 90-ring range,            //If the number of rings is greater than 10, it is impossible to fill            intLeft_scores = Totalscores-score;//When the remaining rings are between 0 and 10, this indicates a desirable combination            if(Left_scores >=0&& Left_scores <=Ten) {Path.push (left_scores);                Printstack (path);                Path.pop ();            ++tot; } path.pop ();returnTot } for(inti =0; I <=Ten; ++i) {//Pruning.            //Calculate how many rings are left when the score ring has been hit.            //If even the remaining 10 rings are still 90 rings, it is not a desirable result.            if(Totalscores-(score + i) <=Ten* left) {path.push (i); Tot + = shoot (score + I, left-1, totalscores, Path); }        }if(false= = Path.isempty ()) {Path.pop (); }returnTot }/** * Print out all elements in the stack * @param list */    Private Static void Printstack(deque<integer> list) {intIX =0;intLEN = List.size (); for(Integer n:list) {if(ix = LEN-1) {System.out.printf ("%d\n", n); Break; } System.out.printf ("%d,", n);        ++ix; }    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Recursive calculation of the number of possible problems in a fighter's target s-time dozen n Rings

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.