uva 10881(ants)
来源:互联网 发布:mm下载软件 编辑:程序博客网 时间:2024/04/27 11:05
题目:
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 first 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.
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只蚂蚁,初始状态时每只蚂蚁要么朝左要么朝右爬,求T秒后每只蚂蚁的位置和状态。
ac代码:
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;const char dirname[][10]={"L","Turning","R"};const int maxn=10005;struct ant{ int id; int pos; int dir; bool operator < (const ant& a) const { return pos < a.pos; }}before[maxn],after[maxn];bool cmp(ant a,ant b){ return a.id<b.id;}int main(){ //freopen("test.txt","r",stdin); int kase; scanf("%d",&kase); for(int cas=1;cas<=kase;cas++) { int l,t,n; cin >> l >> t >> n; for(int i=1;i<=n;i++) { char c; int pos,d; scanf("%d %c",&pos,&c); d=c=='L'? -1:1; before[i]=(ant){i,pos,d}; after[i]=(ant){0,pos+t*d,d}; } sort(before+1,before+n+1); sort(after+1,after+n+1); for(int i=1;i<=n;i++) after[i].id=before[i].id; for(int i=1;i<n;i++) { if(after[i].pos==after[i+1].pos) after[i].dir=after[i+1].dir=0; } sort(after+1,after+n+1,cmp); printf("Case #%d:\n",cas); for(int i=1;i<=n;i++) { if(after[i].pos<0||after[i].pos>l) { printf("Fell off\n"); continue; } printf("%d %s\n",after[i].pos,dirname[after[i].dir+1]); } printf("\n"); } return 0;}
刘汝佳老师版:
#include<cstdio>#include<algorithm>using namespace std;const int maxn = 10000 + 5;struct Ant { int id; // 输入顺序 int p; // 位置 int d; // 朝向。 -1: 左; 0:转身中; 1:右 bool operator < (const Ant& a) const { return p < a.p; }} before[maxn], after[maxn];const char dirName[][10] = {"L", "Turning", "R"};int order[maxn]; // 输入的第i只蚂蚁是终态中的左数第order[i]只蚂蚁int main() { int K; scanf("%d", &K); for(int kase = 1; kase <= K; kase++) { int L, T, n; printf("Case #%d:\n", kase); scanf("%d%d%d", &L, &T, &n); for(int i = 0; i < n; i++) { int p, d; char c; scanf("%d %c", &p, &c); d = (c == 'L' ? -1 : 1); before[i] = (Ant){i, p, d}; after[i] = (Ant){0, p+T*d, d}; // 这里的id是未知的 } // 计算order数组 sort(before, before+n); for(int i = 0; i < n; i++) order[before[i].id] = i; // 计算终态 sort(after, after+n); for(int i = 0; i < n-1; i++) // 修改碰撞中的蚂蚁的方向 if(after[i].p == after[i+1].p) after[i].d = after[i+1].d = 0; // 输出结果 for(int i = 0; i < n; i++) { int a = order[i]; if(after[a].p < 0 || after[a].p > L) printf("Fell off\n"); else printf("%d %s\n", after[a].p, dirName[after[a].d+1]); } printf("\n"); } return 0;}
- uva 10881(ants)
- UVa - 10881 - Piotr's Ants
- uva 10881 - Piotr's Ants
- Uva-10881-Piotr's Ants
- uva 10881 - Piotr's Ants
- UVa 10881 Piotr's Ants
- UVA 10881 Piotr's Ants
- Piotr's ants UVA 10881
- UVa 10881 - Piotr's Ants
- UVa 10881 Piotr's Ants
- UVA - 10881 Piotr's Ants
- uva 10881 Piotr's Ants
- UVA - 10881 Piotr's Ants
- UVa 10881 - Piotr's Ants
- UVa 10881 - Piotr's Ants
- Uva 10881 - Piotr's Ants
- UVA 10881 Piotr's Ants
- Uva 10881 Pior's Ants
- 2016年3月5日CVTE网测的两道编程题
- React-Native中this的带给大家的困惑
- mysql中utf8_bin、utf8_general_ci、utf8_general_cs编码区别
- Linux内核分析,完成一个简单的时间片轮转多道程序内核代码
- java学习--内存泄露和内存溢出
- uva 10881(ants)
- table合并单元格colspan和rowspan
- 【ZYNQ-7000开发之十三】中断:私有定时器中断
- 进程与线程的区别(转)
- 微信扫码关注后实现自动分组
- Android开发之ContentProvider与ContentResolver的使用
- ACM 24 素数距离问题(记录距离最近的素数)
- EL表达<%@page isELIgnored="false"%>问题
- [LeetCode]287. Find the Duplicate Number