Uva 10881蚂蚁
2014-07-26 23:48
225 查看
#include<iostream> #include<algorithm> using namespace std; int position; //记录相撞时候的位置 const int maxn = 10000 + 5; struct Ant { int id; int p; int d; bool operator <(const Ant& a) const { return p<a.p; } } before[maxn],after[maxn]; const char dirName[][10] = {"L","Turning","R"}; int order[maxn]; int main() { int K; cin>>K; for(int i=1; i<=K; i++) { int L,T,n; cout<<"case"<<i<<":"<<endl; cin>>L>>T>>n; for(int i=0; i<n; i++) { int p,d; char c; cin>>p>>c; d = (c == 'L'? -1:1);//d为direction,即方向,左为-1,右为1; //下面为录入每只蚂蚁的信息 before[i].id= i; before[i].p = p; before[i].d = d; //经过移动后每只蚂蚁的信息 after[i].id = 0; after[i].p = p+T*d; after[i].d = d; } //蚂蚁的相对顺序是不变的,所以根据相对左端位置排序后,弄成编号. 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) //相撞的情况 { position = after[i].p; //保存相撞的位置,以便于输出 after[i].p = after[i+1].p = 0;//相撞置0 } } for(int i=0; i<n; i++) { int a = order[i]; //存入a可以便于下面代码的可读性. if(after[a].p < 0 || after[a].p >L) cout<<"Fell off"<<endl; else if(after[a].p == 0) cout<<position<<" "<<"Turning"<<endl; else cout<<after[a].p<<" "<<dirName[after[a].d+1]<<endl; } cout<<endl; } }
相关文章推荐
- 蚂蚁(Piotr's Ants,UVa 10881)
- UVa 10881 蚂蚁(等效变换)
- 思路题(蚂蚁,uva 10881)
- (uva 10881)蚂蚁(Piotr's Ants)
- uva 10881 蚂蚁
- 例题5 UVA 10881 蚂蚁(Piotr's Ants)
- Uva 10881 Piotr’s Ants 蚂蚁
- Uva---10881 Piotr's Ants(蚂蚁)
- POJ 1852 Ants || UVA 10881 - Piotr's Ants 经典的蚂蚁问题
- [ACM_模拟] UVA 10881 Piotr's Ants[蚂蚁移动 数组映射 排序技巧]
- POJ 1852 Ants || UVA 10881 - Piotr's Ants 经典的蚂蚁问题
- UVa 10881 蚂蚁
- UVA-10881 蚂蚁 Piotr's Ants
- UVA-10881 蚂蚁 Piotr's Ants
- uva 10881 蚂蚁
- UVa 10881 Piotr's Ants 蚂蚁
- 【巧妙的模拟】【UVA 10881】 - Piotr's Ants/Piotr的蚂蚁
- 蚂蚁-UVA 10881 - Piotr's Ants
- 【巧妙的模拟】【UVA 10881】 - Piotr's Ants/Piotr的蚂蚁
- uva10881 Piotr's Ants