Ant Climbing Rod Problem (Java Object oriented)

Source: Internet
Author: User

There is a 27 cm fine wood rod, in the 3rd centimeter, 7 cm, 11 cm, 18 cm, 23 cm, each of the five positions have an ant. The wooden rod is very thin and cannot pass through two ants at the same time. At first, the ants head to the left or to the right is arbitrary, they will only go forward or turn around, but will not retreat. When any two ants meet, two ants will turn around in the opposite direction at the same time. Suppose the ants can walk a centimeter of distance per second. Write a program that asks all ants to leave the wood pole for the minimum and maximum time.

Requirements: Using the class to simulate the behavior characteristics of ants, and then simulate the five ants on the wooden rod running process to program the solution. Can not be calculated directly by the formula in mathematical way.

Problem Solving Ideas:

Each ant as an object operation, there is POS (position), direction (direction), State properties, only need to move in the ant movement, each time to determine whether to walk out of the length of the rod (0~27), if out of range, immediately change the state of ants To determine if an ant has met, and if there is a meeting, change the direction of the ant immediately. Record time, output maximum and minimum.

Code implementation:

Class main{public static void Main (String args[]) {int min=999,max=-999; for (int i=0;i<=1;i++) {//Use five for to change the ant's initial direction for (int j=0;j<=1;j++) {for (int k=0;k<=1;k+)            +) {for (int m=0;m<=1;m++) {for (int n=0;n<=1;n++) {ant a=new ant (3);            A ant is placed at 3cm ant B=new ant (7);           b Ant c=new ant (11) at 7cm;           C Ant d=new Ant (18) at 11cm;           D ant at 18cm ant e=new Ant (23);       e ant at 23cm Place a.direction=i;       B.direction=j;       C.direction=k;       D.direction=m;       E.direction=n; for (int l=0;;         l++) {a.move ();          B.move ();         C.move ();         D.move ();         E.move ();        if (A.pos==b.pos) {//If there are ants to meet immediately change these two ants toward a.direction*=-1;b.direction*=-1;        } if (B.pos==c.pos) {b.direction*=-1;c.direction*=-1;        } if (C.pos==d.pos) {c.direction*=-1;d.direction*=-1; }        if (d.pos==e.pos) {d.direction*=-1;e.direction*=-1;         }//System.out.println (a.state+ "" +b.state+ "" +c.state+ "" +d.state); if ((a.state+b.state+c.state+d.state+e.state) ==5) {//If the state of each ant is 1, all out of the length of the lever if (L>max) {m         Ax=l;         } if (l<min) {min=l;         }//system.out.println (i+ "" +j+ "" +k+ "" +m+ "" +n ");         System.out.println (l);        Break      }}}}}}}system.out.println("All ants fall down the minimum time is:" +min ";   System.out.println ("All ants fall down the maximum time is:" +max);                 }} class ant{//Ant object int pos;           position int direction;             toward int state=0;  State public Ant (int pos) {this.pos=pos; } public void Move () {//Move function if (this.pos<=0| |   THIS.POS&GT;=27) {//Beyond the range of wood rods to change the state of this ant state=1;  } if (this.direction==0) {this.direction=-1;  } this.pos=this.pos+direction; }}
Results:

Ant Climbing Rod Problem (Java Object oriented)

Related Article

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.