Uva10881-Algorithm Introduction Classic Training Guide

Source: Internet
Author: User

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&GT;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

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.