Main topic:
A stick of the length of the sticks a pile of ants climb, left or right, the speed is 1, if the two ants collide at the same time turn head (turned to ignore), ask T time after each ant position;
Input: T, (t sample), each sample input l,t,n, followed by n rows of two data per line, a POS (position), a dir (direction);
Output: Output the final position of each ant in the order of input, if it is in collision state, output turning, and drop output "Fell off";
Problem Solving Ideas:
The topic is similar to the Challenge program design of a water problem (POJ-1852 Ants), the idea of the problem; however, the input is not necessarily based on the location size of the input, therefore, an order array needs to be preprocessed, the specific look at the code and comments;
AC Code:
#include <iostream>#include<algorithm>#include<cmath>using namespacestd;/**************************************************** Sample: 1 R5 R3 L10 r************************************** **************/Const intMAXN =10000;structant{intId//input order; intPos//actual position; intDir//Orientation:}BEFORE[MAXN],AFTER[MAXN];BOOLCMP (ant a,ant B)//Sort by Location{ returna.pos<B.pos;}Const Chardire[][Ten]={"L","Turning","R"};//array Save output resultintORDER[MAXN];//the function of order array is to preserve the relative position of each ant's final state;intMain () {intT1; CIN>>T1; for(intKase=1; kase<=t1;kase++) {cout<<"Case #"<<kase<<":"<<Endl; intL,t,n; CIN>>l>>t>>N; for(intI=0; i<n;i++) { intx; CharC; CIN>>x>>C; intD= (c=='L'?-1:1); Before[i]=Ant{i,x,d}; After[i]=ant{0, x+d*t,d};//The direction is still d, because there must be an ant's direction is the original d, such as (1,r) (3,r), but do not know it is the ant;} sort (Before,before+n,cmp); for(intI=0; i<n;i++) {Order[before[i].id]=i;//Before[i].id is the input order of the original array, now before according to the position of the sorting, then the corresponding is the current position of the ant's number;//For example, after sorting before[1] is the position 3 ant, its order is 3, then is order[3]=2;//so the order in the example is 1 3 2 4;//The output After[order[1]].pos of the after array is the last position of the 5 position;} sort (After,after+n,cmp); for(intI=0; i<n-1; i++) { if(after[i].pos==after[i+1].pos) {After[i].dir=after[i+1].dir=0; } } for(intI=0; i<n;i++) { intA=Order[i]; if(After[a].pos <0|| AFTER[A].POS>L) cout<<"Fell off"<<Endl; Else{cout<<after[a].pos<<" "<<dire[after[a].dir+1]<<Endl; }} cout<<Endl; } return 0;}
Algorithmic Race Primer Classic-Training Guide (10881-PIOTR's Ants)