【UVA 10881】 经典模拟题
2012-11-12 16:50
337 查看
题目链接:http://acm.hust.edu.cn:8080/judge/problem/viewProblem.action?id=25979
题目大意:一根长L厘米的木棍上有n只蚂蚁,每只蚂蚁有个开始的位置和爬行方向,速度为1.当两只蚂蚁相撞后,两者同时掉头继续爬行,求按输入顺序给出每只蚂蚁T秒后的位置后朝向。
解题思路:
1.每只蚂蚁相撞后同时掉头可以看做对穿而过,关键的问题就在于求位置的变化。
2.按位置从小到大排序,可以惊奇的发现排序后(befor数组和after数组)所有的蚂蚁相对位置并没有变化,改变的只是朝向。
题目大意:一根长L厘米的木棍上有n只蚂蚁,每只蚂蚁有个开始的位置和爬行方向,速度为1.当两只蚂蚁相撞后,两者同时掉头继续爬行,求按输入顺序给出每只蚂蚁T秒后的位置后朝向。
解题思路:
1.每只蚂蚁相撞后同时掉头可以看做对穿而过,关键的问题就在于求位置的变化。
2.按位置从小到大排序,可以惊奇的发现排序后(befor数组和after数组)所有的蚂蚁相对位置并没有变化,改变的只是朝向。
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int maxn=10005; struct node { int id, p, d; //d表示朝向,-1表示左,0表示碰撞中,1表示右。 bool operator < (const node& S)const { return p<S.p; } } befor[maxn],after[maxn]; char dirname[][10]={"L","Turning","R"}; int order[maxn]; int main() { int T, n, L, t, tcase=0; int ss[10005]; cin >> T; while(T--) { scanf("%d%d%d",&L, &t, &n); printf("Case #%d:\n",++tcase); for(int i=0; i<n; i++) { int p, d; char c; scanf("%d %c",&p,&c); d=(c=='L'?-1:1); befor[i]=(node){i,p,d}; after[i]=(node){0,p+t*d,d}; //所以的蚂蚁相撞后可以看做对穿而过 } sort(befor,befor+n); for(int i=0; i<n; i++) //最巧妙的地方在这里,第一次从左到右所有的蚂蚁的相对位置没有变化 order[befor[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("%d %s\n",after[a].p,dirname[after[a].d+1]); } else printf("Fell off\n"); } puts(""); } return 0; }
相关文章推荐
- POJ 1852 Ants || UVA 10881 - Piotr's Ants 经典的蚂蚁问题
- uva 10881 模拟题
- POJ 1852 Ants || UVA 10881 - Piotr's Ants 经典的蚂蚁问题
- UVA 10881(经典模拟,trick点)
- UVa - 10881 - Piotr's Ants
- 经典第五章 例 5-6 UVA 540 Team Queue(队列的简单应用)【queue】
- UVA 10534 - Wavio Sequence(经典dp)
- 算法竞赛入门经典第四章习题4-7 RAID! UVA - 509
- 算法竞赛入门经典-习题3-2 分子量(Molar Mass, ACM/ICPC Seoul 2007, UVa1586)
- uva 10881(贪心)
- 算法竞赛入门经典第六章例题6-7 Trees on the level UVA - 122
- UVA 1378 A Funny Stone Game (经典博弈)
- 思路题(蚂蚁,uva 10881)
- UVA 11235 经典RMQ问题
- UVA - 524 Prime Ring Problem (经典回溯问题)
- Uva 10881 Piotr’s Ants 蚂蚁
- 【算法竞赛入门经典】7.5 路径寻找问题 例题7-9 UVa1601(1)
- 算法竞赛入门经典 UVa1585
- UVa 10881 Piotr's Ants
- [算法竞赛入门经典] UVA 1471 - Defense Lines