好吧,看到这题首先就想到了 每1s都去更新蚂蚁的位置,最后发现这样每次都要判断当前的这只蚂蚁和其他蚂蚁的位置关系
这样子不可行。
最后看了大白书之后, 才发现原来这么吊。。。
代码:
#include<stdio.h> #include<math.h> #include<string.h> #include<algorithm> #include<limits.h> using namespace std; struct ant { int pos;//蚂蚁的位置 int d; //蚂蚁的方向 int x; //蚂蚁输入的位置 }a[11111],hehe[11111]; int cmp1(ant x, ant y) { return x.pos < y.pos; }//按位置排序 int cmp2(ant x, ant y) { return x.x < y.x; } //按输入顺序排序 int main() { int t; scanf("%d",&t); for(int k= 1; k<= t; k++) { int L,T,N; scanf("%d %d %d",&L,&T,&N); for(int i= 1; i<= N; i++) { int p; char ch; scanf("%d %c",&p,&ch); a[i].pos= p; if(ch == 'L') a[i].d= -1; else if(ch == 'R') a[i].d= 1; a[i].x= i; } sort(a+ 1, a+ N+ 1, cmp1); for(int i= 1; i<= N; i++) { hehe[i].d = a[i].d; if(a[i].d == -1) hehe[i].pos= a[i].pos - T; //左减 else hehe[i].pos= a[i].pos + T; //右加 } sort(hehe+ 1, hehe+ 1+ N,cmp1); for(int i= 1; i<= N; i++) { a[i].pos= hehe[i].pos; if(hehe[i].pos != hehe[i+1].pos && hehe[i].pos != hehe[i-1].pos) a[i].d= hehe[i].d; //相碰的特判 else a[i].d= 0; } sort(a+ 1, a+ N+ 1, cmp2); printf("Case #%d:\n",k); for(int i= 1; i<= N; i++) if(a[i].pos < 0 || a[i].pos > L) printf("Fell off\n"); else { if(a[i].d == 0) printf("%d Turning\n",a[i].pos); else if(a[i].d == 1) printf("%d R\n",a[i].pos); else if(a[i].d == -1) printf("%d L\n",a[i].pos); } printf("\n"); } return 0; }