"Technology to" ants

Source: Internet
Author: User

The original question is the second question of the simulation of the Guide magazine.

Do not give you complete sample data, because I lost =. =

Start.

* The following variable names are used: Size: Map width

I thought about three ways to do it.

1. Move at the same time, the order after the weight, time complexity is O (size*nlog2n) But because the sentence is too difficult to write, give up

2. Simultaneous move, hash weight, use h (k) = (x[k] * Size + y[k]) mod p, time complexity O (size*n) always error, give up

3. In a different way, you can "experiment" with all the collision events, and then deduce it based on the time of the event, Time complexity O (size*n2)--à I realized this

Based on 3, derive a metaphysical formula (apparently feasible), pre-contract collision time, Time complexity O (n2)

5*. According to the Metaphysics of 4, maintain a tree of line segment tree, with this to find and an ant collided with all the ants, Time complexity O (n (log2n) 2)

The first step is to fix some details.

Set two of these ants collided (XT,YT), it is possible, XT,YT is a real number, after the decimal point of 0.5, so the whole map unit length doubled.

The second step is to solve four directions, as follows

West, Horizontal +1

East, Horizontal axis-1

North, Ordinate +1

South, Ordinate-1

The function move (Int&x,int&y,char c) is designed according to four directions, which indicates that the movement time is 1 and change (x, y) according to the direction of C.

According to the three rule, we have a collision test for each pair of ants <i,j>, while simulating the motion of both, in the event of a collision or an ant out of the map ①, recording the two ants ' number I, j, and the expected occurrence of the collision time t

If there are two time t1,t2, there is t1<t2, because the ants will disappear, so in the T1 is expected to collide with the two ants, will not participate in T2 collision.

In another case, if there are two time t1,t2, there are t1=t2, because many ants at the same point will collide, then all the ants will disappear.

So we sort all the collision events collected in the experiment in ascending order based on t as the keyword. Ii

Design an array {vis[i]},vis[i]=j means that ant I disappeared at time J, judging by the above two rules, recording vis[i], scanning out all the existing ants, that is the answer ③

① a total of N2 <i,j> time complexity is O (N2), but each ant will be simulated Size over and over, into O (size*n2)

② If a quick sort is used, then the time complexity is O (nlog2n)

③ Obviously this is O (N2).

So the time complexity of the algorithm is O (size*n2)

"The following is an unproven metaphysical algorithm"

Complexity of Time: O (n2)

Algorithm Flow:

"I think this is called algorithmic flow will be hit" <<-there's a word in it.

For a pair of ants <x,y><a,b>, move for C1,C2, deduce as follows

Category discussion Move direction

C1=n,c2=s, if X=a,y<b so t= (y+b)/2;

C1=e,c2=w, if Y=b,x<a so t= (x+a)/2;

C1=w,c2=n, if X-a=y-b so t=x-a;

...

Similarly, we can talk about the rest of the situation (Ws,es,en) (I vaguely think there is a way not to divide so many classes)

Eh, seems to be able to take a step toward the trend ... In fact, no matter how simplified to a statement to resolve four judgments, and four statements are equivalent, but there is space for discussion.

#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespacestd;intG,n,m,ans;intvis[Wuyi];structdata{intu,v,t;} e[ the];structpair{intFirst,second; Charmov;} p[Wuyi];BOOLMoveint&a,int&b,Charc) {    if(c=='N') b+=1; if(c=='S') b-=1; if(c=='W') a-=1; if(c=='E') a+=1; if(a> -|| a<- -)return 0; if(b> -|| b<- -)return 0; return 1;}intTestintAintb) {    intt=0; intX1=p[a].first,y1=p[a].second,x2=p[b].first,y2=P[b].second; CharC1=p[a].mov,c2=P[b].mov;  while(Move (X1,Y1,C1) &&Move (X2,Y2,C2)) {T++; if(x1==x2&&y1==y2) {m++; E[M].U=A; E[M].V=b; E[M].T=T; return 1; }    }}intCMP1 (ConstData&a,Constdata&b) {    returna.t<b.t;}intCMP2 (ConstPair&a,Constpair&b) {    returna.first<b.first| | (a.first==b.first&&a.second<b.second);}intMain () {Freopen ("ant.in","R", stdin); Freopen ("Ant.out","W", stdout); CIN>>G;  for(intI=1; i<=g;i++) {cin>>N;  for(intI=1; i<=n;i++) cin>>P[i].mov;  for(intI=1; i<=n;i++) {cin>>p[i].first>>P[i].second; P[i].first<<=1;p [i].second<<=1; } sort (P+1, p+n+1, CMP2);//for (int i=1;i<=n;i++) {//cout<<p[i].first<< ' <<p[i].second<<endl;//        }         for(intI=1; i<n;i++) for(intj=i+1; j<=n;j++) test (I,J); Sort (e+1, e+m+1, CMP1);  for(intI=1; i<=m;i++)/*printf ("%d (%c)-(%d,%d)%d (%c)-(%d,%d)%d\n", E[i].u,                                                            P[e[i].u].mov, P[e[i].u].first, P[e[i].u].second, e[                                                            I].V, P[e[i].v].mov,                                                            P[e[i].v].second, P[e[i].v].second, E[I].T);*/         for(intI=1; i<=m;i++){            intx=e[i].u,y=e[i].v; if(vis[x]==0&&vis[y]==0) {Vis[x]=vis[y]=e[i].t; Ans+=2; }            Else                if(vis[x]==e[i].t&&vis[y]==0) {Vis[y]=e[i].t; Ans+=1; }                Else                    if(vis[y]==e[i].t&&vis[x]==0) {Vis[x]=e[i].t; Ans+=1; }} printf ("%d\n", N-ans); Ans=0; m=0; memset (Vis,0,sizeof(VIS)); }     return 0;}

"Technology to" ants

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.