UVA10881 Piotr's Ants (模拟)

来源:互联网 发布:工程数据的内涵 编辑:程序博客网 时间:2024/06/04 18:56

One thing is for certain: there is no stopping them;
the ants will soon be here. And I, for one, welcome our
new insect overlords.”
Kent Brockman
Piotr likes playing with ants. He has
n
of them on a horizontal pole
L
cm long. Each ant is facing
either left or right and walks at a constant speed of 1 cm/s. When two ants bump into each other, they
both turn around (instantaneously) and start walking in opposite directions. Piotr knows where each
of the ants starts and which direction it is facing and wants to calculate where the ants will end up
T
seconds from now.
Input
The rst line of input gives the number of cases,
N
.
N
test cases follow. Each one starts with a line
containing 3 integers:
L
,
T
and
n
(0

n

10000). The next
n
lines give the locations of the
n
ants
(measured in cm from the left end of the pole) and the direction they are facing (
L
or
R
).
Output
For each test case, output one line containing
Case x followed by
n
lines describing the locations
and directions of the
n
ants in the same format and order as in the input. If two or more ants are at
the same location, print

Turning
’ instead of
L
' or

R
’ for their direction. If an ant falls off the pole
before
T
seconds, print `
Fell off
’ for that ant. Print an empty line after each test case.
SampleInput
2
10 1 4
1 R
5 R
3 L
10 R
10 2 3
4 R
5 L
8 R
SampleOutput
Case #1:
2 Turning
6 R
2 Turning
Fell off
Case #2:
3 L
6 R
10 R

题目乱七八糟的,不过没事,代码也很乱,用了几个结构体,其实都是可以合并成一个的,当时写的太乱了。

这道题关键点就是:
蚂蚁虽然相遇了就会互相掉头返回,但是事实上我们可以把两个蚂蚁看成直接互相穿过,不受影响。
事实上,第一只蚂蚁无论时间多久,它最终的位置还是第一只蚂蚁,虽然我们上面看成直接穿过去了,但是事实上它们没有,所以第一只还是第一只。
我们通过这一点可以直接计算位置,至于方向,由于我们看成的是直接穿过,所以方向不会变。
这道题,有一点不懂的,就是边界判断必须是”<0”和”>L”才行,这不太符合常理,我认为应该是“<1”和”>L”才是。

#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;#include<cassert>const int maxn=1e4+5;struct note {    int pos;    char der[15];    int now;} a[maxn];struct nots {    int id;    int nums;    bool operator<(const struct nots aa)const {        return nums<aa.nums;    }} arr[maxn];int old[maxn];struct notes {    int pos;    char der[5];    bool operator<(const struct notes aa)const {        return pos<aa.pos;    }} nows[maxn];int main() {#ifdef tangge    freopen("UVA10881.txt","r",stdin);    freopen("Wronganswer.txt","w",stdout);#endif // tangge    int n,Tcase,T,L;    scanf("%d",&Tcase);    for(int Case=1; Case<=Tcase; ++Case) {        scanf("%d%d%d",&L,&T,&n);        for(int i=0; i<n; ++i) {            scanf("%d%s",&a[i].pos,a[i].der);//            assert((a[i].pos>=0&&a[i].pos<=L));            arr[i].id=i;            arr[i].nums=a[i].pos;            nows[i].pos=a[i].pos+(a[i].der[0]=='L'?-1:1)*T;            strcpy(nows[i].der,a[i].der);//这个方向要跟着跑,不然会错的        }        sort(arr,arr+n);        for(int i=0; i<n; ++i) {            old[i]=arr[i].id;//要输出原来蚂蚁的位置,所以排序之前保留原来蚂蚁的位置信息        }        sort(nows,nows+n);        for(int i=0; i<n; ++i) {            a[old[i]].now=nows[i].pos;//上面old数组存放的是i对应的编号,所以这里的i对应着old[i]            strcpy(a[old[i]].der,nows[i].der);//方向一直要跟着走        }        for(int i=0; i<n-1; ++i) {            if(nows[i].pos==nows[i+1].pos)//这是排了序的,所以如果一样的话,就是Turning                strcpy(a[old[i]].der,"Turning"),strcpy(a[old[i+1]].der,"Turning");        }        printf("Case #%d:\n",Case);        for(int i=0; i<n; ++i) {            if(a[i].now>L||a[i].now<0) {                puts("Fell off");//边界判断                continue;            }            printf("%d %s\n",a[i].now,a[i].der);            //为什么可以直接输出i,而不用old数组转化?因为old数组的i对应着原来的编号顺序,这里当然直接用i        }        putchar(10);    }    return 0;}
0 0
原创粉丝点击