UVA-10881 Piotr's ants ideas

Source: Internet
Author: User

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 ;}

 

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.