UVa 10881 Piotr’s Ants

本文探讨了Piotr在时间限制下模拟蚂蚁在水平杆上行走的问题,详细描述了输入输出格式,提供了代码实现,展示了如何计算蚂蚁在特定时间后的状态。
Piotr's Ants
Time Limit: 2 seconds

 

"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 poleL 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 upT seconds from now.

Input
The first line of input gives the number of cases, NN test cases follow. Each one starts with a line containing 3 integers:L ,T and n(0 <= n <= 10000). The nextn lines give the locations of then 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 byn lines describing the locations and directions of then 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 polebefore T seconds, print "Fell off" for that ant. Print an empty line after each test case.

Sample InputSample Output
2
10 1 4
1 R
5 R
3 L
10 R
10 2 3
4 R
5 L
8 R
Case #1:
2 Turning
6 R
2 Turning
Fell off

Case #2:
3 L
6 R
10 R

 

 蚂蚁相对位置不会改变

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <map>
 4 #include <algorithm>
 5 using namespace std;
 6 map <int,int> v;
 7 struct Node
 8 {
 9     int loc;
10     char direc;
11     int num;
12 }a[10005];
13 bool cmp(Node x,Node y)
14 {
15     return x.loc<y.loc;
16 }
17 bool cmp2(Node x,Node y)
18 {
19     return x.num<y.num;
20 }
21 int main()
22 {
23     int T,n,i,j,k=0,l,t;
24     int nu[10005];
25     scanf("%d",&T);
26     while(T--)
27     {
28         k++;
29         v.clear();
30         scanf("%d%d%d",&l,&t,&n);
31         for(i=1;i<=n;i++)
32         {
33             scanf("%d",&a[i].loc);
34             getchar();
35             scanf("%c",&a[i].direc);
36             a[i].num=i;
37         }
38         sort(a+1,a+n+1,cmp);
39         for(i=1;i<=n;i++)
40             nu[i]=a[i].num;
41         for(i=1;i<=n;i++)
42         {
43             if(a[i].direc=='L')
44             {
45                 a[i].loc=a[i].loc-t;
46             }
47             else if(a[i].direc=='R')
48             {
49                 a[i].loc=a[i].loc+t;
50             }
51             v[a[i].loc]++;
52         }
53         sort(a+1,a+n+1,cmp);
54         for(i=1;i<=n;i++)
55             a[i].num=nu[i];
56         /*for(i=1;i<=n;i++)
57             printf("%d ",a[i].num);
58         printf("&&*\n");*/
59         sort(a+1,a+n+1,cmp2);
60         printf("Case #%d:\n",k);
61         for(i=1;i<=n;i++)
62         {
63             if(a[i].loc<0 || a[i].loc>l)
64                 printf("Fell off\n");
65             else
66             {
67                 if(v[a[i].loc]>1)
68                     printf("%d Turning\n",a[i].loc);
69                 else
70                     printf("%d %c\n",a[i].loc,a[i].direc);
71             }
72         }
73         printf("\n");
74         /*for(i=1;i<=n;i++)
75             printf("%d ",a[i].num);
76         printf("&&*\n");*/
77     }
78     return 0;
79 }
View Code

 

转载于:https://www.cnblogs.com/cyd308/p/4669812.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值