Ant over pole problem (ii)------Java Object-oriented processing

Source: Internet
Author: User

question: 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.

idea: because the ants are not told the initial orientation, so to initialize the ant direction. Use 0 for left, 1 for right, and binary 00000 (0) to indicate that five ants are headed left, and then add 1 each time to 11111 (31), representing all the initial orientations of the five ants. With the passage of time, some ants may meet, this is the direction of the ant to modify the line, that is: if the original direction is 0, then becomes 1, the original is 1 to 0,. The way to do this is to think about how to judge an ant facing the problem, which can be solved by XOR. such as: When the first ant to judge the direction, you can use the binary 10000 (16) and the original direction of the XOR, if the result is 0, then the direction is 1, toward the right, otherwise, with the binary 01000 (8) and the original direction of the different or, you can judge the direction of the second ant, and so on.

Note: After the new object array, also remember to new the object array again, knowing that the last is the base type.

`Ant a[]=new ant[5];for (int i=0;i<a.length;i++) {a[i]=new ant ();}`

Code implementation:

`Import Java.util.*;class ant{int direction,position;public Ant (int d,int p) {this.set (d,p);} Public Ant () {//makes the initial value 0this.set (0,0);} Public Ant (Ant a) {this.set (a.direction,a.position);} public void Set (int d,int p) {this.direction=d;this.position=p;} public void Change (ant a[],int N) {//changes the initial orientation of the ant and changes the initial orientation by adding a. such as: 00000+1---> 00001,00010---> 111 until 11111. (equivalent to binary addition) if (n<a.length) {a[n].direction+=1;if (a[n].direction>1) {///greater than 1 o'clock, the original value becomes 0 and the next one into the 1a[n].direction=0;// The original value becomes 0change (a,n+1);//The next one enters the 1}}return;} public void Move (ant a) {//Ant move if (Ant.direct (a)) {//If 0, move left, 1, move a.position--to the right;} else{a.position++;} a.direction=a.direction&1;} public static Boolean direct (ant a) {//To determine the ant's Orientation Problem return (a.direction&1) ==1;} public int fallnumb (ant a[]) {//record the number of ants falling down int k=0;for (int i=0;i<a.length;i++) {if (a[i].position>=27| | a[i].position<=0) {k++;}} return k;} Public String toString () {return ' direction: ' +this.direction+ ' position: ' +this.position;}} Class Main{public static void Main (string[] args) {ScanNER sc=new Scanner (system.in); Ant ant=new Ant (); Ant a[]=new ant[5];int n;for (int i=0;i<a.length;i++) {//Remember, this must be in new, for beginners Java I got stuck here a[i]=new Ant ();} for (int i=0;i<a.length;i++) {a[i].position=sc.nextint (); a[i].direction=0;} Ant b[]=new ant[5];for (int i=0;i<b.length;i++) {b[i]=new ant ();} int min=99999,max=-99999,step=0;for (int i=0;i<=31;i++) {for (int k=0;k<b.length;k++) {//retains the initial position of the ant, but changes the initial orientation of the B[k ].position=a[k].position;b[k].direction=a[k].direction;} step=0;//Record Steps Ant.change (a,0), while (true) {if (Ant.fallnumb (b) ==5) {//If the number of ants dropped is 5, then end break;}   step++;for (int k=0;k<5;k++) {//Ant move ant.move (B[k]);} /*for (int k=0;k<b.length;k++) {System.out.print (b[k].position+ "");} System.out.println (); */for (int k=0;k<4;k++) {//Determine whether to meet if (b[k].position==b[k+1].position) {0 change to 0b[k] if you meet. direction= (b[k].direction) &1;b[k+1].direction= (b[k+1].direction) &1;}} if (Step>max) {max=step;} if (step<min) {min=Step;} }system.out.println ("Max Time=" +max+ "second."); System.out.println ("min time=" +min+ "second.");}}`

Ant over pole problem (ii)------Java Object-oriented processing

Related Keywords:
Related Article

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.