There are many ants in a straight line. Each ant moves at a speed of 1 and has an initial direction. And when two adjacent ants collide, they turn. Now I want to ask the location of each ant after t time.
Solution: One of the fatal skills of this question is to regard the collision of two ants as the number of two ants that exchange each other and exchange the numbers of the ants. This is very understandable, similar to a physical fully elastic collision. Since the relative positions of any two ants do not change their relative positions in such a turn, we only need to consider that all the ants do not have ant actions. Finally, it maps to the original location relationship based on the location relationship. When determining the position, check whether it is beyond the coordinates.
The Code is as follows:
# Include <cstdlib> # include <cstring> # include <cstdio> # include <algorithm> # include <iostream> using namespace STD; const int INF = 0x7fffffff; int le, Ti, n; struct ant {int Pos; int dir; int no; bool operator <(const ant & Other) const {return POS <Other. pos;} e [10005]; int HX [10005]; int main () {int T, CA = 0; char dir [5]; scanf ("% d ", & T); While (t --) {scanf ("% d", & Le, & Ti, & N); For (INT I = 1; I <= N; ++ I) {scanf ("% d % s", & E [I]. POs, DIR); e [I]. no = I; E [I]. dir = dir [0] = 'l '? -1: 1;} e [0]. pos =-INF, E [n + 1]. pos = inf; sort (E + 1, E + 1 + n); For (INT I = 1; I <= N; ++ I) {HX [E [I]. no] = I; // record a ing between the input sequence and the relative sequence of the ant financial. E [I]. pos + = Ti * E [I]. dir;} Sort (E + 1, E + 1 + n); printf ("case # % d: \ n", ++ CA); For (INT I = 1; I <= N; ++ I) {int P = HX [I]; If (E [p]. pos <0 | E [p]. pos> le) {puts ("fell off");} else if (E [p]. pos = E [P-1]. pos | E [p]. pos = E [p + 1]. pos) {printf ("% d turning \ n", E [p]. Pos);} else {printf (E [p]. dir =-1? "% D l \ n": "% d r \ n", E [p]. Pos) ;}} puts ("") ;}return 0 ;}