Analysis on the algorithm of Fox searching for rabbits

Source: Internet
Author: User

Topic:

Around the top of the mountain there are 10 holes, a rabbit and a fox live in their own hole, the fox always want to eat rabbits, one day the rabbit said to the fox, you want to eat I have a condition, you first put the hole number 1 to 10, you start from the 10th hole, first to the 1th hole looking for me, the second every hole to find me, the third time two holes to find me And so on, and so on, if you can find me you can feast on a meal, not found before I can not stop, fox think only 10 holes, the number of search for unlimited, which can not find the truth, agreed to the conditions, the results of the Fox ran faint in the past also did not find rabbits, ask the rabbit to hide in which hole. The fox can be assumed to have been found 1000 times in the program.

There are two cases corresponding to several algorithms, if the fox over the limit, such as the fox in the ninth hole, the next search if you want to go to partition n hole, 9+n greater than 10 case, the fox is back to 1 holes to something else is, from 10 hole back to 10,9,8,7, the order to find, this corresponds to two algorithms

The first kind of fox back to 1 is relatively simple, can be extended to the hole as 1,2,3,4....8,9,10,1,2,3....8,9,10,1,2,3 .... This kind of cycle, so there is no directivity problem, only need to be based on the displacement of 10 to obtain the actual displacement can be, Mark the fox's newly reached hole, skip it the next time, and reverse-screen the resulting output to get the hole the fox cannot reach.

The second can be seen as a sequence of 18 elements from the 1,2,3...7,8,9,10,9,8,7.....,3,2,1, which can also eliminate the trouble of directionality, the Fox can think in the previous sequence to do the loop, the algorithm is similar to the above.

The third kind is the hard calculation defines the direction value, obtains the surplus to obtain the displacement. The actual displacement is A+bi, I is the direction value, the value can be 1 and -1,b is the displacement after the remainder, a is the initial position

Now, what you can think of is these methods

The C program based on the second method is posted below

#include <stdio.h>void main () {int a[18]={0}; int Fox=1;int place=0;int weiyi,p;for (p=0;p<1000;p++) {weiyi= (p+ 2)%18;place= (Place+weiyi)%18;if (a[place]==1) continue;a[place]=1;} for (int i=0;i<10;i++) {if (a[i]==0) printf ("Hole%d is safe\n", i+1);}}



Analysis on the algorithm of Fox searching for rabbits

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.