Getting started with algorithms Classic Training Guide Chapter I example 5
This problem personal feeling is still very technical content, if pure simulation can kill.
The situation in which an ant is turned around in a collision is finally seen as "over-worn", and this "transformation thought" is very powerful. If only I had this skill one day.
Personal feeling if you can't see it, the problem hangs.
So when dealing with complex problems, if directly to do very complex, very troublesome, it should be a way to adapt, such as the topic, we find common ground, and found that their direction is always the opposite, the speed is always the same, then can be regarded as a hair like ants.
Of course, after processing, there are some small details, such as the relative position of all ants are constant ...
The more annoying is the input and output order. And because of the order, it is possible to find the original position, so we need to keep track of the current I is the original input when the first few. and introduced the order.
Overloaded comparison operators, as well as struct member constructs, are also tips to be aware of.
Attach the code.
#include <cstdio>#include<iostream>#include<algorithm>using namespacestd;Const intmaxn=10000+5;structant{intID; intPOS; intdir; BOOL operator< (ConstAnt &a)Const{ returnpos<A.pos; }}BEFORE[MAXN],AFTER[MAXN];Const Chardirname[][Ten]={"L","Turning","R","Fell off"};intORDER[MAXN];intMain () {intK; scanf ("%d",&K); for(intKase=1; kase<=k;kase++){ intL,t,n; printf ("Case #%d:\n", Kase); scanf ("%d%d%d",&l,&t,&N); for(intI=0; i<n;i++){ intp,d; CharC; scanf ("%d%c",&p,&c); D= (c=='L'?-1:1); Before[i]=(ant) {i,p,d}; After[i]= (ant) {0, p+t*D,d}; } sort (Before,before+N); Sort (After,after+N); for(intI=0; i<n;i++) order[before[i].id]=i; 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) printf ("%s\n", dirname[3]); Elseprintf"%d%s\n", after[a].pos,dirname[after[a].dir+1]); } printf ("\ n"); } return 0;}
Uva10881-Algorithm Introduction Classic Training Guide