One programming question every day ---- Baidu ant Crawler

Source: Internet
Author: User

There is a 27 cm fine wood pole, each of which has an ant in the five positions 3 cm, 7 cm, 11 cm, 17 cm, 23 cm. The wooden pole is very small and cannot pass through an ant at the same time. At the beginning, the head of the ant financial system is either left or right. They only move forward or turn their heads, but do not move back. When any two ants meet, they both turn their heads to the opposite direction. It is assumed that the ant Financial can walk 1 cm away every second. WriteProgramThe minimum time and maximum time for all ants to leave the wooden pole.

Seeing this question, I can use the thought of the mouse's poison test problem that I encountered a few days ago, because at first the ant was uncertain in the left-to-right position, therefore, a method is required to describe the position of the ant head. Because an ant is either right or left, there are five ants in total, we can use a five-digit binary number to represent the direction of the five ants at the beginning. For example, 00001 (0 indicates right, 1 indicates left) indicates that the first ant head is right, and the second ant head is right ...... the fifth ant turned his head to the left, and so on until 11111. Then he got a combination of five ant's heads on the wooden stick and made a loop. Use hashmap to store the serial numbers and their locations of the ant financial. Each time an ant financial leaves one, it will be removed from the hashmap until all the ant financial members leave. Because all the ants are in an odd number, it is impossible for two ants to meet each other in half a centimeter.AlgorithmThere must also be a counter. When all the ants move the 1 cm position, the counter will be + 1 to calculate the time when the ants move. If the time is smaller than mintime, save the time. If the time is longer than maxtime, save the record. Finally, the maximum and minimum time for ant financial to leave is obtained. The JavaCode:

Import Java. util. hashmap; public class testbaiduant {public static void move (hashmap hm, char [] CHS) {for (INT I = 0; I <chs. length; I ++) {int num = integer. parseint (CHS [I] + ""); integer num2 = (integer) Hm. get (I + 1); int S = num2; If (num = 0) {S + = 1; HM. remove (I + 1); HM. put (I + 1, S);} else {S-= 1; HM. remove (I + 1); HM. put (I + 1, S) ;}} public static void meetandrevertion (hashmap hm, char [] CHS) {for (INT I = 1; I <5; I ++) {for (Int J = I + 1; j <= 5; j ++) {int in = (integer) Hm. get (I); int in2 = (integer) Hm. get (j); If (in = in2) {If (integer. parseint (CHS [I-1] + "") = 0) {CHS [I-1] = '1'; CHS [J-1] = '0 ';} else {CHS [I-1] = '0'; CHS [J-1] = '1' ;}}} public static string leftzero (string value, int Len) {If (value. length () <Len) {int minus = len-value.length (); For (INT I = 0; I <minus; I ++) {value = "0" + value ;}} return Value;} public static Boolean checkleaveall (hashmap hm) {Boolean resu Lt = false; int COUNT = 0; For (INT I = 1; I <= 5; I ++) {INTEGER in = (integer) Hm. get (I); If (in <= 0 | in >=27) {count ++ ;}} if (COUNT = 5) {result = true ;} return result;} public static void main (string [] ARGs) {int maxtime = 0; int mintime = 9999999; char [] minlocation = NULL; char [] maxlocation = NULL; for (INT I = 1; I <32; I ++) {hashmap Hm = new hashmap (); HM. put (1, 3); HM. put (2, 7); HM. put (3, 11); HM. put (4, 17); HM. put (5, 23); int time = 0; String Nums = integer. tostring (I, 2); string num = leftzero (Nums, 5); char [] CHS = num. tochararray (); While (! Checkleaveall (HM) {time ++; move (HM, CHS); meetandrevertion (HM, CHS) ;}if (time <mintime) {mintime = time; minlocation = CHS;} else if (Time> maxtime) {maxtime = time; maxlocation = CHS;} system. out. println ("0 indicates head to right, 1 indicates head to left"); system. out. println ("maximum time:" + maxtime); For (INT I = 0; I <5; I ++) {system. out. print ("when the maximum time is" + (I + 1) + "the direction of the ant header is:" + maxlocation [I] + "");} system. out. println (); system. out. println ("shortest time:" + mintime); For (INT I = 0; I <5; I ++) {system. out. print ("when the shortest time is" + (I + 1) + "the direction of the ant header is:" + minlocation [I] + "");}}}

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.