Ant pole issue

Source: Internet
Author: User

Ant pole issue

 Problem:There is a 27 cm fine wood pole, each of which has an ant in the five positions 3 cm, 7 cm, 11 cm, 18 cm, 23 cm. The wooden pole is very small and cannot pass through two ants 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. Write a program to find the minimum and maximum time for all ants to leave the wooden pole.


Ideas:Because it does not tell ant's initial orientation, it is necessary to initialize ant orientation. 0 indicates left-facing, 1 indicates right-facing, and binary 00000 (0) indicates that all five ants are left-facing. Then, 1 is added each time until 11111 (31) is added, represents all the initial orientations of five ants. After that, some ants may meet with each other over time. This is the orientation of the ant, which can be changed to 1 if the original orientation is 0, it turns from 1 to 0 ,. Here, we will think about how to determine the orientation of the ant financial. This can solve the problem through an exclusive or. For example, to determine the orientation of the First ant, you can use binary 10000 (16) to be different from the original orientation. If the result is 0, the orientation is 1, and the orientation is right. Otherwise, the opposite is true; the orientation of the Second ant can be determined by the difference between the binary 01000 (8) and the original orientation, and so on.


Code implementation:

Import java. util. *; public class Ant {public static void main (String args []) {consumer SC = new consumer (System. in); System. out. println ("Enter the location of the five ants (CM) (greater than 0 and less than 27 and cannot have the same):"); while (SC. hasNext () {int [] q = new int [5]; // {3, 7, 11, 18, 23}; enter the initial position of five ants for (int B = 0; B <q. length; B ++) {q [B] = SC. nextInt () ;}int maxtime =-1, mintime = 10000, signmax = 0, signmin = 0; for (int num = 0; num <= 31; num ++) {// five ant's initial orientation int I = num; // after an initial orientation, int [] p = new int [5]; // {3, 7, 11, 18, 23}; for (int B = 0; B <p. length; B ++) {// retain the initial value of q [] p [B] = q [B];} int sum = 0; // The time when all ants fall. while (1> 0) {if (f (p) = p. length) {// exit the loop when all five ants fall, and calculate the break of the next five ants.} sum ++; for (int j = 0; j <p. length; j ++) {// add or subtract int k = (int) Math by determining the orientation. pow (2, j); if (I & k) = 0) {p [j]-= 1 ;}else {p [j] + = 1 ;}} for (int j = 0; j <p. length-1; j ++) {// determine whether there is a meeting and record the orientation of the five ants if (p [j] = p [j + 1]) {int k = (int) Math. pow (2, j); if (I & k) = 0) {I-= k;} else {I + = k ;}}}} if (sum> maxtime) {maxtime = sum; signmax = num;} if (sum <mintime) {mintime = sum; signmin = num;} // System. out. print (sum + "");} System. out. println ("maxtime =" + maxtime + "seconds"); System. out. println ("mintime =" + mintime + "seconds \ n"); System. out. println ("Enter the location of the five ants (CM) (greater than 0 and less than 27 and cannot have the same):") ;}} public static int f (int a []) {// records the number of dropped ant financial records int k = 0; for (int I = 0; I <. length; I ++) {if (a [I]> = 27 | a [I] <= 0) {k ++ ;}} return k ;}}

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.