UVA 10881 Piotr's Ants(等效变换 sort结构体排序)

Piotr's Ants
Time Limit: 2 seconds

 


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.Kent Brockman

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 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 Tseconds, print "Fell off" for that ant. Print an empty line after each test case.

Sample Input

2
10 1 4
1 R
5 R
3 L
10 R
10 2 3
4 R
5 L
8 R

Sample Output
Case #1:
2 Turning
6 R
2 Turning
Fell off

Case #2:
3 L
6 R
10 R

题目大意:一根长度为L厘米的木棍上有n只蚂蚁,每只蚂蚁要么朝左爬要么朝右爬,速度为1厘米/秒。当两只蚂蚁相撞时,二者同时掉头(掉头时间忽略不计)。给出每只蚂蚁初始位置和朝向,计算T秒之后每只蚂蚁的位置。

分析:在远处观察蚂蚁运动,由于黑点太小,所以当蚂蚁碰撞掉头时,看上去和两个点“对穿而过”没有任何区别。换句话说,如果把蚂蚁看成没有区别的小点,那么只需独立计算出每只蚂蚁在T时刻的位置即可。比如,3只蚂蚁,蚂蚁1=(3,R),蚂蚁2=(3,L),蚂蚁3=(4,L),则两秒钟之后,3只蚂蚁分别为(3,R),(1,L)和(2,L)。
而且所有蚂蚁的相对顺序保持不变,因此把所有目标位置从小到大排序,则从左到右的每个位置对应于初始状态下从左到右的每只蚂蚁。由于原题中的蚂蚁不一定按照从左到右的顺序输入,还需要预处理计算出输入中的第i只蚂蚁的序号order[i]。

代码如下:
 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 const int maxn = 10000 + 5;
 6 
 7 struct Ant {
 8   int id; // 输入顺序
 9   int p;  // 位置
10   int d;  // 朝向。 -1: 左; 0:转身中; 1:右
11   bool operator < (const Ant& a) const {
12     return p < a.p;
13   }
14 } before[maxn], after[maxn];
15 
16 const char dirName[][10] = {"L", "Turning", "R"};
17 
18 int order[maxn]; // 输入的第i只蚂蚁是终态中的左数第order[i]只蚂蚁
19 
20 int main() {
21   int K;
22   scanf("%d", &K);
23   for(int kase = 1; kase <= K; kase++) {
24     int L, T, n;
25     printf("Case #%d:\n", kase);
26     scanf("%d%d%d", &L, &T, &n);
27     for(int i = 0; i < n; i++) {
28       int p, d;
29       char c;
30       scanf("%d %c", &p, &c);
31       d = (c == 'L' ? -1 : 1);
32       before[i] = (Ant){i, p, d};
33       after[i] = (Ant){0, p+T*d, d}; // 这里的id是未知的
34     }
35 
36     // 计算order数组
37     sort(before, before+n);
38     for(int i = 0; i < n; i++)
39       order[before[i].id] = i;
40 
41     // 计算终态
42     sort(after, after+n);    
43     for(int i = 0; i < n-1; i++) // 修改碰撞中的蚂蚁的方向
44       if(after[i].p == after[i+1].p) after[i].d = after[i+1].d = 0;
45 
46     // 输出结果
47     for(int i = 0; i < n; i++) {
48       int a = order[i]; 
49       if(after[a].p < 0 || after[a].p > L) printf("Fell off\n");
50       else printf("%d %s\n", after[a].p, dirName[after[a].d+1]);
51     }
52     printf("\n");
53   }
54   return 0;
55 }

 

 

转载于:https://www.cnblogs.com/acm-bingzi/p/3198338.html

本项目是一个基于SSM(Spring+SpringMVC+MyBatis)框架和Vue.js前端技术的大学生第二课堂系统,旨在为大学生提供一个便捷、高效的学习和实践平台。项目包含了完整的数据库设计、后端Java代码实现以及前端Vue.js页面展示,适合计算机相关专业的毕设学生和需要进行项目实战练习的Java学习者。 在功能方面,系统主要实现了以下几个模块:用户管理、课程管理、活动管理、成绩管理和通知公告。用户管理模块支持学生和教师的注册、登录及权限管理;课程管理模块允许教师上传课程资料、设置课程时间,并由学生进行选课;活动管理模块提供了活动发布、报名和签到功能,鼓励学生参与课外实践活动;成绩管理模块则用于记录和查询学生的课程成绩和活动参与情况;通知公告模块则实时发布学校或班级的最新通知和公告。 技术实现上,后端采用SSM框架进行开发,Spring负责业务逻辑层,SpringMVC处理Web请求,MyBatis进行数据库操作,确保了系统的稳定性和扩展性。前端则使用Vue.js框架,结合Axios进行数据请求,实现了前后端分离,提升了用户体验和开发效率。 该项目不仅提供了完整的源代码和相关文档,还包括了详细的数据库设计文档和项目部署指南,为学习和实践提供了便利。对于基础较好的学习者,可以根据自己的需求在此基础上进行功能扩展和优化,进一步提升自己的技术水平和项目实战能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值